Create tettst.html - #128
Conversation
Hacktron Security Check - SkippedReason: Billing required for Code Review seats
|
| 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 Handler in tettst.html
The newly added file testerror/tettst.html introduces an insecure message event listener that does not validate the sender's origin (event.origin) and directly processes the message payload (event.data) using the dangerous innerHTML DOM sink.
An attacker hosting a malicious website can open this page in a window, tab, or iframe, and send a crafted postMessage containing malicious HTML/JavaScript. Because there is no origin check, the listener will accept the message and inject it directly into the DOM, executing arbitrary JavaScript in the context of the victim's session.
While this repository is designed as an educational demonstration of insecure postMessage handling, adding this new file introduces a new endpoint vulnerable to DOM-based Cross-Site Scripting (XSS).
Steps to Reproduce
- Open
testerror/tettst.htmlin a web browser. - Open the browser developer console on any other website (e.g.,
https://example.com). - Execute the following JavaScript to open the vulnerable page and send a malicious payload:
const win = window.open('path/to/testerror/tettst.html'); setTimeout(() => { win.postMessage('<img src=x onerror="alert(document.domain)">', '*'); }, 1000);
- Observe the alert dialog executing in the context of the vulnerable page.
Fix with AI
A security vulnerability was found by Hacktron.
File: tettst.html
Lines: 30-37
Severity: high
Vulnerability: DOM-based Cross-Site Scripting (XSS) via Insecure postMessage Handler in tettst.html
Description:
The newly added file `testerror/tettst.html` introduces an insecure `message` event listener that does not validate the sender's origin (`event.origin`) and directly processes the message payload (`event.data`) using the dangerous `innerHTML` DOM sink.
An attacker hosting a malicious website can open this page in a window, tab, or iframe, and send a crafted `postMessage` containing malicious HTML/JavaScript. Because there is no origin check, the listener will accept the message and inject it directly into the DOM, executing arbitrary JavaScript in the context of the victim's session.
While this repository is designed as an educational demonstration of insecure `postMessage` handling, adding this new file introduces a new endpoint vulnerable to DOM-based Cross-Site Scripting (XSS).
Proof of Concept:
**Steps to Reproduce**
1. Open `testerror/tettst.html` in a web browser.
2. Open the browser developer console on any other website (e.g., `https://example.com`).
3. Execute the following JavaScript to open the vulnerable page and send a malicious payload:
```javascript
const win = window.open('path/to/testerror/tettst.html');
setTimeout(() => {
win.postMessage('<img src=x onerror="alert(document.domain)">', '*');
}, 1000);
```
4. Observe the alert dialog executing in the context of the vulnerable page.
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.
There was a problem hiding this comment.
A fix for this finding has been opened: #129
No description provided.