Skip to content

fix(javascript): correct lexical and dynamic scope analysis - #538

Merged
morluto merged 2 commits into
mainfrom
cleanup/semantic-scope-and-shadowing
Oct 5, 2026
Merged

morluto merged 2 commits into
mainfrom
cleanup/semantic-scope-and-shadowing

Conversation

@morluto

@morluto morluto commented Oct 5, 2026 •

Copy link
Copy Markdown
Owner

Static JavaScript analysis could invent CommonJS exports from shadowed names, resolve switch discriminants against case-local declarations, and fail graph construction when encountering with or direct eval.

This change checks lexical and dynamic environments before recognizing CommonJS globals, isolates static-block declarations, shares one lexical scope across switch cases while evaluating the discriminant in its outer scope, and exposes dynamic-scope unknowns through the graph schema. with blocks outer-name resolution while retaining lexical locals; shadowed, optional, and indirect eval calls do not receive a direct-eval frontier. Computed assignments on unrelated or shadowed objects no longer invent wildcard exports.

Rebased onto current main, preserving the existing destructuring-write and compound-assignment read/write fixes. This is static analysis; it does not restore runtime replay or execute analyzed source.

Validation:

  • npm run check:pr: passed; 349 test files, 1,971 tests passed and one skipped.
  • Final semantic regression run: 92 tests across 12 files passed, including public graph construction.
  • npm run check:fast: passed.
  • npm run docs:generate: passed; generated catalog and evidence metadata refreshed.
  • No real Hopper/Ghidra verification performed; these changes affect static JavaScript analysis.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-05T08:33:44.195040Z c46e5f1 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c46e5f1ac0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

: ["reads-property", "writes-property"],
family,
relation_kinds: [...relationKinds],
reason: frontier.kind,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Accept dynamic-scope in the graph unknown schema

For any analyzed file containing with or direct eval, this passes "dynamic-scope" to createJavaScriptSemanticGraphUnknown, but JAVASCRIPT_SEMANTIC_UNKNOWN_REASONS in javascriptSemanticGraphSchemas.ts does not include that value. Its Zod parse therefore throws during buildJavaScriptSemanticGraph instead of returning the newly added unknown; add the reason to the schema or map it to an existing supported reason.

Useful? React with 👍 / 👎.

Comment on lines +381 to +382
const reason = t.isWithStatement(node)
? "`with` introduces a runtime binding environment; names inside are not statically resolvable."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not resolve names through a with environment

When a with body references an outer binding, such as let x; with (obj) { use(x); }, recording a frontier alone leaves the scope state unchanged, so collectSemanticReferences still marks x as resolved and the graph emits a resolved read even though obj.x may intercept it at runtime. The with body must block or downgrade lexical resolution rather than merely adding an unrelated unknown frontier.

AGENTS.md reference: AGENTS.md:L9-L9

Useful? React with 👍 / 👎.

Comment thread src/domain/javascriptSemanticCalls.ts Outdated
Comment on lines +383 to +385
: t.isCallExpression(node) &&
t.isIdentifier(node.callee) &&
node.callee.name === "eval"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Gate eval frontiers on an unshadowed global

For valid sloppy-mode code such as function run(eval) { eval("text"); }, the callee is a parameter and cannot introduce bindings through JavaScript's direct-eval semantics, yet this name-only check reports a dynamic-scope unknown. Resolve the callee binding and honor blocked resolution, as the new CommonJS and existing require checks do, so ordinary functions named eval are not misclassified.

AGENTS.md reference: AGENTS.md:L9-L9

Useful? React with 👍 / 👎.

Comment on lines +274 to +276
// A switch body is one lexical scope shared by all its cases, so `let` in
// two cases must resolve to one binding rather than collide or leak.
if (t.isSwitchStatement(node)) return "block";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the switch discriminant outside the case scope

Creating the block scope on the whole SwitchStatement also assigns the discriminant AST to that scope. In let mode = 1; switch (mode) { case 1: let mode = 2; }, resolution therefore binds the discriminant to the case-local mode, although JavaScript evaluates the discriminant before establishing the case block's lexical environment and uses the outer binding. Scope only the case-block descendants while leaving the discriminant in the enclosing scope.

Useful? React with 👍 / 👎.

@morluto
morluto force-pushed the cleanup/semantic-scope-and-shadowing branch from c46e5f1 to ec91661 Compare October 5, 2026 15:42
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@morluto morluto changed the title fix(javascript): gate CommonJS exports on unshadowed globals and record dynamic scope fix(javascript): correct lexical and dynamic scope analysis Oct 5, 2026
@morluto
morluto merged commit 6fa6f1f into main Oct 5, 2026
14 checks passed
@morluto
morluto deleted the cleanup/semantic-scope-and-shadowing branch October 5, 2026 15:42
darsrc pushed a commit to darsrc/rea that referenced this pull request Oct 10, 2026
)

* fix(javascript): gate CommonJS exports on unshadowed globals and record dynamic scope

* fix(javascript): correct dynamic and switch scope boundaries
@morluto morluto added bug Something isn't working area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant