-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: escape HTML entities in localization arguments
cherry-picked from V15
- Loading branch information
1 parent
586bde9
commit 94f6b03
Showing
14 changed files
with
109 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 @@ | ||
export const UMB_STORAGE_TOKEN_RESPONSE_NAME = 'umb:userAuthTokenResponse'; |
This file contains 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 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 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 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
16 changes: 16 additions & 0 deletions
16
src/packages/core/utils/sanitize/escape-html.function.test.ts
This file contains 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,16 @@ | ||
import { expect } from '@open-wc/testing'; | ||
import { escapeHTML } from './escape-html.function.js'; | ||
|
||
describe('escapeHtml', () => { | ||
it('should escape html', () => { | ||
expect(escapeHTML('<script>alert("XSS")</script>')).to.equal('<script>alert("XSS")</script>'); | ||
}); | ||
|
||
it('should escape html with single quotes', () => { | ||
expect(escapeHTML("<script>alert('XSS')</script>")).to.equal('<script>alert('XSS')</script>'); | ||
}); | ||
|
||
it('should escape html with mixed quotes', () => { | ||
expect(escapeHTML("<script>alert('XSS')</script>")).to.equal('<script>alert('XSS')</script>'); | ||
}); | ||
}); |
This file contains 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,19 @@ | ||
/** | ||
* Escapes HTML entities in a string. | ||
* @example escapeHTML('<script>alert("XSS")</script>'), // "<script>alert("XSS")</script>" | ||
* @param html The HTML string to escape. | ||
* @returns The sanitized HTML string. | ||
*/ | ||
export function escapeHTML(html: unknown): string { | ||
if (typeof html !== 'string' && html instanceof String === false) { | ||
return html as string; | ||
} | ||
|
||
return html | ||
.toString() | ||
.replace(/&/g, '&') | ||
.replace(/"/g, '"') | ||
.replace(/'/g, ''') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>'); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/packages/core/utils/sanitize/sanitize-html.function.test.ts
This file contains 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,24 @@ | ||
import { expect } from '@open-wc/testing'; | ||
import { sanitizeHTML } from './sanitize-html.function.js'; | ||
|
||
describe('sanitizeHTML', () => { | ||
it('should allow benign HTML', () => { | ||
expect(sanitizeHTML('<strong>Test</strong>')).to.equal('<strong>Test</strong>'); | ||
}); | ||
|
||
it('should remove potentially harmful content', () => { | ||
expect(sanitizeHTML('<script>alert("XSS")</script>')).to.equal(''); | ||
}); | ||
|
||
it('should remove potentially harmful attributes', () => { | ||
expect(sanitizeHTML('<a href="javascript:alert(\'XSS\')">Test</a>')).to.equal('<a>Test</a>'); | ||
}); | ||
|
||
it('should remove potentially harmful content and attributes', () => { | ||
expect(sanitizeHTML('<a href="javascript:alert(\'XSS\')"><script>alert("XSS")</script></a>')).to.equal('<a></a>'); | ||
}); | ||
|
||
it('should allow benign attributes', () => { | ||
expect(sanitizeHTML('<a href="/test">Test</a>')).to.equal('<a href="/test">Test</a>'); | ||
}); | ||
}); |
This file contains 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