Skip to content

Commit 4a5cf95

Browse files
authored
feat: escape = (#24)
* escape `=` * update README
1 parent 140202a commit 4a5cf95

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const username = '<img src="https://example.com/pwned.png">';
5555
const greeting = html`<h1>Hello, ${username}</h1>`;
5656

5757
console.log(greeting);
58-
// Output: <h1>Hello, &#60;img src=&#34;https://example.com/pwned.png&#34;&#62;</h1>
58+
// Output: <h1>Hello, &#60;img src&#61;&#34;https://example.com/pwned.png&#34;&#62;</h1>
5959
```
6060

6161
To bypass escaping:

src/html.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const escapeRegExp = /["&'<>`]/;
1+
const escapeRegExp = /["&'<=>`]/;
22

33
const escapeFunction = (string) => {
44
let escaped = "";
@@ -22,6 +22,10 @@ const escapeFunction = (string) => {
2222
escaped += string.slice(start, end) + "&#60;";
2323
start = end + 1;
2424
continue;
25+
case 61: // =
26+
escaped += string.slice(start, end) + "&#61;";
27+
start = end + 1;
28+
continue;
2529
case 62: // >
2630
escaped += string.slice(start, end) + "&#62;";
2731
start = end + 1;

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ test("renders unsafe content /2", () => {
7070
);
7171
});
7272

73+
test("renders unsafe content /3", () => {
74+
// prettier-ignore
75+
assert.strictEqual(
76+
html`<img src="https://picsum.photos/200/300" alt=${"altText onload=alert(String.fromCharCode(112,119,110,101,100))"} />`,
77+
`<img src="https://picsum.photos/200/300" alt=altText onload&#61;alert(String.fromCharCode(112,119,110,101,100)) />`,
78+
);
79+
});
80+
7381
test("renders arrays", () => {
7482
assert.strictEqual(
7583
html`<p>${[descriptionSafe, descriptionUnsafe]}</p>`,

0 commit comments

Comments
 (0)