-
Notifications
You must be signed in to change notification settings - Fork 62
Improve HTML content sanitization and fix linting issues #1821
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
5 commits
Select commit
Hold shift + click to select a range
8a8e34b
fix linting on save
stephenegriffin 090c05c
Stronger sanitization on html content
stephenegriffin 45ccee1
Apply suggestions from code review
stephenegriffin c10bbc2
Fix test cases
stephenegriffin 74aa96b
Potential fix for pull request finding
stephenegriffin 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
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,33 +1,78 @@ | ||
| import { ArchivedRow } from "./ArchivedRow"; | ||
| import { Decoder } from "../2047"; | ||
| import { Strings } from "../Strings"; | ||
|
|
||
| jest.mock("../Strings", () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| Strings: { | ||
| mapHeaderToURL: jest.fn(), | ||
| mapValueToURL: jest.fn() | ||
| } | ||
| })); | ||
| jest.mock("../Strings", () => { | ||
| const actualStrings = jest.requireActual("../Strings") as typeof import("../Strings"); | ||
|
|
||
| return { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| Strings: { | ||
| mapHeaderToURL: jest.fn((headerName: string, text?: string) => actualStrings.Strings.mapHeaderToURL(headerName, text)), | ||
| htmlEncode: jest.fn((value: string) => actualStrings.Strings.htmlEncode(value)) | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| describe("ArchivedRow", () => { | ||
| const header = "testHeader"; | ||
| const header = "Archived-At"; | ||
| const label = "testLabel"; | ||
| let archivedRow: ArchivedRow; | ||
|
|
||
| beforeEach(() => { | ||
| (Strings.mapHeaderToURL as jest.Mock).mockReturnValue("mockedHeaderURL"); | ||
| (Strings.mapValueToURL as jest.Mock).mockReturnValue("mockedValueURL"); | ||
| jest.clearAllMocks(); | ||
| archivedRow = new ArchivedRow(header, label); | ||
| }); | ||
|
|
||
| it("should set url using Strings.mapHeaderToURL", () => { | ||
| expect(Strings.mapHeaderToURL).toHaveBeenCalledWith(header, label); | ||
| expect(archivedRow.url).toBe("mockedHeaderURL"); | ||
| expect(archivedRow.url).toBe("<a href = 'https://tools.ietf.org/html/rfc5064' target = '_blank'>testLabel</a>"); | ||
| }); | ||
|
stephenegriffin marked this conversation as resolved.
stephenegriffin marked this conversation as resolved.
stephenegriffin marked this conversation as resolved.
|
||
|
|
||
| it("should return html href for bracketed http(s) links", () => { | ||
| archivedRow.value = "<https://example.test/path>"; | ||
| const valueUrl = archivedRow.valueUrl; | ||
|
|
||
| expect(valueUrl).toContain("<a"); | ||
| expect(valueUrl).toContain("href="); | ||
| expect(valueUrl).toContain("https://example.test/path"); | ||
| expect(valueUrl).toContain("target="); | ||
| }); | ||
|
stephenegriffin marked this conversation as resolved.
|
||
|
|
||
| it("should html encode non-bracketed values", () => { | ||
| archivedRow.value = "https://example.test/path"; | ||
| const valueUrl = archivedRow.valueUrl; | ||
| expect(Strings.htmlEncode).toHaveBeenCalledWith(archivedRow.value); | ||
| expect(valueUrl).toBe("https://example.test/path"); | ||
| }); | ||
|
|
||
| it("should return valueUrl using Strings.mapValueToURL", () => { | ||
| archivedRow["valueInternal"] = "internalValue"; | ||
| expect(archivedRow.valueUrl).toBe("mockedValueURL"); | ||
| expect(Strings.mapValueToURL).toHaveBeenCalledWith("internalValue"); | ||
| it("should html encode raw img payload instead of rendering executable HTML", () => { | ||
| archivedRow.value = "<img src=x onerror=alert('XSS')>"; | ||
| const valueUrl = archivedRow.valueUrl; | ||
| expect(Strings.htmlEncode).toHaveBeenCalledWith(archivedRow.value); | ||
| expect(valueUrl).toContain("<img"); | ||
| expect(valueUrl).toContain(">"); | ||
| expect(valueUrl).not.toContain("<img"); | ||
| expect(valueUrl).not.toContain("<a href="); | ||
| }); | ||
|
|
||
| it("should encode RFC2047-decoded img payload instead of rendering executable HTML", () => { | ||
| const encodedPayload = "=?UTF-8?B?PGltZyBzcmM9eCBvbmVycm9yPWFsZXJ0KCdYU1MnKT4=?="; | ||
| archivedRow.value = Decoder.clean2047Encoding(encodedPayload); | ||
|
|
||
| expect(archivedRow.valueUrl).toContain("<img"); | ||
| expect(archivedRow.valueUrl).toContain(">"); | ||
| expect(archivedRow.valueUrl).not.toContain("<img"); | ||
| expect(archivedRow.valueUrl).not.toContain("<a href="); | ||
| }); | ||
|
|
||
| it("should render anchor only for strict angle-bracketed http(s) URL", () => { | ||
| const url = "https://example.com/test/list/foo-users@lists.example.com/message/mysubject/"; | ||
| archivedRow.value = `<${url}>`; | ||
| const valueUrl = archivedRow.valueUrl; | ||
|
|
||
| expect(valueUrl).toMatch("<a href=\""); | ||
| expect(valueUrl).toContain(url); | ||
| expect(valueUrl).toContain("target=\"_blank\""); | ||
| }); | ||
| }); | ||
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,10 +1,30 @@ | ||
| import { Strings } from "../Strings"; | ||
| import { SummaryRow } from "./SummaryRow"; | ||
|
|
||
| // Handle the "Archived-At" header per https://tools.ietf.org/html/rfc5064 | ||
| export class ArchivedRow extends SummaryRow { | ||
| constructor(header: string, label: string) { | ||
| super(header, label); | ||
| this.url = Strings.mapHeaderToURL(header, label); | ||
| } | ||
|
stephenegriffin marked this conversation as resolved.
stephenegriffin marked this conversation as resolved.
stephenegriffin marked this conversation as resolved.
|
||
| override get valueUrl(): string { return Strings.mapValueToURL(this.valueInternal); } | ||
| } | ||
| // Return the URL for the archived message, or the encoded value if it's not a valid URL. | ||
| override get valueUrl(): string { | ||
| const match = this.valueInternal.match(/^\s*<([^>]+)>\s*$/); | ||
| if (match?.[1]) { | ||
| try { | ||
| const url = new URL(match[1]); | ||
| if (url.protocol === "http:" || url.protocol === "https:") { | ||
| const a = document.createElement("a"); | ||
| a.href = url.href; | ||
| a.target = "_blank"; | ||
| a.rel = "noopener noreferrer"; | ||
| a.textContent = url.href; | ||
| return a.outerHTML; | ||
| } | ||
| } catch { | ||
| // Fall through to encoded output. | ||
| } | ||
| } | ||
|
|
||
| return Strings.htmlEncode(this.valueInternal); | ||
| } | ||
|
stephenegriffin marked this conversation as resolved.
|
||
| } | ||
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
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.