Skip to content

Commit

Permalink
Ensure we escape sequences of different entities
Browse files Browse the repository at this point in the history
In a naive, multi-pass entity replacement (eg, replace all `&` with
`&amp;`, replace all `<` with `&lt;`) the replacement order is
important. (You must replace `&` with `&amp;` first, lest you replace
`<` with `&lt;` then replace `&lt;` with `&amp;lt;`.)

The `escapeHTML` function is a single-pass replacement of each entity at
a time, so is not vulnerable to such a failure mode, but add a test to
avoid regressions.
  • Loading branch information
ethomson committed Dec 12, 2023
1 parent 696de5a commit 2312788
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe("dashboard", async () => {
status: TestStatus.Fail,
name: "another name escaped 'properly'", // single quotes require escaping
description: "another description escaped & properly", // ampersand requires escaping
},
{
status: TestStatus.Fail,
name: "entities ' are & escaped < in > proper & order",
description: "order is important in a multi-pass replacement",
}
]
}
Expand All @@ -29,6 +34,7 @@ describe("dashboard", async () => {
expect(actual).contains("description escaped &quot;properly&quot;")
expect(actual).contains("another name escaped &apos;properly&apos;")
expect(actual).contains("another description escaped &amp; properly")
expect(actual).contains("entities &apos; are &amp; escaped &lt; in &gt; proper &amp; order")
})

it("uses <no name> for test cases without name", async () => {
Expand Down

0 comments on commit 2312788

Please sign in to comment.