Truly independent web browser
LibJS: Limit the length of subscript chains
Andreas Kling1729b4478ce1
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rsindex e951449c576..0036578a6f2 100644--- a/Libraries/LibJS/Rust/src/parser/base.rs+++ b/Libraries/LibJS/Rust/src/parser/base.rs@@ -346,6 +346,10 @@ pub const DEFAULT_STACK_BUDGET: usize = 4 << 20;/// How deeply statements and expressions may nest.const MAX_RECURSION_DEPTH: u32 = 2000;+/// How many member accesses, calls and tagged templates one chain like `a.b[c](d)` may have. The parser goes through a+/// chain in a loop, so the recursion limit does not apply to it.+pub(crate) const MAX_SUBSCRIPT_CHAIN_LENGTH: u32 = 10000;+pub(crate) const RECURSION_LIMIT_MESSAGE: &str = "Maximum parser recursion depth exceeded";impl<'a, U: Unit> Parser<'a, U> {diff --git a/Libraries/LibJS/Rust/src/parser/expr.rs b/Libraries/LibJS/Rust/src/parser/expr.rsindex b5a720a7df6..8618f1564f6 100644--- a/Libraries/LibJS/Rust/src/parser/expr.rs+++ b/Libraries/LibJS/Rust/src/parser/expr.rs@@ -371,6 +371,7 @@ impl<U: Unit> Parser<'_, U> {&& self.potential_arrow_at == start;let mut optional_chained = false;let mut chain_pos = start;+ let mut chain_length = 0u32;loop {// NB: All of an optional chain is where it starts.if optional_chained && self.marks {@@ -405,6 +406,7 @@ impl<U: Unit> Parser<'_, U> {return result;}base = result;+ chain_length += 1;continue;}let operator_pos = self.tok_start;@@ -485,6 +487,11 @@ impl<U: Unit> Parser<'_, U> {}_ => break,}+ chain_length += 1;+ if chain_length > MAX_SUBSCRIPT_CHAIN_LENGTH {+ self.fail(self.tok_start, RECURSION_LIMIT_MESSAGE);+ return base;+ }}if optional_chained {if self.marks {diff --git a/Tests/LibJS/Runtime/long-operator-chains.js b/Tests/LibJS/Runtime/long-operator-chains.jsindex bae6c8486fa..7211815733c 100644--- a/Tests/LibJS/Runtime/long-operator-chains.js+++ b/Tests/LibJS/Runtime/long-operator-chains.js@@ -33,10 +33,6 @@ describe("chains of operators of any length, at the top level of code", () => {const chains = {"binary operators": ["1" + "+1".repeat(length), length + 1],"logical operators": ["true" + "&&true".repeat(length), true],- "member expressions": ["typeof a" + ".b".repeat(length), "function"],- "computed member expressions": ["typeof a" + "[0]".repeat(length), "function"],- calls: ["typeof a" + "()".repeat(length), "function"],- "tagged templates": ["typeof a" + "``".repeat(length), "function"],"a chain in an operand": ["[0" + "+1".repeat(length) + "][0]", length],"a chain in a class field": ["new (class { x = 1" + "+1".repeat(length) + "; })().x", length + 1],};@@ -49,13 +45,57 @@ describe("chains of operators of any length, at the top level of code", () => {}test("in the targets of a destructuring assignment", () => {- const member = (0, eval)(`${prelude}[a${".b".repeat(length)}.c] = [1]; a.c`);- expect(member).toBe(1);const key = (0, eval)(`${prelude}[a[0${"+1".repeat(length)}]] = [1]; Object.keys(a).length`);expect(key).toBe(3);});});+describe("chains of subscripts up to the limit", () => {+ const limit = 10000;+ const prelude = "var a = function () { return a; }; a.b = a; a[0] = a;";+ const chains = {+ "member expressions": ".b",+ "optional member expressions": "?.b",+ "computed member expressions": "[0]",+ calls: "()",+ "tagged templates": "``",+ };++ for (const [name, link] of Object.entries(chains)) {+ test(name, () => {+ const source = "typeof a" + link.repeat(limit);+ expect((0, eval)(`${prelude}${source}`)).toBe("function");+ expect(new Function(`${prelude}return ${source}`)()).toBe("function");+ expect(() => (0, eval)(`${prelude}${source}${link}`)).toThrowWithMessage(+ SyntaxError,+ "Maximum parser recursion depth exceeded"+ );+ expect(() => new Function(`${prelude}return ${source}${link}`)).toThrowWithMessage(+ SyntaxError,+ "Maximum parser recursion depth exceeded"+ );+ });+ }++ test("a chain that starts with a call of async", () => {+ const source = `${prelude}var async = a; async()${"()".repeat(limit - 1)}`;+ expect(typeof (0, eval)(source)).toBe("function");+ expect(() => (0, eval)(`${source}()`)).toThrowWithMessage(+ SyntaxError,+ "Maximum parser recursion depth exceeded"+ );+ });++ test("in the targets of a destructuring assignment", () => {+ const member = (0, eval)(`${prelude}[a${".b".repeat(limit - 1)}.c] = [1]; a.c`);+ expect(member).toBe(1);+ expect(() => (0, eval)(`${prelude}[a${".b".repeat(limit)}.c] = [1]`)).toThrowWithMessage(+ SyntaxError,+ "Maximum parser recursion depth exceeded"+ );+ });+});+test("nested code that eval gets at the end of the stack does not run out of it", () => {const nested = "(".repeat(1900) + "1" + ")".repeat(1900);function atTheEndOfTheStack() {