Skip to content

Commit a39ca11

Browse files
authored
fix: remove newline entities (#46)
* fix: remove newline entities * strip out tabs and newlines * not decoding, so just replace * update changelog
1 parent ab8d43d commit a39ca11

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## unreleased
4+
- Fix issue where urls in the form `https://example.com

/something` were not properly sanitized
5+
36
## 6.0.1
47

58
- Fix issue where urls in the form `javascript:alert('xss');` were not properly sanitized

src/__tests__/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ describe("sanitizeUrl", () => {
9292
);
9393
});
9494

95+
it("removes newline entities from urls", () => {
96+
expect(sanitizeUrl("https://example.com

/something")).toBe(
97+
"https://example.com/something"
98+
);
99+
});
100+
95101
it("decodes html entities", () => {
96102
// all these decode to javascript:alert('xss');
97103
const attackVectors = [

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const invalidProtocolRegex = /^([^\w]*)(javascript|data|vbscript)/im;
22
const htmlEntitiesRegex = /&#(\w+)(^\w|;)?/g;
3-
const htmlTabEntityRegex = /&tab;/gi;
3+
const htmlCtrlEntityRegex = /&(newline|tab);/gi;
44
const ctrlCharactersRegex =
55
/[\u0000-\u001F\u007F-\u009F\u2000-\u200D\uFEFF]/gim;
66
const urlSchemeRegex = /^.+(:|:)/gim;
@@ -12,14 +12,14 @@ function isRelativeUrlWithoutProtocol(url: string): boolean {
1212

1313
// adapted from https://stackoverflow.com/a/29824550/2601552
1414
function decodeHtmlCharacters(str: string) {
15-
str = str.replace(htmlTabEntityRegex, "	");
1615
return str.replace(htmlEntitiesRegex, (match, dec) => {
1716
return String.fromCharCode(dec);
1817
});
1918
}
2019

2120
export function sanitizeUrl(url?: string): string {
2221
const sanitizedUrl = decodeHtmlCharacters(url || "")
22+
.replace(htmlCtrlEntityRegex, "")
2323
.replace(ctrlCharactersRegex, "")
2424
.trim();
2525

0 commit comments

Comments
 (0)