Skip to content

Commit 3d796de

Browse files
committed
security: fix host injection vulnerability in URL validation
- Replace dangerous substring check webviewUri.includes('vscode-cdn.net') - Add proper URL host validation using URL constructor - Check url.host === 'vscode-cdn.net' to prevent injection via paths/queries - Graceful fallback when URL parsing fails - Addresses final CodeQL warning for incomplete URL substring sanitization
1 parent a1c402e commit 3d796de

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/integrations/misc/imageDataUrl.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function webviewUriToFilePath(webviewUri: string): string {
5757
// Use strict prefix matching to prevent arbitrary host injection
5858
if (
5959
webviewUri.startsWith("vscode-resource://vscode-webview/") &&
60-
(webviewUri.includes("vscode-userdata") || webviewUri.includes("vscode-cdn.net"))
60+
(webviewUri.includes("vscode-userdata") || isValidVsCodeCdnHost(webviewUri))
6161
) {
6262
try {
6363
// Decode safely with length limits
@@ -92,6 +92,20 @@ function webviewUriToFilePath(webviewUri: string): string {
9292
/**
9393
* Gets the MIME type from a file path
9494
*/
95+
/**
96+
* Safely validates if a webview URI is from the trusted vscode-cdn.net host
97+
* Prevents host injection attacks by properly parsing the URL
98+
*/
99+
function isValidVsCodeCdnHost(webviewUri: string): boolean {
100+
try {
101+
const url = new URL(webviewUri)
102+
return url.host === "vscode-cdn.net"
103+
} catch {
104+
// URL parsing failed - not a valid URL
105+
return false
106+
}
107+
}
108+
95109
function getMimeTypeFromPath(filePath: string): string {
96110
const ext = path.extname(filePath).toLowerCase()
97111

0 commit comments

Comments
 (0)