MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / parseDeepLink

Function parseDeepLink

source code/utils/deepLink/parseDeepLink.ts:86–155  ·  view source on GitHub ↗
(uri: string)

Source from the content-addressed store, hash-verified

84 * @throws {Error} if the URI is malformed or contains dangerous characters
85 */
86export function parseDeepLink(uri: string): DeepLinkAction {
87 // Normalize: accept with or without the trailing colon in protocol
88 const normalized = uri.startsWith(`${DEEP_LINK_PROTOCOL}://`)
89 ? uri
90 : uri.startsWith(`${DEEP_LINK_PROTOCOL}:`)
91 ? uri.replace(`${DEEP_LINK_PROTOCOL}:`, `${DEEP_LINK_PROTOCOL}://`)
92 : null
93
94 if (!normalized) {
95 throw new Error(
96 `Invalid deep link: expected ${DEEP_LINK_PROTOCOL}:// scheme, got "${uri}"`,
97 )
98 }
99
100 let url: URL
101 try {
102 url = new URL(normalized)
103 } catch {
104 throw new Error(`Invalid deep link URL: "${uri}"`)
105 }
106
107 if (url.hostname !== 'open') {
108 throw new Error(`Unknown deep link action: "${url.hostname}"`)
109 }
110
111 const cwd = url.searchParams.get('cwd') ?? undefined
112 const repo = url.searchParams.get('repo') ?? undefined
113 const rawQuery = url.searchParams.get('q')
114
115 // Validate cwd if present — must be an absolute path
116 if (cwd && !cwd.startsWith('/') && !/^[a-zA-Z]:[/\\]/.test(cwd)) {
117 throw new Error(
118 `Invalid cwd in deep link: must be an absolute path, got "${cwd}"`,
119 )
120 }
121
122 // Reject control characters in cwd (newlines, etc.) but allow path chars like backslash.
123 if (cwd && containsControlChars(cwd)) {
124 throw new Error('Deep link cwd contains disallowed control characters')
125 }
126 if (cwd && cwd.length > MAX_CWD_LENGTH) {
127 throw new Error(
128 `Deep link cwd exceeds ${MAX_CWD_LENGTH} characters (got ${cwd.length})`,
129 )
130 }
131
132 // Validate repo slug format. Resolution happens later (protocolHandler.ts) —
133 // this parser stays pure with no config/filesystem access.
134 if (repo && !REPO_SLUG_PATTERN.test(repo)) {
135 throw new Error(
136 `Invalid repo in deep link: expected "owner/repo", got "${repo}"`,
137 )
138 }
139
140 let query: string | undefined
141 if (rawQuery && rawQuery.trim().length > 0) {
142 // Strip hidden Unicode characters (ASCII smuggling / hidden prompt injection)
143 query = partiallySanitizeUnicode(rawQuery.trim())

Callers 1

handleDeepLinkUriFunction · 0.85

Calls 3

containsControlCharsFunction · 0.85
partiallySanitizeUnicodeFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected