Skip to content

feat(cache): authenticate remote bytecode-cache entries - #4206

Closed
antoniosarosi wants to merge 1 commit into
canaryfrom
antoniosarosi/b-747
Closed

antoniosarosi wants to merge 1 commit into
canaryfrom
antoniosarosi/b-747

Conversation

@antoniosarosi

@antoniosarosi antoniosarosi commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • wrap uploaded remote bytecode-cache entries in an HMAC-SHA256 authenticated envelope
  • keep signing and verification secrets local and separate from the bearer credential sent to the blob server
  • require authentication before entry validation or deserialization when a verification key is configured, while retaining the existing integrity-only no-key mode

Root cause

The existing payload SHA-256 was stored alongside attacker-controlled bytes, so a malicious remote could replace both the executable bytecode and its checksum. A keyed MAC known only to writers and readers prevents the blob-serving origin from forging a replacement.

Validation

  • cargo fmt --all
  • cargo test -p bex_cache
  • cargo clippy -p bex_cache --all-targets -- -D warnings

Fixes B-747

Summary by CodeRabbit

  • New Features

    • Added optional HMAC-SHA256 authentication for remote bytecode cache uploads and downloads.
    • Added support for configuring separate signing and verification keys through environment variables.
    • Remote cache entries can now be verified for authenticity before use.
  • Bug Fixes

    • Tampered or improperly signed remote entries are rejected when verification is enabled.
    • Existing unsigned-cache behavior remains supported when authentication is not configured.

@linear

linear Bot commented Jul 26, 2026

Copy link
Copy Markdown

B-747

@vercel

vercel Bot commented Jul 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jul 26, 2026 2:06pm
promptfiddle Ready Ready Preview, Comment Jul 26, 2026 2:06pm
promptfiddle2 Ready Ready Preview, Comment Jul 26, 2026 2:06pm

Request Review

@vercel
vercel Bot temporarily deployed to Preview – beps July 26, 2026 13:46 Inactive
@github-actions

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@coderabbitai

coderabbitai Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dd96f87c-644a-4cfb-b2c6-90fbc38d1347

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
📝 Walkthrough

Walkthrough

The bytecode cache now supports optional HMAC-SHA256 authentication for remote entries. Signing and verification keys are read from environment variables, authenticated envelopes are handled during remote reads and writes, and tests cover valid, tampered, required-signature, and unsigned compatibility cases.

Changes

Remote cache authentication

Layer / File(s) Summary
Authentication contracts and configuration
baml_language/crates/bex_cache/Cargo.toml, baml_language/crates/bex_cache/src/lib.rs
Adds the HMAC dependency, authenticated envelope constants, remote signing and verification fields, and environment-variable key parsing.
Authenticated remote transport
baml_language/crates/bex_cache/src/lib.rs
Wraps uploads with HMAC envelopes when configured, authenticates and unwraps downloads, and forwards the resulting bytes through local cache validation.
Authentication behavior tests
baml_language/crates/bex_cache/src/lib.rs
Tests valid signed entries, tampered signatures, required signatures, and unsigned remote entries without verification configuration.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant BytecodeCache
  participant RemoteCache
  participant RemoteStore
  BytecodeCache->>RemoteCache: request shared entry
  RemoteCache->>RemoteStore: fetch remote payload
  RemoteStore-->>RemoteCache: signed or unsigned bytes
  RemoteCache->>RemoteCache: verify and unwrap when configured
  RemoteCache-->>BytecodeCache: cache entry bytes
  BytecodeCache->>BytecodeCache: validate and deserialize
Loading

Possibly related PRs

  • BoundaryML/baml#3924: Modifies the same remote/shared cache implementation and provides existing remote CAS support extended here with HMAC verification.

Suggested reviewers: hellovai

Poem

I’m a rabbit guarding bytes in flight,
With HMAC sealed from day to night.
Tainted payloads lose their way,
True signed entries hop through the cache today.
Unsigned friends still find their place—
A verified burrow, at a steady pace.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: authenticating remote bytecode-cache entries.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch antoniosarosi/b-747

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.

@antoniosarosi

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 26, 2026 13:53 Inactive
@coderabbitai

coderabbitai Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
baml_language/crates/bex_cache/src/lib.rs (1)

