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
5 changes: 1 addition & 4 deletions packages/runtime/src/band-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import { Hono } from "hono";
import { createHash } from "crypto";
import { randomId } from "./random-id";

const BAND_RUNNER_USER = "band-runner";
let executing = false;
Expand Down Expand Up @@ -136,10 +137,6 @@ function shellIgnoreError(cmd: string): void {
Bun.spawnSync(["sudo", "bash", "-c", cmd]);
}

function randomId(): string {
return Math.random().toString(36).slice(2, 10);
}

/** Reject values containing shell metacharacters. Used for any band-config value interpolated into shell commands. */
function shellSafe(value: string, label: string): string {
if (/[;`$(){}|&<>!\\"\n\r]/.test(value)) {
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime/src/random-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { randomBytes } from "crypto";

export function randomId(): string {
return randomBytes(8).toString("hex");
}
21 changes: 21 additions & 0 deletions packages/runtime/test/unit/random-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, test } from "bun:test";
import { randomId } from "../../src/random-id";

describe("randomId", () => {
test("generates 16-character hex IDs", () => {
expect(randomId()).toMatch(/^[0-9a-f]{16}$/);
});

test("does not use Math.random", () => {
const originalRandom = Math.random;
Math.random = () => {
throw new Error("Math.random should not be used for workdir IDs");
};

try {
expect(randomId()).toMatch(/^[0-9a-f]{16}$/);
} finally {
Math.random = originalRandom;
}
});
});
Loading