Skip to content

Update xss-example.js #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

yhaggai
Copy link
Contributor

@yhaggai yhaggai commented Aug 6, 2025

No description provided.

@@ -4,4 +4,5 @@
var username = urlParams.get('username');

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 8 days 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.

2 participants