-
Notifications
You must be signed in to change notification settings - Fork 53
feat(web): HTML sanitization #680
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
hejsztynx
wants to merge
8
commits into
main
Choose a base branch
from
@ksienkiewicz/feat-html-sanitization
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
8 commits
Select commit
Hold shift + click to select a range
05d5b22
feat(web): mention sanitization
hejsztynx 6ae68b2
feat: mention sanitization tests
hejsztynx d505d54
docs: default mention style documentation
hejsztynx 7eb3c09
test: fix obsolete tests
hejsztynx 543de30
feat: example app update
hejsztynx 0263e54
docs: cleanup
hejsztynx a95a17c
test: link sanitization tests
hejsztynx 726b7ea
feat: pass all mention attrs in normalization process
hejsztynx 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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,113 @@ | ||
| import { | ||
| sanitizeHtml, | ||
| sanitizeMentionAttributes, | ||
| checkMentionAttributes, | ||
| } from '../sanitization/htmlSanitizer'; | ||
|
|
||
| describe('sanitizeMentionAttributes', () => { | ||
| it('returns an empty object when given no attributes', () => { | ||
| expect(sanitizeMentionAttributes()).toEqual({}); | ||
| expect(sanitizeMentionAttributes({})).toEqual({}); | ||
| }); | ||
|
|
||
| it('keeps data-* and commonly-allowed attributes', () => { | ||
| expect( | ||
| sanitizeMentionAttributes({ | ||
| 'data-user-id': '42', | ||
| 'data-team': 'core', | ||
| 'id': 'm1', | ||
| 'class': 'highlight', | ||
| }) | ||
| ).toEqual({ | ||
| 'data-user-id': '42', | ||
| 'data-team': 'core', | ||
| 'id': 'm1', | ||
| 'class': 'highlight', | ||
| }); | ||
| }); | ||
|
|
||
| it('strips event handlers and unsafe attributes', () => { | ||
| const result = sanitizeMentionAttributes({ | ||
| 'onclick': 'alert(1)', | ||
| 'onmouseover': 'steal()', | ||
| // eslint-disable-next-line no-script-url | ||
| 'href': 'javascript:alert(1)', | ||
| 'data-user-id': '42', | ||
| }); | ||
| expect(result).toEqual({ 'data-user-id': '42' }); | ||
| }); | ||
|
|
||
| it('does not return the reserved text/indicator attributes', () => { | ||
| const result = sanitizeMentionAttributes({ | ||
| 'text': 'Joe', | ||
| 'indicator': '@', | ||
| 'data-user-id': '42', | ||
| }); | ||
| expect(result).toEqual({ 'data-user-id': '42' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('checkMentionAttributes', () => { | ||
| let warnSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| warnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('does not warn for data-*, text, indicator, or commonly-allowed attributes', () => { | ||
| checkMentionAttributes({ | ||
| 'data-user-id': '42', | ||
| 'text': 'Joe', | ||
| 'indicator': '@', | ||
| 'id': 'm1', | ||
| 'class': 'x', | ||
| 'style': 'color: red', | ||
| }); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('warns for custom attributes without a recognized prefix', () => { | ||
| checkMentionAttributes({ foo: 'bar' }); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy.mock.calls[0][0]).toContain('foo'); | ||
| }); | ||
|
|
||
| it('does nothing when given no attributes', () => { | ||
| checkMentionAttributes(); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('sanitizeHtmlMention', () => { | ||
| it('keeps <mention> tags with text/indicator/data-* attributes', () => { | ||
| const out = sanitizeHtml( | ||
| '<mention text="Joe" indicator="@" data-user-id="42">@Joe</mention>' | ||
| ); | ||
| expect(out).toContain('text="Joe"'); | ||
| expect(out).toContain('indicator="@"'); | ||
| expect(out).toContain('data-user-id="42"'); | ||
| }); | ||
|
|
||
| it('strips <mention> event handlers', () => { | ||
| expect( | ||
| sanitizeHtml('<mention onclick="alert(1)">x</mention>') | ||
| ).not.toContain('onclick'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('sanitizeLinkAttributes', () => { | ||
| it('strips javascript: URLs from links', () => { | ||
| const out = sanitizeHtml('<a href="javascript:alert(1)">x</a>'); | ||
| // eslint-disable-next-line no-script-url | ||
| expect(out).not.toContain('javascript:'); | ||
| }); | ||
|
|
||
| it('strips unknown protocol URLs from links', () => { | ||
| const out = sanitizeHtml('<a href="custom://link">x</a>'); | ||
| expect(out).not.toContain('custom'); | ||
| }); | ||
| }); |
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
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 |
|---|---|---|
| @@ -1,8 +1,62 @@ | ||
| import DOMPurify from 'dompurify'; | ||
|
|
||
| const MENTION_ATTRS = ['text', 'indicator']; | ||
|
|
||
| // Attributes DOMPurify keeps by default and are commonly used, so we don't emit an unnecessary warning | ||
| const COMMONLY_ALLOWED_ATTRS = ['id', 'class', 'style']; | ||
|
|
||
| export function sanitizeHtml(html: string) { | ||
| return DOMPurify.sanitize(html, { | ||
| ADD_TAGS: ['mention', 'codeblock'], | ||
| ADD_ATTR: ['text', 'indicator'], | ||
| ADD_ATTR: MENTION_ATTRS, | ||
| }); | ||
| } | ||
|
|
||
| export function sanitizeMentionAttributes( | ||
| attributes?: Record<string, string> | ||
| ): Record<string, string> { | ||
| if (!attributes) return {}; | ||
|
|
||
| const el = document.createElement('mention'); | ||
| for (const [name, value] of Object.entries(attributes)) { | ||
| try { | ||
| el.setAttribute(name, value); | ||
| } catch { | ||
| // Ignore invalid attribute names. | ||
| } | ||
| } | ||
|
|
||
| const cleaned = new DOMParser() | ||
| .parseFromString(sanitizeHtml(el.outerHTML), 'text/html') | ||
| .querySelector('mention'); | ||
|
|
||
| const out: Record<string, string> = {}; | ||
| if (!cleaned) return out; | ||
|
|
||
| for (const attr of Array.from(cleaned.attributes)) { | ||
| if (MENTION_ATTRS.includes(attr.name.toLowerCase())) continue; | ||
| out[attr.name] = attr.value; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| // Runtime warning: custom attributes without a "data-" prefix may be | ||
| // removed by sanitization. This is a heuristic (it does not run DOMPurify). | ||
| export function checkMentionAttributes(attributes?: Record<string, string>) { | ||
| if (!attributes) return; | ||
|
|
||
| Object.keys(attributes).forEach((attrName) => { | ||
| const lower = attrName.toLowerCase(); | ||
| if ( | ||
| lower.startsWith('data-') || | ||
| MENTION_ATTRS.includes(lower) || | ||
| COMMONLY_ALLOWED_ATTRS.includes(lower) | ||
| ) { | ||
| return; | ||
| } | ||
| console.warn( | ||
| `[EnrichedMention] Attribute "${attrName}" on the <mention> tag may be removed during sanitization. ` + | ||
| `Consider using the "data-" prefix for custom data attributes.` | ||
| ); | ||
| }); | ||
| } |
Oops, something went wrong.
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.