Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/runtime/server/ServerConfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ impl ServerConfig {
}

if let Some(unix) = arg.get_stringish(global, "unix")? {
let unix = bun_core::OwnedString::new(unix);
let unix_str = unix.to_utf8();
if !unix_str.slice().is_empty() {
if has_hostname {
Expand Down
37 changes: 36 additions & 1 deletion test/js/bun/http/bun-serve-args.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serve } from "bun";
import { describe, expect, test } from "bun:test";
import { tmpdirSync } from "../../../harness";
import { bunEnv, bunExe, isASAN, tmpdirSync } from "../../../harness";

const defaultHostname = "localhost";

Expand Down Expand Up @@ -583,6 +583,41 @@ describe("Bun.serve unix socket validation", () => {
).toThrow();
});

test("unix option does not leak the string", async () => {
// The WTFStringImpl backing the 'unix' option was never deref'd. Drive the
// hostname+unix error path (no actual bind) with a large string and check
// RSS growth stays bounded.
const src = `
const base = Buffer.alloc(512 * 1024, "x").toString();
function once(i) {
try {
Bun.serve({ hostname: "127.0.0.1", unix: "/tmp/" + i + base, fetch: () => new Response("ok") });
} catch {}
}
for (let i = 0; i < 50; i++) once(i);
Bun.gc(true);
Bun.gc(true);
const before = process.memoryUsage.rss();
for (let i = 0; i < 1000; i++) once(i);
Bun.gc(true);
Bun.gc(true);
const after = process.memoryUsage.rss();
console.log(JSON.stringify({ deltaMB: (after - before) / 1024 / 1024 }));
`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", src],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
if (exitCode !== 0) console.error(stderr);
const { deltaMB } = JSON.parse(stdout);
// Leaking: 1000 * 512KB ≈ 500MB. Fixed: ~22MB (flat, independent of string size).
expect(deltaMB).toBeLessThan(isASAN ? 192 : 64);
Comment thread
robobun marked this conversation as resolved.
expect(exitCode).toBe(0);
});

describe("invalid unix socket paths should throw", () => {
const invalidPaths = [
{
Expand Down
Loading