1592-1655: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add coverage for the untested (None, true) branch.

authenticate_download's doc comment explicitly calls out tolerating signed uploads when no verification key is configured, but none of the four new tests exercise a signed envelope read by a reader with verification_key: None. The three covered branches (Some,true/Some,false/None,false) leave this documented compatibility path unverified.

🧪 Suggested additional test
#[test]
fn signed_remote_entry_is_tolerated_without_verification_key() {
    const AUTH_KEY: [u8; 32] = [0xd8; 32];

    let (url, _) = spawn_http_store();
    let program = Program::default();
    let files = vec![("mixed.baml".to_string(), "fn mixed {}")];
    let key = compute_key(&KeyInputs {
        compiler_fingerprint: [15u8; 32],
        opt_level: 2,
        emit_test_cases: false,
        manifest: None,
        files: &files,
    });

    let writer_dir = tempfile::tempdir().expect("writer tempdir");
    let mut writer = BytecodeCache::open(writer_dir.path().to_path_buf());
    writer.remote = Some(remote_for_with_keys(&url, Some(AUTH_KEY), None));
    writer.store_shared(&key, &program).expect("signed store");

    let reader_dir = tempfile::tempdir().expect("reader tempdir");
    let mut reader = BytecodeCache::open(reader_dir.path().to_path_buf());
    reader.remote = Some(remote_for(&url));
    assert!(
        reader.load_shared(&key).is_some(),
        "signed envelope is tolerated when no verification key is configured"
    );
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@baml_language/crates/bex_cache/src/lib.rs` around lines 1592 - 1655, Add a
test alongside the existing remote cache tests covering a signed upload read
without a verification key. Use remote_for_with_keys with an auth key for the
writer, remote_for for the reader, store a shared entry, and assert load_shared
succeeds to verify the documented (None, true) compatibility branch.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@baml_language/crates/bex_cache/src/lib.rs`:
- Around line 1592-1655: Add a test alongside the existing remote cache tests
covering a signed upload read without a verification key. Use
remote_for_with_keys with an auth key for the writer, remote_for for the reader,
store a shared entry, and assert load_shared succeeds to verify the documented
(None, true) compatibility branch.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a87474b0-10f4-452d-a55d-e1045b85c0b3

📥 Commits

Reviewing files that changed from the base of the PR and between 788d5c0 and 3ddc7f0.

⛔ Files ignored due to path filters (1)
  • baml_language/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • baml_language/crates/bex_cache/Cargo.toml
  • baml_language/crates/bex_cache/src/lib.rs

@github-actions

Copy link
Copy Markdown

Binary size checks passed

✅ 7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
✅ baml-cli Linux 🔒 27.2 MB 11.6 MB file 27.1 MB +153.5 KB (+0.6%) OK
✅ packed-program Linux 🔒 17.6 MB 7.3 MB file 17.6 MB +7.2 KB (+0.0%) OK
✅ baml-cli macOS 🔒 21.2 MB 10.1 MB file 21.0 MB +149.1 KB (+0.7%) OK
✅ packed-program macOS 🔒 13.8 MB 6.4 MB file 13.8 MB +33.1 KB (+0.2%) OK
✅ baml-cli Windows 🔒 22.8 MB 10.3 MB file 22.7 MB +133.6 KB (+0.6%) OK
✅ packed-program Windows 🔒 14.7 MB 6.5 MB file 14.7 MB +4.6 KB (+0.0%) OK
✅ bridge_wasm WASM 16.5 MB 🔒 4.5 MB gzip 4.5 MB +18.7 KB (+0.4%) OK

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.


Generated by cargo size-gate · workflow run

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 26, 2026 14:06 Inactive
@antoniosarosi

Copy link
Copy Markdown
Contributor Author

Closing — this was an orchestration experiment run; not landing.

@antoniosarosi
antoniosarosi deleted the antoniosarosi/b-747 branch July 26, 2026 14:15

This branch was previously deployed

3 inactive deployments
Preview – promptfiddle — 3ddc7f05 Deployed Jul 26, 2026 by vercel[bot]
Preview – promptfiddle2 — 3ddc7f05 Deployed Jul 26, 2026 by vercel[bot]
Preview – beps — 3ddc7f05 Deployed Jul 26, 2026 by vercel[bot]
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.

1 participant