Skip to content

fix(javascript): recover Electron APIs through default imports and optional calls - #1673

Merged
morluto merged 2 commits into
mainfrom
fix/electron-default-import-optional-calls
Oct 11, 2026
Merged

morluto merged 2 commits into
mainfrom
fix/electron-default-import-optional-calls

Conversation

@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1656 (fixed by 2612e47). Three Electron API shapes on current main still report 0 findings with no limitation that names the miss:

Shape (otherwise identical to the control) main this PR
import electron from "electron"; electron.ipcMain.handle("ch:ping", …) main_handlers: 0 1
import electron from "electron"; const { ipcMain } = electron; ipcMain.handle(…) 0 1
import electron from "electron"; new electron.BrowserWindow(…) and electron.contextBridge.exposeInMainWorld(…) browser_windows: 0, context_bridge_apis: 0 1, 1
ipcMain?.handle(…), contextBridge?.exposeInMainWorld(…), utilityProcess?.fork(…) 0 1
if (!event.senderFrame?.url.startsWith("file://")) … inside a handler sender_validation_observations: 0 1

The default import is the pattern used by libraries such as electron-util and electron-store.

Causes

  • bindImports gives a default import the origin path ["default"], so the classifier produced ["default", "ipcMain"], which no Electron matcher accepts. Electron's CommonJS exports define no default member, so the segment can only mean the module object (the ES module default import of a CommonJS module is module.exports). The new electronExportPath drops the leading default only when the classifier returns an Electron path. The semantic IR keeps its exact ["default"] origin. electron.default.ipcMain still matches nothing.
  • classifyParsedJavaScriptElectronBindingsSteps already records OptionalCallExpression roots, and electronCalleePath walks OptionalMemberExpression. But the IPC, context-bridge, utility-process and sender-validation visitors accepted only CallExpression. Babel parses any call in a ?. chain, including event.senderFrame?.url.startsWith(x), as an OptionalCallExpression. So the existing optional-member branch in inspectValidationCall could never run for that chain.

Not changed: data-flow shapes the identity limitation already calls unresolved (let ipc; ipc = ipcMain, passing ipcMain as an argument, { ipc: ipcMain }.ipc, ipcMain || null, .bind). Bundler interop wrappers (__importDefault(require("electron")).default) remain unrecognized, as before.

Verification

  • New src/domain/javascript/electronStaticAnalysisIdentity.test.ts (2 tests). Both fail with the source changes stashed and pass with them.
  • npm run test:focused -- all 43 src/domain/javascript/*.test.ts files plus tests/composition/javascript-applications/electronApplicationAnalysis.test.ts: 808 passed.
  • tests/acceptance/applications/applicationWorkflowCli.test.ts (after build:cached): 31 passed.
  • npm run typecheck, npm run check:fast pass. jscpd stays at 2.489%.
  • Built CLI, analyze-javascript-application <dir> --artifact-format directory, compared main vs branch on the shapes in the table.

No Hopper/Ghidra involvement; no real Electron runtime execution.

🤖 Generated with Claude Code

…tional calls

`import electron from "electron"` bound the module with a leading `default`
export segment, so `electron.ipcMain.handle(...)`, `new
electron.BrowserWindow(...)` and destructuring from that binding matched no
Electron API. Electron's CommonJS exports define no `default` member, so the
segment can only denote the module object; drop it when classifying
Electron bindings.

The binding classifier already recorded optional-call roots, but the IPC,
context-bridge, utility-process and sender-validation visitors accepted only
plain calls. `ipcMain?.handle(...)`, `contextBridge?.exposeInMainWorld(...)`
and the common `event.senderFrame?.url.startsWith(...)` check were dropped
without a limitation.

Refs #1656

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@morluto
morluto merged commit fe574c0 into main Oct 11, 2026
26 checks passed
@morluto
morluto deleted the fix/electron-default-import-optional-calls branch October 11, 2026 02:31
@CassieFox23

Copy link
Copy Markdown
Contributor

TL;DR: I tested this after the merge, on Linux. Default imports and optional calls now work for Electron code. Rewritten to a default import, real Stretchly code gives all 30 handlers back.

This PR merged as fe574c0 (#1673). I tested main at 17f7040 (#1682) against 3a7a307 (#1670), the commit just before the merge.

Results

Check Node 22.23.1 Node 24.18.0 Node 26.11.1
The PR's test file on main (2 tests) 2 pass 2 pass 2 pass

On 3a7a307, both tests fail (Node 24.18.0). In the next table, Before is 3a7a307 and Main is 17f7040, all on Node 24.18.0.

Check Before Main
Default import, electron.ipcMain.handle(...) 0 handlers 1 handler
Default import, then const { ipcMain } = electron 0 handlers 1 handler
new electron.BrowserWindow(...) 0 windows 1 window
ipcMain?.handle(...) and ipcMain?.on(...) 0 handlers 2 handlers
contextBridge?.exposeInMainWorld, utilityProcess?.fork 0 and 0 1 and 1
event.senderFrame?.url.startsWith(...) in a handler 0 sender checks 1 sender check
Stretchly app folder, unchanged 30 handlers, 36 edges, 27 paired same, and the result file is byte for byte equal
Stretchly main.js with a default import (3 forms) 0 handlers 30 handlers, 36 edges, 27 paired
Stretchly main.js with ipcMain?.on( and ?.handle( (3 forms) 0 handlers 30 handlers, 36 edges, 27 paired
Stretchly with one #1664 line added (10 forms) as in my #1664 comment same as 3a7a307
Default import, then electron.ipcMain = {...} 30 handlers kept 0 handlers

The last row follows from the fix. A default import now counts as the Electron namespace, so a write to it drops the handlers, like the import * as write from #1664. My #1664 comment said a default import was not read as a namespace. That is out of date on main.

Still unresolved, as the PR says (0 on both commits): electron.default.ipcMain.handle, the __importDefault(require("electron")).default wrapper, and let ipc; ipc = ipcMain. An ipcMain?.handle on a plain local object gives 0 too.

Real app: Stretchly 1.22.1 (BSD-2-Clause), app/ folder, command line and stdio MCP. I only parsed the rewritten files. I never ran them. The unchanged counts match my #1664 comment.

Not run

  • Real Electron or any GUI. Windows and macOS.
  • The PR's wider checks: the other 43 JavaScript test files, the type check, check:fast and the full suite.
  • Stretchly with its npm packages installed.
  • MCP for the small fixtures. The command line ran them alone.

Thanks, kaoru0822-kitauji, for the fix and for the new tests.

Variant details

Each Stretchly variant keeps every line number. The default-import forms rewrite lines 1 to 5. The optional-call forms change the 30 ipcMain.on( and ipcMain.handle( calls. On main the handler list (operation, channel, line) equals the unchanged app's in all six.

Test setup
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • RAM: 32 GB DDR4-3600
  • Node: 22.23.1, 24.18.0, 26.11.1
  • Main: 17f7040
  • Base: 3a7a307
  • Other: the app is Stretchly 1.22.1 (BSD-2-Clause). I used the source of tag v1.22.1. I never installed or ran it.
Commands
npm ci && npm run build && npm run test:prepare
npm run test:local -- src/domain/javascript/electronStaticAnalysisIdentity.test.ts
node scripts/rea.mjs analyze-javascript-application <dir> --artifact-format directory --json

🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: domain Domain logic, errors, and pure functions bug Something isn't working provider: electron Analysis provider / engine: electron

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants