Skip to content

refactor(user-transformations): share V1 sandbox API injection and keep no-op log in-isolate - #5675

Closed
sivashanmukh wants to merge 1 commit into
developfrom
fix/ivm-shared-sandbox-apis
Closed

sivashanmukh wants to merge 1 commit into
developfrom
fix/ivm-shared-sandbox-apis

Conversation

@sivashanmukh

Copy link
Copy Markdown
Contributor

What are the changes introduced in this PR?

Follow-up to #5637, covering two review findings on that PR.

  1. One shared injector for V1 sandbox APIs. createIvm in src/util/ivmFactory.js and injectFreshApis in src/util/ivmCache/contextReset.js each kept their own copy of the host APIs injected into the isolate (_ivm, _fetch, _fetchV2, _geolocation, _getCredential, log, extractStackTrace). The copies drifted: the reset path never set log, so with USE_IVM_CACHE=true every cache hit of a V1 transformation that called log() failed with ReferenceError: log is not defined. That bug existed from feat: ivm cache #4675 until fix(user-transformations): preserve log in cached V1 contexts #5637. Both paths now call injectSandboxApis in the new src/util/ivmSandboxApis.js, so they cannot drift again.
  2. The production log no longer crosses into the host. Outside test mode, log was a host callback that did nothing. isolated-vm still deep-copied every argument into host memory (outside the isolate's memoryLimit) before calling it. It also threw ... could not be cloned for function or symbol arguments. The host now injects log only in test mode, where it captures output for the test API. Otherwise the bootstrap script defines global.log = function () {} inside the isolate.

What is the related Linear task?

Follow-up to MAR-2930 (#5637).

Please explain the objectives of your changes below

The moved code is unchanged apart from two things. The credentials error message reads ids from trTags. log is wrapped in if (testMode). contextReset.js drops about 130 lines of duplicated code.

Sandbox exposure goes down. One fewer host function is reachable from customer code in production, and nothing new is added.

Any changes to existing capabilities/behaviour, mention the reason & what are the changes ?

  • In production, log(x) is now a pure in-isolate no-op. Calls that used to throw for uncloneable arguments, such as log(fn), now succeed.
  • Test mode (testMode = true) still captures Log: ... lines through the host callback. The existing user_transformation_code_test_output.json fixture covers this and passes.
  • The reset path now reads GEOLOCATION_TIMEOUT_IN_MS once at module load, the same as the fresh path, instead of on every context reset.

Any new dependencies introduced with this change?

N/A

Any new generic utility introduced or modified. Please explain the changes.

src/util/ivmSandboxApis.js exports injectSandboxApis(jail, { trTags, credentials, testMode, logs }). Both the fresh-isolate path and the cached-isolate reset path use it.

Any technical or performance related pointers to consider with the change?

log calls in production no longer copy their arguments to the host or block on a host round trip.

Validation

  • I rewrote the cache-hit regression test from fix(user-transformations): preserve log in cached V1 contexts #5637 under a neutral name. It calls log('...', () => 1) and returns Object.getOwnPropertyNames(globalThis). It asserts that the cache-miss and cache-hit outputs are equal, which checks that both paths expose the same globals.
  • Mutation check: forcing the host log back on in production makes that test fail.
  • NODE_OPTIONS='--no-node-snapshot' npx jest -c jest.default.config.js test/__tests__/ivmCache test/__tests__/user_transformation*.test.js src/util/ivm/scriptRunner.test.ts src/routerUtils.test.js src/services/eventTest/__tests__/eventTester.test.ts --runInBand passes 246/246 on Node 22.
  • lint-staged (eslint and prettier) passes.

Developer checklist

  • My code follows the style guidelines of this project

  • No breaking changes are being introduced.

  • All related docs linked with the PR? N/A

  • All changes manually tested?

  • Any documentation changes needed with this change? No.

  • Is the PR limited to 10 file changes?

  • Is the PR limited to one linear task?

  • Are relevant unit and component test-cases added in new readability format?

Reviewer checklist

  • Is the type of change in the PR title appropriate as per the changes?

  • Verified that there are no credentials or confidential data exposed with the changes.

🤖 Generated with Claude Code

…ep no-op log in-isolate

createIvm and the cached-isolate context reset each kept their own copy of
the host APIs injected into the sandbox. The copies drifted once already:
the reset path never set log, so cached V1 transformations failed with
"log is not defined" until #5637. Both paths now call injectSandboxApis in
src/util/ivmSandboxApis.js, so they expose the same globals.

Outside test mode, log was a host callback that did nothing. isolated-vm
still copied every argument into host memory before calling it, and threw
"could not be cloned" for function or symbol arguments. The host now sets
log only in test mode, where it captures output. Otherwise the bootstrap
script defines a no-op log inside the isolate.

The cache-hit regression test now asserts that a cache miss and a cache hit
see the same globals, and that log accepts an uncloneable argument.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

🔒 Scanned for secrets using gitleaks 8.28.0
@sivashanmukh
sivashanmukh requested review from a team as code owners October 14, 2026 15:03
@coderabbitai

coderabbitai Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 611a38d3-3ebe-4ae8-89d8-8b90dc916012

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.13%. Comparing base (baf5d6a) to head (3e13ba9).
⚠️ Report is 134 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5675   +/-   ##
========================================
  Coverage    92.12%   92.13%           
========================================
  Files          713      714    +1     
  Lines        39591    39526   -65     
  Branches      9276     9201   -75     
========================================
- Hits         36474    36416   -58     
- Misses        2852     2925   +73     
+ Partials       265      185   -80     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lvrach lvrach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved. Both paths now call injectSandboxApis. The reset path re-runs the bootstrap, so cache hits also get the in-isolate no-op log, and test mode still captures logs through the host callback.

[praise] The cache-hit test compares Object.getOwnPropertyNames(globalThis) on a miss and a hit, which pins both paths to the same globals.
CI: only the live (hs) job in Live Integration Tests fails. That workflow is also red on unrelated PRs today.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is considered to be stale. It has been open for 20 days with no further activity thus it is going to be closed in 7 days. To avoid such a case please consider removing the stale label manually or add a comment to the PR.

@github-actions github-actions Bot added the Stale label Nov 16, 2026
@github-actions github-actions Bot closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants