Skip to content
Merged
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
22 changes: 19 additions & 3 deletions src/runtime/shell/builtin/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,34 @@ impl Rm {
// Check that none of the paths will delete the root.
{
let cwd = Builtin::shell(interp, cmd).cwd().to_vec();
// Operands are unbounded user input, so neither
// step may use the fixed-size thread-local
// buffers behind `join` / `normalize_string`.
// Normalizing never grows a path by more than
// one byte.
let mut join_spill = Vec::new();
let mut normalize_buf = Vec::new();

for i in args_start..argc {
let path = Builtin::of(interp, cmd).arg_bytes(i);
let resolved: &[u8] = if Platform::AUTO.is_absolute(path) {
path
} else {
resolve_path::join::<platform::Auto>(&[&cwd, path])
resolve_path::join_spill::<platform::Auto>(
&mut join_spill,
&[&cwd, path],
)
};
let normalized = resolve_path::normalize_string::<
if normalize_buf.len() <= resolved.len() {
normalize_buf.resize(resolved.len() + 1, 0);
}
let normalized = resolve_path::normalize_string_buf::<
false,
platform::Auto,
>(resolved);
false,
>(
resolved, &mut normalize_buf[..]
);
let dirname =
resolve_path::dirname::<platform::Auto>(normalized);
if dirname.is_empty() {
Expand Down
64 changes: 63 additions & 1 deletion test/js/bun/shell/commands/rm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { $ } from "bun";
import { beforeAll, describe, expect, setDefaultTimeout, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
import { bunEnv, bunExe, isWindows, tempDir } from "harness";
import { existsSync, mkdirSync, renameSync, symlinkSync, writeFileSync } from "node:fs";
import path from "path";
import { createTestBuilder, sortedShellOutput } from "../util";
Expand Down Expand Up @@ -329,3 +329,65 @@ test.skipIf(process.platform === "win32")(
expect(exitCode).toBe(0);
},
);

// The preserve-root check resolved every operand through the fixed-size
// thread-local path buffers (1024 bytes for normalizing, 4096 for joining),
// so an operand longer than either crashed the process before rm ever
// touched the filesystem. Runs in a child process so the crash shows up as a
// failed assertion rather than taking the test runner down with it.
test("operands longer than the path scratch buffers are reported, not a crash", async () => {
using dir = tempDir("rm-long-operand", { "short.txt": "" });
const long = Buffer.alloc(1100, "a").toString();
const longer = Buffer.alloc(8192, "b").toString();
const absolute = path.join(String(dir), long);
// Joins back down to "/" no matter how long it is, so it still has to be refused.
const upToRoot = Buffer.alloc(6000, "../").toString();

const fixture = /* ts */ `
import { $ } from "bun";
import { existsSync } from "node:fs";
$.nothrow();
const { LONG, LONGER, ABSOLUTE, UP_TO_ROOT } = process.env;
const run = async (...args: string[]) => {
const { exitCode, stderr } = await $\`rm \${args}\`.quiet();
return { exitCode, stderr: stderr.toString() };
};
const results = {
relative: await run(LONG!),
absolute: await run(ABSOLUTE!),
overJoinBuffer: await run(LONGER!),
mixed: { ...(await run(LONG!, "short.txt")), shortRemoved: !existsSync("short.txt") },
upToRoot: UP_TO_ROOT ? await run(UP_TO_ROOT) : undefined,
};
console.log(JSON.stringify(results));
`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", fixture],
env: {
...bunEnv,
LONG: long,
LONGER: longer,
ABSOLUTE: absolute,
// The check is effectively a no-op for drive-rooted paths on Windows.
...(isWindows ? {} : { UP_TO_ROOT: upToRoot }),
},
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");

// Which errno an over-long name produces is up to the OS; what matters is
// that each operand fails on its own and the others are still processed.
const tooLong = (operand: string) =>
isWindows ? expect.stringMatching(/^rm: /) : `rm: ${operand}: File name too long\n`;
expect(JSON.parse(stdout)).toEqual({
relative: { exitCode: 1, stderr: tooLong(long) },
absolute: { exitCode: 1, stderr: tooLong(absolute) },
overJoinBuffer: { exitCode: 1, stderr: tooLong(longer) },
mixed: { exitCode: 1, stderr: tooLong(long), shortRemoved: true },
...(isWindows ? {} : { upToRoot: { exitCode: 1, stderr: 'rm: "/" may not be removed\n' } }),
});
expect(exitCode).toBe(0);
});