Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ latest one; the GitHub release carries the same notes.
MattCozendey); a `!` program built for the CPU because no CUDA toolkit
was found says so (PR #1888); colliding output requests are refused
before anything is written (PR #2010 by vicmcorrea); a hyphenated
published file prints valid aliases (PR #1849 by vicmcorrea).
published file prints valid aliases (PR #1849 by vicmcorrea); a page
bundle refuses imports that resolve outside the page's directory.
- **Fixes**:
- Checker and verdict: a law names its module's own def when a Base
constructor shares the name (#1236, PR #2047 by nicolas-abril); `!=`
Expand Down
33 changes: 32 additions & 1 deletion bend2/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,43 @@ async function cli_bundle(page: string, dir: string): Promise<void> {
if (same_file(path.join(dir, path.basename(entry)), entry)) {
cli_fail("-o " + dir + " would overwrite the page");
}
// Non-.bend specifiers reach Bun's asset loader, which copies whatever it
// resolves into the publishable output. Confine file imports to the page's
// real directory; realpath catches links out of it, including links in
// node_modules. Real .bend modules outside the page are compiled by PLUGIN,
// not copied as assets, so they stay available.
const root = path.dirname(entry);
const confine: BunPlugin = {
name: "bend confine",
setup(build) {
build.onResolve({ filter: /.*/ }, (a) => {
if (a.kind === "entry-point-build" || a.kind === "entry-point"
|| a.namespace !== "file") return;
let at: string;
try {
at = Bun.resolveSync(a.path, a.resolveDir || root);
} catch {
return; // an unresolvable import is Bun's error to report
}
const real = path_real(at);
const rel = path.relative(root, real);
if (rel === ".." || rel.startsWith(".." + path.sep)
|| path.isAbsolute(rel)) {
if (at.endsWith(".bend") && real.endsWith(".bend")) {
return; // compiled by PLUGIN, not copied by Bun's asset loader
}
cli_fail("bundle import " + JSON.stringify(a.path)
+ " is outside the page directory");
}
});
},
};
const out = await Bun.build({
entrypoints: [entry],
outdir: dir,
target: "browser",
minify: true,
plugins: [inline, PLUGIN],
plugins: [inline, PLUGIN, confine],
});
for (const a of out.outputs) {
cli_say(1, a.path + " (" + (a.size / 1024).toFixed(1) + "kb)\n");
Expand Down
14 changes: 14 additions & 0 deletions tests/io/bundle_page_confine.bend
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# A page bundles only files inside its own directory: ../, absolute and
# symlinked imports are refused, no output holds the out-of-tree canary, and
# an in-tree .bend import still bundles and runs.
import Base

def Bundle.page_confine() -> IO(U32):
import "./bundle_page_confine.js"

def main() -> IO(Unit):
do IO<Unit>:
value : U32 <- Bundle.page_confine()
IO.print(U32.show(value))

#|0
81 changes: 81 additions & 0 deletions tests/io/bundle_page_confine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
function bundle_page_confine() {
const fs = require("node:fs");
const path = require("node:path");
const child = require("node:child_process");
const dir = fs.mkdtempSync(path.join(process.cwd(), ".bend-page-confine-"));
const env = { ...process.env, HOME: dir, BEND_NO_TELEMETRY: "1",
BEND_ORIGIN: "http://127.0.0.1:9" };
const canary = "CANARY_bd3311 = hunter2";
const build = (page, out) => child.spawnSync(process.execPath,
[path.resolve("bend2/main.ts"), page, "-o", out],
{ encoding: "utf8", env, timeout: 20000 });
const pageDir = path.join(dir, "page");
const secretDir = path.join(dir, "secretdir");
const libDir = path.join(dir, "lib");
const outs = path.join(dir, "outs");
const leaks = (root) => {
for (const ent of fs.readdirSync(root, { withFileTypes: true })) {
const at = path.join(root, ent.name);
if (ent.isDirectory() && leaks(at)) return true;
if (ent.isFile() && fs.readFileSync(at, "utf8").includes(canary)) {
return true;
}
}
return false;
};
const inline = (spec) => '<script type="module">import * as G from '
+ JSON.stringify(spec) + '; console.log(G);</script>\n';
try {
for (const at of [pageDir, secretDir, libDir, outs]) fs.mkdirSync(at);
fs.writeFileSync(path.join(secretDir, "credentials"),
canary + "\nsecond SECRET2\n");
fs.writeFileSync(path.join(pageDir, "good.bend"),
"import Base\ndef value() -> U32:\n 42\n");
fs.writeFileSync(path.join(libDir, "helper.bend"),
"import Base\ndef value() -> U32:\n 7\n");
fs.symlinkSync(path.join(secretDir, "credentials"),
path.join(pageDir, "leak.bend"));
// A bare specifier through node_modules must not reach the same secret.
const modules = path.join(pageDir, "node_modules");
fs.mkdirSync(modules);
fs.symlinkSync(secretDir, path.join(modules, "pkg"));
const absolute = path.join(secretDir, "credentials");
for (const [name, spec] of [["rel", "../secretdir/credentials"],
["abs", absolute], ["link", "./leak.bend"], ["bare", "pkg/credentials"]]) {
fs.writeFileSync(path.join(pageDir, name + ".html"), inline(spec));
}
fs.writeFileSync(path.join(pageDir, "ok.html"),
'<script type="module">import G from "./good.bend";'
+ " console.log(G.value());</script>\n");
// A real out-of-tree .bend module is compiled, not copied as an asset.
fs.writeFileSync(path.join(pageDir, "out.html"),
'<script type="module">import G from "../lib/helper.bend";'
+ " console.log(G.value());</script>\n");
for (const [name, want] of [["ok", "42"], ["out", "7"]]) {
const ok = build(path.join(pageDir, name + ".html"), path.join(outs, name));
if (ok.status !== 0) return 1;
const js = fs.readdirSync(path.join(outs, name))
.find((f) => f.endsWith(".js"));
if (js === undefined || child.spawnSync(process.execPath,
[path.join(outs, name, js)], { encoding: "utf8" }).stdout.trim() !== want) {
return 2;
}
}
// Every escaping import is refused with a clean CLI error, not a crash.
for (const name of ["rel", "abs", "link", "bare"]) {
const r = build(path.join(pageDir, name + ".html"), path.join(outs, name));
if (r.status !== 1 || !r.stderr.includes("outside the page directory")
|| r.stderr.includes("Bundle failed")
|| r.stderr.includes("AggregateError")) {
return 3;
}
}
// No output directory copied the out-of-tree secret.
if (leaks(outs)) return 4;
return 0;
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}

io_eff(CID(Bundle.page_confine), bundle_page_confine);