Create ffsdf.html#52
Conversation
| window.addEventListener('message', (event) => { | ||
| // Shows the origin but FAILS to validate it (critical bug) | ||
| document.getElementById('origin').textContent = 'Last message origin: ' + event.origin; | ||
|
|
||
| // Dangerous sink: direct innerHTML assignment of untrusted data | ||
| const incoming = typeof event.data === 'string' ? event.data : JSON.stringify(event.data); | ||
| document.getElementById('output').innerHTML = incoming; | ||
| }); |
There was a problem hiding this comment.
DOM-based Cross-Site Scripting (XSS) via Unvalidated postMessage in ffsdf.html
The pull request introduces a new static HTML file ffsdf.html containing an insecure postMessage event listener. The listener registers a handler on the window object that processes incoming cross-origin messages without validating the sender's origin (event.origin). The handler then extracts the payload from event.data and assigns it directly to the innerHTML property of the #output DOM element.
An attacker can exploit this by hosting a malicious website that opens ffsdf.html (either in an iframe or via window.open) and sending a payload containing malicious HTML/JavaScript (e.g., <img src=x onerror="alert(document.domain)">). Because there is no origin validation, the message is accepted and rendered as HTML, leading to DOM-based Cross-Site Scripting (XSS) in the context of the domain hosting ffsdf.html. This could allow the attacker to execute arbitrary code, steal sensitive local data (such as localStorage or cookies), or perform unauthorized actions on behalf of the user.
Steps to Reproduce
- Host
ffsdf.htmlon a target domain (e.g.,http://victim.local/ffsdf.html). - An attacker hosts a malicious page with the following script:
const win = window.open('http://victim.local/ffsdf.html');
setTimeout(() => {
win.postMessage('<img src=x onerror="alert(\'XSS on \' + document.domain)">', '*');
}, 1000);- When a victim visits the attacker's page, it opens the target page and executes the injected script in the victim's origin context.
Fix with AI
A security vulnerability was found by Hacktron.
File: ffsdf.html
Lines: 30-37
Severity: high
Vulnerability: DOM-based Cross-Site Scripting (XSS) via Unvalidated postMessage in ffsdf.html
Description:
The pull request introduces a new static HTML file `ffsdf.html` containing an insecure `postMessage` event listener. The listener registers a handler on the `window` object that processes incoming cross-origin messages without validating the sender's origin (`event.origin`). The handler then extracts the payload from `event.data` and assigns it directly to the `innerHTML` property of the `#output` DOM element.
An attacker can exploit this by hosting a malicious website that opens `ffsdf.html` (either in an iframe or via `window.open`) and sending a payload containing malicious HTML/JavaScript (e.g., `<img src=x onerror="alert(document.domain)">`). Because there is no origin validation, the message is accepted and rendered as HTML, leading to DOM-based Cross-Site Scripting (XSS) in the context of the domain hosting `ffsdf.html`. This could allow the attacker to execute arbitrary code, steal sensitive local data (such as localStorage or cookies), or perform unauthorized actions on behalf of the user.
Proof of Concept:
**Steps to Reproduce**
1. Host `ffsdf.html` on a target domain (e.g., `http://victim.local/ffsdf.html`).
2. An attacker hosts a malicious page with the following script:
```javascript
const win = window.open('http://victim.local/ffsdf.html');
setTimeout(() => {
win.postMessage('<img src=x onerror="alert(\'XSS on \' + document.domain)">', '*');
}, 1000);
```
3. When a victim visits the attacker's page, it opens the target page and executes the injected script in the victim's origin context.
Affected Code:
window.addEventListener('message', (event) => {
// Shows the origin but FAILS to validate it (critical bug)
document.getElementById('origin').textContent = 'Last message origin: ' + event.origin;
// Dangerous sink: direct innerHTML assignment of untrusted data
const incoming = typeof event.data === 'string' ? event.data : JSON.stringify(event.data);
document.getElementById('output').innerHTML = incoming;
});
Acceptance criteria:
- Acceptance is defined by the **actual reported behavior**, not by tests passing.
- Reproduce the issue, or narrow the exact code path that produces it, *before* changing code. State what you confirmed.
- Fix the underlying cause. Mitigations that paper over the reported behavior do not count as a fix.
- Add a regression test that fails on the unpatched code and passes on the fix. If a regression test is genuinely impractical (e.g. race condition, infra-level issue), say so and explain why.
- Existing tests passing is **not** the bar. Do not declare done on tests-pass theatre.
Only change what is necessary to fix this vulnerability. Do not refactor adjacent code or modify unrelated files.
Triage: Reply !fp <reason> (false positive), !valid (confirmed), !accepted_risk <reason>, or !fixed (resolved). Any other reply is saved as a triage note.
Reason is optional but improves future scans — e.g. !fp internal endpoint, not user-facing.
| window.addEventListener('message', (event) => { | ||
| // Shows the origin but FAILS to validate it (critical bug) | ||
| document.getElementById('origin').textContent = 'Last message origin: ' + event.origin; | ||
|
|
||
| // Dangerous sink: direct innerHTML assignment of untrusted data | ||
| const incoming = typeof event.data === 'string' ? event.data : JSON.stringify(event.data); | ||
| document.getElementById('output').innerHTML = incoming; | ||
| }); |
There was a problem hiding this comment.
DOM-based Cross-Site Scripting (XSS) via Insecure postMessage Listener
The application registers a message event listener on the window object that processes incoming messages from any origin without validation. It then directly assigns the untrusted message data to the innerHTML property of a DOM element, leading to DOM-based Cross-Site Scripting (XSS). An attacker can exploit this by opening the vulnerable page in an iframe or a new tab/window and sending a malicious postMessage payload.
Steps to Reproduce
- Open
testerror/ffsdf.htmlin a web browser. - Open another tab/window on a different origin (e.g., https://example.com) and run the following script in its console:
const victim = window.opener || window.open('http://localhost:8000/testerror/ffsdf.html'); setTimeout(() => { victim.postMessage('<img src=x onerror="alert(\'XSS executed!\')">', '*'); }, 1000);
- Observe that the alert dialog executes in the context of the vulnerable page's origin.
Fix with AI
A security vulnerability was found by Hacktron.
File: ffsdf.html
Lines: 30-37
Severity: high
Vulnerability: DOM-based Cross-Site Scripting (XSS) via Insecure postMessage Listener
Description:
The application registers a message event listener on the window object that processes incoming messages from any origin without validation. It then directly assigns the untrusted message data to the innerHTML property of a DOM element, leading to DOM-based Cross-Site Scripting (XSS). An attacker can exploit this by opening the vulnerable page in an iframe or a new tab/window and sending a malicious postMessage payload.
Proof of Concept:
**Steps to Reproduce**
1. Open `testerror/ffsdf.html` in a web browser.
2. Open another tab/window on a different origin (e.g., https://example.com) and run the following script in its console:
```javascript
const victim = window.opener || window.open('http://localhost:8000/testerror/ffsdf.html');
setTimeout(() => {
victim.postMessage('<img src=x onerror="alert(\'XSS executed!\')">', '*');
}, 1000);
```
3. Observe that the alert dialog executes in the context of the vulnerable page's origin.
Affected Code:
window.addEventListener('message', (event) => {
// Shows the origin but FAILS to validate it (critical bug)
document.getElementById('origin').textContent = 'Last message origin: ' + event.origin;
// Dangerous sink: direct innerHTML assignment of untrusted data
const incoming = typeof event.data === 'string' ? event.data : JSON.stringify(event.data);
document.getElementById('output').innerHTML = incoming;
});
Acceptance criteria:
- Acceptance is defined by the **actual reported behavior**, not by tests passing.
- Reproduce the issue, or narrow the exact code path that produces it, *before* changing code. State what you confirmed.
- Fix the underlying cause. Mitigations that paper over the reported behavior do not count as a fix.
- Add a regression test that fails on the unpatched code and passes on the fix. If a regression test is genuinely impractical (e.g. race condition, infra-level issue), say so and explain why.
- Existing tests passing is **not** the bar. Do not declare done on tests-pass theatre.
Only change what is necessary to fix this vulnerability. Do not refactor adjacent code or modify unrelated files.
Triage: Reply !fp <reason> (false positive), !valid (confirmed), !accepted_risk <reason>, or !fixed (resolved). Any other reply is saved as a triage note.
Reason is optional but improves future scans — e.g. !fp internal endpoint, not user-facing.
No description provided.