Skip to content

Commit 983f5f6

Browse files
committed
fix: validate protocol of urls more securely
1 parent 1852d7d commit 983f5f6

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

server/utils/readme.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,20 @@ function resolveUrl(url: string, packageName: string, repoInfo?: RepositoryInfo)
206206
if (url.startsWith('#')) {
207207
return url
208208
}
209-
if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith('//')) {
210-
return url
209+
if (hasProtocol(url, { acceptRelative: true })) {
210+
try {
211+
const parsed = new URL(url, 'https://example.com')
212+
if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
213+
return url
214+
}
215+
} catch {
216+
// Invalid URL, fall through to resolve as relative
217+
}
218+
// return protocol-relative URLs (//example.com) as-is
219+
if (url.startsWith('//')) {
220+
return url
221+
}
222+
// for non-HTTP protocols (javascript:, data:, etc.), don't return, treat as relative
211223
}
212224

213225
// Prefer GitHub raw URLs when repository info is available

0 commit comments

Comments
 (0)