-
Notifications
You must be signed in to change notification settings - Fork 98
fix(#657): prevent HTML injection and XSS by sanitizing participant names #819
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
Shan7Usmani
wants to merge
4
commits into
shouri123:main
Choose a base branch
from
Shan7Usmani:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
86256fa
fix(#657): prevent HTML injection and XSS by sanitizing participant n…
Shan7Usmani bf64afc
docs: add detailed troubleshooting and common errors section to README
DeepeshKafalatiya 6cd8014
style: fix README.md formatting with Prettier
DeepeshKafalatiya 7c51bcf
Merge branch 'main' into main
Shan7Usmani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { escapeHtml, formatDuration, sanitizeTopicStatus } from "./domHelpers"; | ||
|
|
||
| test("escapeHtml prevents XSS", () => { | ||
| assert.equal(escapeHtml(null), ""); | ||
| assert.equal(escapeHtml(undefined), ""); | ||
|
|
||
| assert.equal(escapeHtml(""), ""); | ||
| assert.equal(escapeHtml("Alice"), "Alice"); | ||
| assert.equal(escapeHtml("O'Brien"), "O'Brien"); | ||
| assert.equal(escapeHtml('Say "hello"'), "Say "hello""); | ||
| assert.equal(escapeHtml("a < b"), "a < b"); | ||
| assert.equal(escapeHtml("a > b"), "a > b"); | ||
| assert.equal(escapeHtml("M&Ms"), "M&Ms"); | ||
|
|
||
| const xss = '<script>alert("xss")</script>'; | ||
| assert.equal(escapeHtml(xss), "<script>alert("xss")</script>"); | ||
|
|
||
| const imgXss = '<img src=x onerror="alert(1)">'; | ||
| assert.equal(escapeHtml(imgXss), "<img src=x onerror="alert(1)">"); | ||
|
|
||
| const safe = escapeHtml(xss); | ||
| assert.ok(!safe.includes("<script>")); | ||
| assert.ok(!safe.includes("</script>")); | ||
| assert.ok(!safe.includes('"')); | ||
| }); | ||
|
|
||
| test("escapeHtml handles edge cases", () => { | ||
| assert.equal(escapeHtml(" "), " "); | ||
| assert.equal(escapeHtml("a".repeat(1000)), "a".repeat(1000)); | ||
|
|
||
| assert.equal(escapeHtml("Jack & Jill"), "Jack &amp; Jill"); | ||
|
|
||
| assert.equal(escapeHtml("\n\t"), "\n\t"); | ||
| }); | ||
|
|
||
| test("escapeHtml in attribute context prevents injection", () => { | ||
| const name = 'John "Drop Table" Doe'; | ||
| const escaped = escapeHtml(name); | ||
| const attr = `data-name="${escaped}"`; | ||
| assert.equal(attr, 'data-name="John "Drop Table" Doe"'); | ||
|
|
||
| const singleQuote = "O'Brien"; | ||
| const escaped2 = escapeHtml(singleQuote); | ||
| const attr2 = `data-author="${escaped2}"`; | ||
| assert.equal(attr2, 'data-author="O'Brien"'); | ||
| }); | ||
|
|
||
| test("regression: malicious speaker name in transcript entry pattern", () => { | ||
| const malicious = '<script>alert("xss")</script>'; | ||
| const speaker = escapeHtml(malicious); | ||
|
|
||
| const initials = speaker | ||
| .split(" ") | ||
| .filter(Boolean) | ||
| .map((w) => w[0]) | ||
| .join("") | ||
| .toUpperCase() | ||
| .slice(0, 2); | ||
|
|
||
| assert.equal(initials, "&"); | ||
| assert.ok(!initials.includes("<")); | ||
| assert.ok(!initials.includes(">")); | ||
| assert.ok(!initials.includes('"')); | ||
|
|
||
| const html = `<div class="transcript-speaker">${speaker}</div>`; | ||
| assert.ok(!html.includes("<script>")); | ||
| assert.ok(html.includes("<script>")); | ||
| }); | ||
|
|
||
| test("escapeHtml output is safe when embedded in innerHTML", () => { | ||
| const payloads = [ | ||
| '<script>alert("xss")</script>', | ||
| '<img src=x onerror="alert(1)">', | ||
| '"><script>alert(1)</script>', | ||
| "javascript:alert(1)", | ||
| "<<script>script>alert(1)</script>", | ||
| ]; | ||
|
|
||
| for (const payload of payloads) { | ||
| const escaped = escapeHtml(payload); | ||
| assert.ok(!escaped.includes("<script>"), `payload still contains <script>: ${payload}`); | ||
| assert.ok(!escaped.includes("</script>"), `payload still contains </script>: ${payload}`); | ||
| assert.ok(!escaped.includes('"'), `payload still contains double quote: ${payload}`); | ||
| } | ||
| }); | ||
|
|
||
| test("formatDuration", () => { | ||
| assert.equal(formatDuration(0), "00:00:00"); | ||
| assert.equal(formatDuration(90), "00:01:30"); | ||
| assert.equal(formatDuration(3661), "01:01:01"); | ||
| assert.equal(formatDuration(86399), "23:59:59"); | ||
| assert.equal(formatDuration(360000), "100:00:00"); | ||
| }); | ||
|
|
||
| test("sanitizeTopicStatus", () => { | ||
| assert.equal(sanitizeTopicStatus("completed"), "completed"); | ||
| assert.equal(sanitizeTopicStatus("unresolved"), "unresolved"); | ||
| assert.equal(sanitizeTopicStatus("active"), "active"); | ||
| assert.equal(sanitizeTopicStatus("pending"), "active"); | ||
| assert.equal(sanitizeTopicStatus(""), "active"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.