Skip to content

Commit 7029f1d

Browse files
committed
security: fix polynomial regex and improve URL sanitization in imageDataUrl.ts
- Replace potentially polynomial regex /(?:Users|C:)([^?#]+\.(?:png|jpg|jpeg|gif|webp))/i - Split into separate bounded patterns for Unix and Windows paths - Add length limit {0,500} to prevent ReDoS attacks - Improve URL substring sanitization for vscode-userdata and vscode-cdn.net URIs - Addresses GitHub CodeQL security warnings for polynomial regex vulnerability
1 parent 32b7085 commit 7029f1d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/integrations/misc/imageDataUrl.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ function webviewUriToFilePath(webviewUri: string): string {
5757
if (webviewUri.includes("vscode-userdata") || webviewUri.includes("vscode-cdn.net")) {
5858
// Try to decode the URI and extract the file path
5959
const decoded = decodeURIComponent(webviewUri)
60-
// Look for a file path pattern in the decoded URI
61-
const pathMatch = decoded.match(/(?:Users|C:)([^?#]+\.(?:png|jpg|jpeg|gif|webp))/i)
60+
61+
// Use safer, non-polynomial regex patterns
62+
// Look for Unix-style paths first
63+
let pathMatch = decoded.match(/\/Users\/[^?#]*\.(?:png|jpg|jpeg|gif|webp)/i)
64+
if (pathMatch) {
65+
return pathMatch[0]
66+
}
67+
68+
// Look for Windows-style paths with bounded length to prevent polynomial behavior
69+
pathMatch = decoded.match(/C:\\[^?#]{0,500}\.(?:png|jpg|jpeg|gif|webp)/i)
6270
if (pathMatch) {
63-
const extractedPath = pathMatch[0]
64-
return extractedPath
71+
return pathMatch[0]
6572
}
6673
}
6774

0 commit comments

Comments
 (0)