Skip to content

Commit 25cd549

Browse files
committed
[XSS] Faster invalidCharsRx initialization on Gecko 78 and above.
1 parent d6b6276 commit 25cd549

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

src/test/XSS_test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
if (UA.isMozilla) {
2222
let y = async (url, originUrl = '') => await XSS.test({originUrl, url, method: "GET"});
2323
let n = async (...args) => !await y(...args);
24-
Promise.all([
24+
let xssTest = Promise.all([
2525
() => y("https://noscript.net/<script"),
2626
() => n("https://noscript.net/<script", "https://noscript.net/"),
2727
() => y("https://vulnerabledoma.in/char_test?body=%80%3Cscript%3Ealert(1)%3C/script%3E"),
@@ -32,25 +32,25 @@ if (UA.isMozilla) {
3232
() => y("https://vulnerabledoma.in/xss_link?url=javascript%26colo%00n%3Balert%u00281%29"),
3333
() => y("https://vulnerabledoma.in/xss_link?url=javascript:\\u{%0A6e}ame"),
3434
].map(t => Test.run(t))
35-
).then(() => Test.report());
35+
);
3636

3737
let invalidCharsTest = async () => {
3838

3939
await include("xss/InjectionChecker.js");
4040
let IC = await XSS.InjectionChecker;
4141
let rx = new IC().invalidCharsRx;
42-
42+
console.log("Testing invalidCharsRx", rx);
4343
let x = n => '\\u' + ("0000" + n.toString(16)).slice(-4);
4444
function check(ch) {
45-
eval(`{let _${ch}_}`);
45+
Function(`let _${ch}_`);
4646
}
4747
let cur = 0x7e;
4848
let fail = false;
49-
while (cur++ < 0xffff) {
49+
while (cur++ < 0xffff && !fail) {
5050
let ch = String.fromCharCode(cur);
5151
try {
5252
check(ch);
53-
if (tx.test(ch)) {
53+
if (rx.test(ch)) {
5454
console.error(x(cur) + " should not test invalid!");
5555
fail = true;
5656
}
@@ -64,6 +64,10 @@ if (UA.isMozilla) {
6464
}
6565
return !fail;
6666
};
67-
68-
Test.run(invalidCharsTest, "InjectionChecker.invalidCharsRx").then(Test.report());
67+
(async () => {
68+
await xssTest;
69+
Test.report();
70+
await Test.run(invalidCharsTest, "InjectionChecker.invalidCharsRx");
71+
Test.report();
72+
})();
6973
}

src/xss/InjectionChecker.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,15 @@ XSS.InjectionChecker = (async () => {
526526
},
527527

528528
get invalidCharsRx() {
529-
let value = new RegExp("^[^\"'`/<>]*[" + this._createInvalidRanges() + "]");
529+
let preamble = "^[^\"'`/<>]*";
530+
let value;
531+
try {
532+
// see https://mathiasbynens.be/notes/javascript-identifiers-es6#acceptable-unicode-symbols
533+
value = new RegExp(preamble + "[^$_\\p{ID_Start}\\p{ID_Continue}\\u200c\\u200d\\u2028\\u2029]", "u");
534+
} catch (e) {
535+
// Unicode entities are not supported in Gecko <= 77
536+
value = new RegExp(preamble + `[${this._createInvalidRanges()}]`, "u");
537+
}
530538
Object.defineProperty(Object.getPrototypeOf(this), 'invalidCharsRx', {value});
531539
return value;
532540
},

0 commit comments

Comments
 (0)