Skip to content

Commit a9e4f1e

Browse files
committed
fix: address CodeQL security vulnerabilities in URL handling
- Validate and sanitize URLs before loading in iframe - Use URL constructor to parse and validate URLs - Only allow HTTP and HTTPS protocols - Use setAttribute instead of direct property assignment - Add proper error handling for invalid URLs This fixes: - Client-side URL redirect vulnerability - DOM text reinterpreted as HTML - Client-side cross-site scripting (XSS)
1 parent e6b80cc commit a9e4f1e

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/core/webview/preview/preview.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,35 @@
113113
}
114114

115115
function loadUrl(url) {
116-
// Ensure URL has protocol
117-
if (!url.startsWith("http://") && !url.startsWith("https://")) {
118-
url = "http://" + url
119-
}
120-
116+
// Validate and sanitize URL
121117
try {
122-
iframe.src = url
123-
document.getElementById("urlInput").value = url
118+
// Ensure URL has protocol
119+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
120+
url = "http://" + url
121+
}
122+
123+
// Parse and validate URL
124+
const parsedUrl = new URL(url)
125+
126+
// Only allow http and https protocols
127+
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
128+
throw new Error("Only HTTP and HTTPS protocols are allowed")
129+
}
130+
131+
// Create a safe URL string
132+
const safeUrl = parsedUrl.toString()
133+
134+
// Set iframe source using setAttribute for better security
135+
iframe.setAttribute("src", safeUrl)
136+
137+
// Update input field with the safe URL
138+
const urlInput = document.getElementById("urlInput")
139+
urlInput.value = safeUrl
124140

125141
// Notify extension
126142
vscode.postMessage({
127143
type: "urlChanged",
128-
url: url,
144+
url: safeUrl,
129145
})
130146

131147
// Setup iframe load handler
@@ -137,7 +153,7 @@
137153
} catch (error) {
138154
vscode.postMessage({
139155
type: "error",
140-
error: error.message,
156+
error: "Invalid URL: " + error.message,
141157
})
142158
}
143159
}

0 commit comments

Comments
 (0)