-
-
Notifications
You must be signed in to change notification settings - Fork 26
feat(options): add CSP support with trustedTypePolicy
#1439
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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,67 @@ | ||
| import { getHTMLForInnerHTML } from '../../src/client/domparser'; | ||
|
|
||
| describe('getHTMLForInnerHTML', () => { | ||
| it('returns the html string as-is when no trusted type policy is provided', () => { | ||
| const html = '<div>test</div>'; | ||
| const result = getHTMLForInnerHTML(html); | ||
|
|
||
| expect(result).toBe(html); | ||
| }); | ||
|
|
||
| it('returns the html string when trustedTypePolicy is undefined', () => { | ||
| const html = '<p>Hello World</p>'; | ||
| const result = getHTMLForInnerHTML(html, undefined); | ||
|
|
||
| expect(result).toBe(html); | ||
| }); | ||
|
|
||
| it('calls trustedTypePolicy.createHTML with the html string when policy is provided', () => { | ||
| const html = '<span>content</span>'; | ||
| const trustedTypePolicy = { | ||
| createHTML: vi.fn((input: string) => input), | ||
| }; | ||
|
|
||
| const result = getHTMLForInnerHTML(html, trustedTypePolicy); | ||
|
|
||
| expect(trustedTypePolicy.createHTML).toHaveBeenCalledOnce(); | ||
| expect(trustedTypePolicy.createHTML).toHaveBeenCalledWith(html); | ||
| expect(result).toBe(html); | ||
| }); | ||
|
|
||
| it('returns the result of trustedTypePolicy.createHTML when policy is provided', () => { | ||
| const html = '<div>test</div>'; | ||
| const trustedHtml = 'TRUSTED_HTML_VALUE'; | ||
| const trustedTypePolicy = { | ||
| createHTML: vi.fn(() => trustedHtml), | ||
| }; | ||
|
|
||
| const result = getHTMLForInnerHTML(html, trustedTypePolicy); | ||
|
|
||
| expect(result).toBe(trustedHtml); | ||
| }); | ||
|
|
||
| it('handles empty html string', () => { | ||
| const html = ''; | ||
| const result = getHTMLForInnerHTML(html); | ||
|
|
||
| expect(result).toBe(''); | ||
| }); | ||
|
|
||
| it('handles html string with special characters', () => { | ||
| const html = '<div>Test & "quotes" \'apostrophe\'</div>'; | ||
| const result = getHTMLForInnerHTML(html); | ||
|
|
||
| expect(result).toBe(html); | ||
| }); | ||
|
|
||
| it('calls trustedTypePolicy with complex html', () => { | ||
| const html = '<div><p>Nested <span>content</span></p></div>'; | ||
| const trustedTypePolicy = { | ||
| createHTML: vi.fn((input: string) => input), | ||
| }; | ||
|
|
||
| getHTMLForInnerHTML(html, trustedTypePolicy); | ||
|
|
||
| expect(trustedTypePolicy.createHTML).toHaveBeenCalledWith(html); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| import type { DomHandlerOptions } from 'domhandler'; | ||
| import type { Comment, Element, ProcessingInstruction, Text } from 'domhandler'; | ||
| import type { ParserOptions } from 'htmlparser2'; | ||
|
|
||
| export type { Comment, Element, ProcessingInstruction, Text }; | ||
|
|
||
| export type DOMNode = Comment | Element | ProcessingInstruction | Text; | ||
|
|
||
| export interface TrustedTypePolicyLike { | ||
| createHTML(input: string): { toString(): string }; | ||
| } | ||
|
|
||
| export type HTMLDOMParserOptions = ParserOptions & | ||
| DomHandlerOptions & { | ||
| trustedTypePolicy?: TrustedTypePolicyLike; | ||
| }; |
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.