Skip to content

fix(js): authenticate BashTool snapshots#1838

Merged
chaliy merged 5 commits into
mainfrom
2026-06-02-fix-bashtool-snapshot-vulnerability
Jun 2, 2026
Merged

fix(js): authenticate BashTool snapshots#1838
chaliy merged 5 commits into
mainfrom
2026-06-02-fix-bashtool-snapshot-vulnerability

Conversation

@chaliy
Copy link
Copy Markdown
Contributor

@chaliy chaliy commented Jun 2, 2026

Motivation

  • Prevent unauthenticated snapshot restores for multi-tenant tool instances by requiring HMAC-authenticated snapshots for BashTool flows and steering JS bindings to keyed Rust APIs when a key is supplied.

Description

  • Add hmacKey to JS SnapshotOptions and plumb it through toNativeSnapshotOptions into the napi binding.
  • Require a non-empty hmacKey for BashTool.snapshot, BashTool.restoreSnapshot, and BashTool.fromSnapshot in the JS wrapper and reject missing/empty keys.
  • Use Rust keyed snapshot APIs (snapshot_to_bytes_keyed, from_bytes_keyed, restore_snapshot_keyed) when a key is present; fall back to unkeyed APIs for plain Bash usage.
  • Update JS integration tests and README.md to require/illustrate hmacKey usage and add an integration test asserting missing/wrong/tampered-key failures.

Testing

  • Ran cargo fmt --check and cargo check -p bashkit-js (both succeeded).
  • Installed JS deps and built the napi binding with pnpm install --frozen-lockfile and pnpm run build:napi (succeeded).
  • Built JS artifacts with pnpm run build:cjs && pnpm run build:ts and ran pnpm run type-check (succeeded).
  • Ran JS integration tests with ./node_modules/.bin/ava --match="integration: BashTool *" and all relevant integration tests passed.
  • pnpm run format:check initially reported pre-existing unrelated formatting warnings; changed files were formatted with prettier --write as part of the rollout.

Codex Task

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Jun 2, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 3bdf267 Commit Preview URL Jun 02 2026, 01:52 PM

@chaliy chaliy force-pushed the 2026-06-02-fix-bashtool-snapshot-vulnerability branch from 21aee56 to 63722a9 Compare June 2, 2026 09:20
@chaliy chaliy force-pushed the 2026-06-02-fix-bashtool-snapshot-vulnerability branch from 63722a9 to ef3a68e Compare June 2, 2026 13:32
Copilot AI review requested due to automatic review settings June 2, 2026 13:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens snapshot/restore for multi-tenant BashTool usage by requiring HMAC-authenticated snapshots and plumbing an hmacKey through the JS wrapper into the napi/Rust keyed snapshot APIs, while keeping plain Bash usage backward-compatible (unkeyed by default, keyed when a key is supplied).

Changes:

  • Added hmacKey to JS SnapshotOptions, converting it to a native Buffer and enforcing it for BashTool snapshot/restore/fromSnapshot flows.
  • Extended the napi/Rust SnapshotOptions with hmac_key and routed snapshot/restore to keyed Rust APIs when a key is present (required for BashTool).
  • Updated README examples and JS integration tests to require/verify HMAC behavior for BashTool snapshots.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
crates/bashkit-js/wrapper.ts Adds hmacKey plumbing and enforces HMAC key presence for BashTool snapshot APIs.
crates/bashkit-js/src/lib.rs Adds hmac_key to napi SnapshotOptions and switches to keyed Rust snapshot APIs when a key is provided/required.
crates/bashkit-js/README.md Updates snapshot security guidance and BashTool snapshot usage examples/API list.
crates/bashkit-js/__test__/integration.spec.ts Updates BashTool integration tests to include required hmacKey and verify HMAC failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/bashkit-js/wrapper.ts Outdated
Comment on lines 1260 to 1263
snapshot(options?: SnapshotOptions): Uint8Array {
requireSnapshotHmacKey(options);
return this.native.snapshot(toNativeSnapshotOptions(options));
}
Comment thread crates/bashkit-js/wrapper.ts Outdated
Comment on lines 1270 to 1276
restoreSnapshot(data: Uint8Array, options?: SnapshotOptions): void {
requireSnapshotHmacKey(options);
this.native.restoreSnapshot(
Buffer.from(data),
toNativeSnapshotOptions(options),
);
}
Comment on lines +1301 to +1306
static fromSnapshot(
data: Uint8Array,
options?: BashOptions,
snapshotOptions?: SnapshotOptions,
): BashTool {
requireSnapshotHmacKey(snapshotOptions);
Comment on lines +272 to +273
const snapshotKey = new TextEncoder().encode("integration snapshot hmac key");

Comment thread crates/bashkit-js/README.md Outdated
- `Bash.fromSnapshot(data)` / `Bash.fromSnapshotKeyed(data, key, options?)`
- `snapshot(options?)` / `snapshotKeyed(key, options?)`
- `restoreSnapshot(data, options?)` / `restoreSnapshotKeyed(data, key)`
- `Bash.fromSnapshot(data, options?)` / `Bash.fromSnapshotKeyed(data, key, options?)`
chaliy added 2 commits June 2, 2026 13:47
- BashTool.snapshot/restoreSnapshot require SnapshotOptions & { hmacKey }
- BashTool.fromSnapshot second param is BashOptions | undefined so third
  (snapshotOptions with required hmacKey) can be non-optional
- Add Bash hmacKey roundtrip and tamper-rejection integration tests
- Fix README: Bash.fromSnapshotKeyed has no options parameter
@chaliy chaliy merged commit bf4031f into main Jun 2, 2026
29 checks passed
@chaliy chaliy deleted the 2026-06-02-fix-bashtool-snapshot-vulnerability branch June 2, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants