Skip to content

security: replace new Function() eval with direct ESM import#92

Open
xiaolai wants to merge 1 commit intopbakaus:mainfrom
xiaolai:fix/replace-new-function-with-import
Open

security: replace new Function() eval with direct ESM import#92
xiaolai wants to merge 1 commit intopbakaus:mainfrom
xiaolai:fix/replace-new-function-with-import

Conversation

@xiaolai
Copy link
Copy Markdown

@xiaolai xiaolai commented Apr 10, 2026

Summary

  • Replaces 3 instances of new Function() code evaluation with direct ESM imports of the already-exported ANTIPATTERNS array
  • Eliminates an arbitrary code execution surface at build time and dev-server startup
  • Net reduction of 21 lines; no new dependencies

Motivation

Three build scripts (scripts/build.js, scripts/build-extension.js, scripts/lib/sub-pages-data.js) extract the ANTIPATTERNS array from src/detect-antipatterns.mjs via regex, then evaluate it with new Function(). Since ANTIPATTERNS is already exported from the module (line 3577), this indirection is unnecessary.

The new Function() pattern means any commit that modifies the matched regex region in detect-antipatterns.mjs gets arbitrary code execution at:

  • bun run build (build.js + build-extension.js)
  • bun run dev (sub-pages-data.js runs at server boot)

With a direct ESM import, a malformed ANTIPATTERNS entry produces a parse error at import time rather than silent code execution.

Changes

File Before After
scripts/build.js regex + new Function() in validateAntipatternRules() import { ANTIPATTERNS } from source
scripts/build-extension.js regex + new Function() to generate antipatterns.json import { ANTIPATTERNS } from source
scripts/lib/sub-pages-data.js readAntipatternRules() with regex + new Function() readAntipatternRules() returns imported ANTIPATTERNS directly

Test plan

  • bun run build completes without errors
  • bun run dev starts without errors
  • node scripts/build-extension.js generates extension/detector/antipatterns.json with correct rule count
  • Built output matches pre-change output (diff dist/ before and after)

ANTIPATTERNS is already exported from src/detect-antipatterns.mjs, so the
three build scripts that previously extracted it via regex and evaluated it
with new Function() can simply import it directly.

This eliminates an arbitrary code execution surface at build time and
dev-server startup: a tampered detect-antipatterns.mjs could previously
execute arbitrary code silently during the regex-to-eval path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant