Skip to content

fix(cli): confine bundled imports to page directory - #2358

Open
beardthelion wants to merge 1 commit into
bendlang:mainfrom
beardthelion:fix/cli-bundle-confine
Open

beardthelion wants to merge 1 commit into
bendlang:mainfrom
beardthelion:fix/cli-bundle-confine

Conversation

@beardthelion

Copy link
Copy Markdown

Fixes #2356.

Summary

bend page.html -o out runs Bun.build with a loader for .bend imports and a virtual module for inline <script type="module"> blocks. Every other import specifier falls through to Bun's asset loader, which copies the resolved file into the publishable output directory. A page could therefore read any file the bundler's user could read: a ../ path to a sibling secret, an absolute path such as /etc/hostname, or a symlink named .bend pointing outside the page tree.

cli_bundle now adds a third, minimal onResolve plugin. For file-namespace specifiers it resolves with Bun.resolveSync, canonicalizes with the existing path_real(), and refuses any target outside the entry page's real directory. This covers relative, absolute, and bare specifiers, including bare imports that resolve through node_modules links. Real .bend modules outside the page directory are still allowed because the loader compiles them rather than copying them as assets. The entry point, virtual namespaces, and unresolvable imports are left to their existing handling; the -o overwrite guard and all control behavior are unchanged. A rejected import prints a clean CLI error such as:

bend: bundle import "../secretdir/credentials" is outside the page directory (see bend --help)

Test plan

  • Added tests/io/bundle_page_confine.bend and tests/io/bundle_page_confine.js. The foreign-JS test creates a temp tree with a canary secret outside the page directory, a valid in-tree .bend module, a real out-of-tree .bend module, a .bend symlink to the secret, and a node_modules symlink to the secret. It builds pages with a ../ import, a self-contained absolute import, the symlink, and a bare pkg/credentials specifier, plus in-tree and out-of-tree .bend controls. It asserts each escaping import exits 1 with the confinement error and no crash text, both controls bundle and their output runs, and no output file contains the canary. Temp directories are removed on exit; telemetry is disabled and HOME is set to the temp dir.
  • Verified the repro cases are refused, the in-tree control still bundles and runs, and -o . still reports the overwrite error.
  • Updated CHANGELOG.md with the CLI behavior change.
  • Ran bun gates/repo.ts: PASS 56 / 56.
  • Ran bun gates/test.ts; it could not acquire cluster nodes because ssh cluster does not resolve in this environment. The new test and the existing bundle tests (bundle_page_input, bundle_inline, html_script_types, bundle_own_exports) were each run directly and returned 0.
  • bend2/main.ts token count: 13656 with ttok -m gpt-3.5-turbo, under the 16000 cap.

Cross-references

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.

bend page.html -o out copies files the page imports from outside its directory

1 participant