Skip to content

Conversation

@zutshisunakshi
Copy link
Member

No description provided.

form.addEventListener('submit', (ev) => {
ev.preventDefault();
const q = document.getElementById('input').value;
document.getElementById('out').innerHTML = q;

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.

Copilot Autofix

AI 4 months ago

To prevent DOM-based XSS, you should never assign untrusted user input directly to innerHTML. Instead, use a method that treats the input as plain text. In the case of this example, replacing innerHTML with either textContent or ensuring output is properly escaped before assignment is safest.
The best way to fix this specific issue:

  • Change line 23 from document.getElementById('out').innerHTML = q; to document.getElementById('out').textContent = q;.

This change ensures that any characters in q with HTML significance (such as <, >, &) will be rendered as text, not interpreted as markup or script.

No new imports or library dependencies are required for this fix.


Suggested changeset 1
index.html

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/index.html b/index.html
--- a/index.html
+++ b/index.html
@@ -20,7 +20,7 @@
     form.addEventListener('submit', (ev) => {
       ev.preventDefault();
       const q = document.getElementById('input').value;
-      document.getElementById('out').innerHTML = q;
+      document.getElementById('out').textContent = q;
     });
   </script>
 </body>
EOF
@@ -20,7 +20,7 @@
form.addEventListener('submit', (ev) => {
ev.preventDefault();
const q = document.getElementById('input').value;
document.getElementById('out').innerHTML = q;
document.getElementById('out').textContent = q;
});
</script>
</body>
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new HTML test page designed to demonstrate XSS (Cross-Site Scripting) vulnerabilities for local testing purposes. The page contains a form that accepts user input and directly outputs it to the DOM without sanitization.

  • Adds a complete HTML page with form input and JavaScript handling
  • Implements vulnerable XSS behavior by directly inserting user input into innerHTML
  • Includes basic form structure with submit handling and DOM manipulation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

<title>Vulnerable XSS Test Page (Local test)</title>
</head>
<body>
<h1>Vulnerable Testt</h1>
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in the heading text. 'Testt' should be 'Test'.

Suggested change
<h1>Vulnerable Testt</h1>
<h1>Vulnerable Test</h1>

Copilot uses AI. Check for mistakes.
const form = document.getElementById('f');
form.addEventListener('submit', (ev) => {
ev.preventDefault();
const q = document.getElementById('input').value;
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is vulnerable to XSS attacks as it directly inserts user input into innerHTML without sanitization. For a test page demonstrating vulnerabilities, this is intentional, but consider adding a comment explaining this is the vulnerable behavior being demonstrated.

Suggested change
const q = document.getElementById('input').value;
const q = document.getElementById('input').value;
// WARNING: This is intentionally vulnerable code for XSS demonstration purposes.
// Do NOT use this pattern in production. Always sanitize user input before inserting into innerHTML.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

<title>Vulnerable XSS Test Page (Local test)</title>
</head>
<body>
<h1>Vulnerable Testt</h1>
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word "Testt" contains a spelling error with an extra 't' at the end. It should be "Test".

Suggested change
<h1>Vulnerable Testt</h1>
<h1>Vulnerable Test</h1>

Copilot uses AI. Check for mistakes.
form.addEventListener('submit', (ev) => {
ev.preventDefault();
const q = document.getElementById('input').value;
document.getElementById('out').innerHTML = q;
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is vulnerable to Cross-Site Scripting (XSS) attacks. User input is directly inserted into the DOM via innerHTML without any sanitization. An attacker could inject malicious scripts by entering HTML/JavaScript code. Use textContent instead of innerHTML, or implement proper input sanitization if HTML content is genuinely needed.

Suggested change
document.getElementById('out').innerHTML = q;
document.getElementById('out').textContent = q;

Copilot uses AI. Check for mistakes.
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