Skip to content

Conversation

@yhaggai
Copy link
Contributor

@yhaggai yhaggai commented Aug 6, 2025

No description provided.

var unsafe_div = window.document.getElementById("vulnerable-div");
unsafe_div.innerHTML = "Hello to you ";
// here's an XSS:
unsafe_div.innerHTML = "Hello to you " + username;

Check failure

Code scanning / CodeQL

Client-side cross-site scripting High

Cross-site scripting vulnerability due to
user-provided value
.

Copilot Autofix

AI 5 months ago

To fix the DOM-based XSS vulnerability, we must ensure that any user-provided data written to the DOM is properly escaped or encoded. The best way to do this is to avoid using innerHTML for untrusted data and instead use textContent, which automatically escapes any HTML special characters. This preserves the original functionality (displaying a greeting with the username) while preventing XSS. The change should be made in xss-example.js, replacing the assignment to innerHTML with an assignment to textContent. No additional imports or dependencies are required.


Suggested changeset 1
xss-example.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/xss-example.js b/xss-example.js
--- a/xss-example.js
+++ b/xss-example.js
@@ -7,2 +7,2 @@
 // here's an XSS:
-unsafe_div.innerHTML = "Hello to you " + username;
+unsafe_div.textContent = "Hello to you " + username;
EOF
@@ -7,2 +7,2 @@
// here's an XSS:
unsafe_div.innerHTML = "Hello to you " + username;
unsafe_div.textContent = "Hello to you " + username;
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants