Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344import { describe, expect, it } from "vitest";import { findForbiddenCode } from "../../tools/extensionCheck";
const rules = (src: string) => findForbiddenCode(src, "x.js").map((f) => f.rule);
describe("findForbiddenCode", () => { it("accepts ordinary code", () => { expect(rules("const a = [1,2].map((x) => x * 2); fetch('/x');")).toEqual([]); }); it("finds eval", () => { expect(rules("const r = eval('1+1');")).toEqual(["eval"]); expect(rules("window.eval ( code )")).toEqual(["eval"]); }); it("does not flag words that merely contain eval", () => { expect(rules("const retrieval = 1; evaluate(2); medieval(3);")).toEqual([]); }); it("finds new Function and Function()", () => { expect(rules("new Function('return 1')")).toEqual(["function-constructor"]); expect(rules("Function('return 1')()")).toEqual(["function-constructor"]); }); it("finds remote code loading", () => { expect(rules("importScripts('https://cdn.example/x.js')")).toEqual(["remote-code"]); expect(rules("await import('http://a.example/x.js')")).toEqual(["remote-code"]); expect(rules('<script src="https://cdn.example/x.js"></script>')).toEqual(["remote-code"]); }); it("allows importing local files", () => { expect(rules("importScripts('lib.js'); await import('./x.js')")).toEqual([]); }); it("finds a string-argument setTimeout or setInterval (it evaluates code)", () => { expect(rules("setTimeout('doIt()', 10)")).toEqual(["string-timer"]); expect(rules("setInterval(\"x()\", 1)")).toEqual(["string-timer"]); expect(rules("setTimeout(() => 1, 10)")).toEqual([]); }); it("finds unsafe-eval", () => { expect(rules("script-src 'unsafe-eval'")).toEqual(["unsafe-eval"]); }); it("reports the file and 1-based line", () => { expect(findForbiddenCode("a\n\neval(1)", "bg.js")).toEqual([{ file: "bg.js", line: 3, rule: "eval" }]); }); it("reports each problem on its own", () => { expect(rules("eval(1); new Function('x')")).toEqual(["eval", "function-constructor"]); });});