|
| 1 | +import wrapRegExp from "../../helpers/wrapRegExp.js"; |
| 2 | + |
| 3 | +describe("wrapRegExp", () => { |
| 4 | + describe("should handle maliciously crafted substitutions", () => { |
| 5 | + it("$<$<...$<group", () => { |
| 6 | + const pattern = "(foo)"; |
| 7 | + const groups = { group: 1 }; |
| 8 | + const myRegExp = wrapRegExp(pattern, groups); |
| 9 | + const targetStr = "foofoo"; |
| 10 | + const replacement = "$<".repeat(1e5) + "group"; |
| 11 | + const startTime = Date.now(); |
| 12 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 13 | + // This test will fail if 2000ms is passed |
| 14 | + const timeTaken = Date.now() - startTime; |
| 15 | + expect(timeTaken).toBeLessThan(2000); |
| 16 | + expect(result).toBe(replacement + "foo"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("$<$<...$<group>", () => { |
| 20 | + const pattern = "(foo)"; |
| 21 | + const groups = { group: 1 }; |
| 22 | + const myRegExp = wrapRegExp(pattern, groups); |
| 23 | + const targetStr = "foofoo"; |
| 24 | + const replacement = "$<".repeat(1e5) + "group>"; |
| 25 | + const startTime = Date.now(); |
| 26 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 27 | + // This test will fail if 2000ms is passed |
| 28 | + const timeTaken = Date.now() - startTime; |
| 29 | + expect(timeTaken).toBeLessThan(2000); |
| 30 | + expect(result).toBe("foo"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("$<g$<g...$<group", () => { |
| 34 | + const pattern = "(foo)"; |
| 35 | + const groups = { group: 1 }; |
| 36 | + const myRegExp = wrapRegExp(pattern, groups); |
| 37 | + const targetStr = "foofoo"; |
| 38 | + const replacement = "$<g".repeat(1e5) + "group"; |
| 39 | + const startTime = Date.now(); |
| 40 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 41 | + // This test will fail if 2000ms is passed |
| 42 | + const timeTaken = Date.now() - startTime; |
| 43 | + expect(timeTaken).toBeLessThan(2000); |
| 44 | + expect(result).toBe(replacement + "foo"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("$<g$<g...$<group>", () => { |
| 48 | + const pattern = "(foo)"; |
| 49 | + const groups = { group: 1 }; |
| 50 | + const myRegExp = wrapRegExp(pattern, groups); |
| 51 | + const targetStr = "foofoo"; |
| 52 | + const replacement = "$<g".repeat(1e5) + "group>"; |
| 53 | + const startTime = Date.now(); |
| 54 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 55 | + // This test will fail when 2000ms is passed |
| 56 | + const timeTaken = Date.now() - startTime; |
| 57 | + expect(timeTaken).toBeLessThan(2000); |
| 58 | + expect(result).toBe("foo"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("$<_$>", () => { |
| 62 | + const pattern = "(foo)"; |
| 63 | + const groups = { _$: 1 }; |
| 64 | + const myRegExp = wrapRegExp(pattern, groups); |
| 65 | + const targetStr = "foobar"; |
| 66 | + const replacement = "$<_$>$<_$>"; |
| 67 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 68 | + expect(result).toBe("foofoobar"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("$<hasOwnProperty>", () => { |
| 72 | + const pattern = "(foo)"; |
| 73 | + const groups = { hasOwnProperty: 1 }; |
| 74 | + const myRegExp = wrapRegExp(pattern, groups); |
| 75 | + const targetStr = "foobar"; |
| 76 | + const replacement = "$<hasOwnProperty>"; |
| 77 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 78 | + expect(result).toBe("foobar"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("$<__proto__>", () => { |
| 82 | + const pattern = "(foo)"; |
| 83 | + const groups = { ["__proto__"]: 1 }; |
| 84 | + const myRegExp = wrapRegExp(pattern, groups); |
| 85 | + const targetStr = "foobar"; |
| 86 | + const replacement = "$<__proto__>"; |
| 87 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 88 | + expect(result).toBe("foobar"); |
| 89 | + }); |
| 90 | + }); |
| 91 | + describe("substitutions", () => { |
| 92 | + it("unknown group", () => { |
| 93 | + const pattern = "(foo)"; |
| 94 | + const groups = { group: 1 }; |
| 95 | + const myRegExp = wrapRegExp(pattern, groups); |
| 96 | + const targetStr = "foobar"; |
| 97 | + const replacement = "$<UNKNOWN>"; |
| 98 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 99 | + expect(result).toBe("bar"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("$<hasOwnProperty> - unknown group", () => { |
| 103 | + const pattern = "(foo)"; |
| 104 | + const groups = { group: 1 }; |
| 105 | + const myRegExp = wrapRegExp(pattern, groups); |
| 106 | + const targetStr = "foobar"; |
| 107 | + const replacement = "$<hasOwnProperty>"; |
| 108 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 109 | + expect(result).toBe("bar"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("$<__proto__> -- unknown group", () => { |
| 113 | + const pattern = "(foo)"; |
| 114 | + const groups = { group: 1 }; |
| 115 | + const myRegExp = wrapRegExp(pattern, groups); |
| 116 | + const targetStr = "foobar"; |
| 117 | + const replacement = "$<__proto__>"; |
| 118 | + const result = myRegExp[Symbol.replace](targetStr, replacement); |
| 119 | + expect(result).toBe("bar"); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments