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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion xss-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 10 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.
Loading