Skip to content

A local file can take a hub package's 0x… namespace: ../0x<hash>/f.bend from a subdirectory is loaded locally but named like the package #1005

Closed

Description

@jasisz

What I did

A hub package lives in $BEND_LIB/0x<hash>/ and its files get the namespace 0x<hash>/<file>. book_load decides whether an import goes to the cache by testing the normalized import path against /^0x[0-9a-f]+\//, but computes the namespace afterwards as path.posix.join(path.posix.dirname(ns), rel) (book_load). From a file in a subdirectory, ../0x<hash>/f.bend does not start with 0x, so it is read from the local disk, while the join normalizes its namespace to 0x<hash>/f: exactly the namespace of the package file it imitates.

In the examples below, 0x0123… stands for 0x0123456789abcdef0123456789abcdef. The package is already in the cache (BEND_LIB points at a scratch directory, BEND_HUB is unreachable, and there is no network access).

$BEND_LIB/0x0123…/f.bend (the published package):

import Base

def two() -> Nat:
  Nat.add(1n, 1n)

What happened

On its own, the local file collides with the package. Put a local proj/0x0123…/f.bend next to the project:

import Base

def two() -> Nat:
  3n

proj/sub/n.bend:

import Base
import ../0x0123456789abcdef0123456789abcdef/f.bend as Fake

def unused() -> Nat:
  0n

proj/main.bend:

import Base
import ./sub/n.bend as M
import 0x0123456789abcdef0123456789abcdef/f.bend as F

def main() -> Nat:
  F.two()
Error:
- expected : a fresh name (duplicate declaration: 0x0123456789abcdef0123456789abcdef/f.two)

The two files have different real paths, so seen loads both, and the "one namespace per file" check never fires: two files share one namespace. The real package can no longer be loaded, and the error names the package's own declaration as the duplicate.

Together with #1002, the local file changes what the package means. Replace the local file with proj/0x0123…/f.Nat.bend:

import Base

def add(a: Nat, b: Nat) -> Nat:
  7n

and import it from proj/sub/m.bend as import ../0x0123456789abcdef0123456789abcdef/f.Nat.bend as N, with main.bend importing ./sub/m.bend before the package. bun bend2/main.ts main.bend then prints 7n instead of 2n: the package's Nat.add resolves to the local 0x0123…/f.Nat.add by the rule reported in #1002. The package file in the cache is byte-for-byte what was published; its meaning is not.

A name@version/f.bend import is rewritten to the same hash path before any of this (name_hash), so named packages are affected the same way. I read this from the code but did not run it, because it needs the hub.

Why it matters

A hash (or a name@version that never moves) is supposed to fix what a dependency is. Here a local file can occupy the dependency's namespace, and then:

For the LAWS.bend gate, this means laws about a hub library the human trusts by hash can be proven against a different library, and nothing in LAWS.bend, PROOF.bend or the cached package changes.

pkg_files also skips every file whose namespace starts with 0x when publishing, on the assumption that such files come from the hub. From reading the code, a local file that took such a namespace would be checked by the publisher but left out of the published package. I have not run this.

Expected behavior

The namespace should follow where the file actually comes from: book_load should reject a namespace starting with 0x for any file outside BEND_LIB. It may also be worth enforcing "one file per namespace" as well as "one namespace per file". This is separate from #1002: fixing name resolution there removes the rewrite, but the collision above remains.

Version

bend 2.0.26
commit 6a77e124
Bun 1.4.2
Darwin arm64

Activity

  1. jasisz commented on Sep 23, 2026

    @jasisz
    ContributorAuthor

    One more way into the same boundary, which also affects the fix proposed above.

    BEND_LIB comes from process.env (bend.ts#L1015). Bun loads a .env from the working directory into process.env when the shell does not set the variable, both for bun bend2/main.ts and for a binary built with bun build --compile using the default options. book_load checks a package against its hash only when the file is missing from BEND_LIB (bend.ts#L1052-L1065). So a project can point BEND_LIB at its own copy of a package, and that copy is used without any check.

    Repro (0x0123… = 0x0123456789abcdef0123456789abcdef, BEND_HUB unreachable, no network):

    • the package in the user's cache: def f() -> Nat: 2n
    • LAWS.bend imports 0x0123…/lib.bend as L and declares law f_is_5: {L.f() == 5n : Nat}
    • PROOF.bend imports it too and fills the law with {==}
    • the project ships .cache/0x0123…/lib.bend (the same file, with f returning 5n) and a one-line .env: BEND_LIB=./.cache
    $ BEND_LIB=$HOME_LIB bun bend2/main.ts PROOF.bend
    Error:
    - expected : 2n
    - observed : 5n
    $ bun bend2/main.ts PROOF.bend        # BEND_LIB not set in the shell
    All terms check.
    $ bun bend2/main.ts main.bend         # main prints L.f()
    5
    

    No file is renamed and no namespace is taken; the cached file is simply different. Rejecting a 0x… namespace for files outside BEND_LIB, as proposed above, would not help here, because the project decides where BEND_LIB is.

    Two changes would close it:

    • bend should not read .env from the working directory (bun build --compile can turn dotenv autoloading off). This also stops a project from setting CC or BEND_ORIGIN for the user, for example making bend main.bend -o app run a script from the project.
    • Check cached package files against their hash on every load, not only when a file is missing.

    Tested on 6a77e12, Bun 1.4.2, Darwin arm64.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions