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/codegen/generate-js2native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export function getJS2NativeRust() {
"JS2Rust___src_runtime_dns_jsc_dns_rs__Resolver_getRuntimeDefaultResultOrderOption",
"JS2Rust___src_runtime_dns_jsc_dns_rs__Resolver_newResolver",
"JS2Rust___src_runtime_dns_jsc_dns_rs__internal_seedCacheForTesting",
"JS2Rust___src_runtime_dns_jsc_dns_rs__internal_isLocalhostNameForTesting",
]);

const srcRoot = path.resolve(import.meta.dir, "..");
Expand Down
10 changes: 9 additions & 1 deletion src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,19 @@ export const stringsInternals = {
};

/** Seed the connect-path DNS cache for `hostname` via the real `process_results` interleave; returns family order. */
export const dnsCacheSeed = $newRustFunction("runtime/dns_jsc/dns.rs", "internal.seedCacheForTesting", 2) as (
export const dnsCacheSeed = $newRustFunction("runtime/dns_jsc/dns.rs", "internal.seedCacheForTesting", 3) as (
hostname: string,
addresses: string[],
firstFamily?: 4 | 6,
) => number[];

/** The names the connect-path resolver treats as loopback names ("localhost" and below); see `add_filtered_loopback_family`. */
export const dnsIsLocalhostName = $newRustFunction(
"runtime/dns_jsc/dns.rs",
"internal.isLocalhostNameForTesting",
1,
) as (hostname: string) => boolean;

export const fetchH2Internals = {
liveCounts: $newRustFunction("http/H2Client.rs", "TestingAPIs.liveCounts", 0) as () => {
sessions: number;
Expand Down
125 changes: 107 additions & 18 deletions src/runtime/dns_jsc/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,72 @@ pub mod internal {
hints_copy
}

/// "localhost" or a name under it, with or without the root dot (RFC 6761 §6.3).
fn is_localhost_name(host: &[u8]) -> bool {
const LOCALHOST: &[u8] = b"localhost";
let host = host.strip_suffix(b".").unwrap_or(host);
let Some(prefix_len) = host.len().checked_sub(LOCALHOST.len()) else {
return false;
};
let (prefix, label) = host.split_at(prefix_len);
strings::eql_case_insensitive_ascii(label, LOCALHOST, true)
&& (prefix.is_empty() || prefix.ends_with(b"."))
}
Comment thread
robobun marked this conversation as resolved.
Resolved

/// `bun:internal-for-testing`: [`is_localhost_name`], whose effect on a real lookup only shows on a host where AI_ADDRCONFIG filters loopback.
pub(crate) fn is_localhost_name_for_testing(
global: &JSGlobalObject,
frame: &CallFrame,
) -> JsResult<JSValue> {
let hostname = frame.argument(0);
if !hostname.is_string() {
return Err(global.throw_invalid_arguments(format_args!("expected (hostname: string)")));
}
let hostname = hostname.to_slice(global)?;
Ok(JSValue::js_boolean(is_localhost_name(hostname.slice())))
}

/// glibc's AI_ADDRCONFIG drops a family present only on loopback, so a localhost name answered with one family gets the unfiltered list; returns the answered family to keep at the head, else `AF_UNSPEC`.
#[cfg(not(windows))]
unsafe fn add_filtered_loopback_family(
hints: &mut AddrInfo,
host_ptr: *const c_char,
service: *const c_char,
addrinfo: &mut *mut AddrInfo,
) -> c_int {
if hints.ai_family != netc::AF_UNSPEC {
return netc::AF_UNSPEC;
}
// SAFETY: the caller passes the non-null list a successful getaddrinfo() with
// `hints`, `host_ptr` and `service` returned, and frees whatever list is left
// in `*addrinfo` on return.
let filtered_family = unsafe { (**addrinfo).ai_family };
let mut node = *addrinfo;
while !node.is_null() {
// SAFETY: walking the same list.
unsafe {
if (*node).ai_family != filtered_family {
return netc::AF_UNSPEC;
}
node = (*node).ai_next;
}
}

hints.ai_flags &= !netc::AI_ADDRCONFIG;
let mut unfiltered: *mut AddrInfo = ptr::null_mut();
// SAFETY: FFI getaddrinfo with the caller's still-valid host/service
// pointers; `hints`/`unfiltered` are live locals.
if unsafe { libc::getaddrinfo(host_ptr, service, &raw const *hints, &raw mut unfiltered) }
!= 0
{
return netc::AF_UNSPEC;
}
// SAFETY: `*addrinfo` came from getaddrinfo() and nothing else points into it.
unsafe { bun_dns::freeaddrinfo(*addrinfo) };
*addrinfo = unfiltered;
filtered_family
}

// `Request` is passed opaquely to usockets and round-tripped back into
// Rust; the C side never dereferences fields, so layout is irrelevant.
#[allow(improper_ctypes)]
Expand Down Expand Up @@ -2492,11 +2558,12 @@ pub mod internal {
// Pack getaddrinfo results into one allocation with address families
// interleaved (RFC 8305 §4) so an unroutable family can never fill all
// CONCURRENT_CONNECTIONS parallel connect attempts. See #4938 / #33278.
fn process_results(info: *mut AddrInfo) -> Box<[ResultEntry]> {
// `first_family` takes index 0 and the even slots (AF_UNSPEC: the list's own leading family); the slot arithmetic is a bijection for any split.
fn process_results(info: *mut AddrInfo, first_family: c_int) -> Box<[ResultEntry]> {
let mut count: usize = 0;
let mut n_first: usize = 0;
let first_family = if info.is_null() {
netc::AF_UNSPEC
let first_family = if first_family != netc::AF_UNSPEC || info.is_null() {
first_family
} else {
// SAFETY: info is a live addrinfo node; freed by caller after we return.
unsafe { (*info).ai_family }
Expand Down Expand Up @@ -2574,9 +2641,9 @@ pub mod internal {
results
}

fn after_result(req: *mut Request, info: *mut AddrInfo, err: c_int) {
fn after_result(req: *mut Request, info: *mut AddrInfo, err: c_int, first_family: c_int) {
let results: Option<Box<[ResultEntry]>> = if !info.is_null() {
let res = process_results(info);
let res = process_results(info, first_family);
// ws2_32!getaddrinfo-allocated on Windows — free via the matching
// ws2_32!freeaddrinfo (NOT uv_freeaddrinfo: different allocator).
// `.cast()` is identity on POSIX, libuv_sys→ws2_32 addrinfo on Windows.
Expand Down Expand Up @@ -2645,29 +2712,36 @@ pub mod internal {
&wsa_hints,
&mut addrinfo,
);
after_result(req, addrinfo.cast(), err);
after_result(req, addrinfo.cast(), err, netc::AF_UNSPEC);
}
#[cfg(not(windows))]
// SAFETY: FFI getaddrinfo; `req.key.host` is the owned NUL-terminated host
// set at construction, `hints`/`addrinfo` are stack locals.
unsafe {
let mut addrinfo: *mut AddrInfo = ptr::null_mut();
let mut hints = get_hints();
let host = (*req).key.host.as_ref();

let host_ptr = (*req)
.key
.host
.as_ref()
let host_ptr = host
.map(|h| h.as_ptr().cast::<c_char>())
.unwrap_or(ptr::null());
let mut err = libc::getaddrinfo(host_ptr, service, &raw const hints, &raw mut addrinfo);

// optional fallback
if err == netc::EAI_NONAME && (hints.ai_flags & netc::AI_ADDRCONFIG) != 0 {
hints.ai_flags &= !netc::AI_ADDRCONFIG;
err = libc::getaddrinfo(host_ptr, service, &raw const hints, &raw mut addrinfo);
let mut first_family = netc::AF_UNSPEC;
if (hints.ai_flags & netc::AI_ADDRCONFIG) != 0 {
if err == netc::EAI_NONAME {
// optional fallback
hints.ai_flags &= !netc::AI_ADDRCONFIG;
err = libc::getaddrinfo(host_ptr, service, &raw const hints, &raw mut addrinfo);
} else if err == 0
&& !addrinfo.is_null()
&& host.is_some_and(|h| is_localhost_name(h.as_bytes()))
{
first_family =
add_filtered_loopback_family(&mut hints, host_ptr, service, &mut addrinfo);
}
}
after_result(req, addrinfo, err);
after_result(req, addrinfo, err, first_family);
}
}

Expand Down Expand Up @@ -2795,7 +2869,7 @@ pub mod internal {
unsafe { (*base.add(i)).ai_next = base.add(i + 1) };
}

let results = process_results(nodes.as_mut_ptr());
let results = process_results(nodes.as_mut_ptr(), netc::AF_UNSPEC);
after_result_entries(req, Some(results), 0);
}

Expand Down Expand Up @@ -2848,14 +2922,15 @@ pub mod internal {
/// `bun:internal-for-testing`: seed the connect-path DNS cache for `hostname`
/// by running `addresses` through the real [`process_results`] interleave and
/// storing the result, so a real `fetch()` / `Bun.connect()` consumes it.
/// The optional `firstFamily` (4 or 6) is what `add_filtered_loopback_family` passes for a loopback name.
pub(crate) fn seed_cache_for_testing(
global: &JSGlobalObject,
frame: &CallFrame,
) -> JsResult<JSValue> {
let args = frame.arguments();
if args.len() < 2 || !args[0].is_string() || !args[1].is_array() {
return Err(global.throw_invalid_arguments(format_args!(
"expected (hostname: string, addresses: string[])"
"expected (hostname: string, addresses: string[], firstFamily?: 4 | 6)"
)));
}
let hostname_slice = args[0].to_slice(global)?;
Expand All @@ -2866,6 +2941,16 @@ pub mod internal {
global.throw_invalid_arguments(format_args!("addresses must have 1..=64 entries"))
);
}
let first_family = match frame.argument(2) {
v if v.is_undefined() => netc::AF_UNSPEC,
v if v.is_int32() && v.as_int32() == 4 => netc::AF_INET,
v if v.is_int32() && v.as_int32() == 6 => netc::AF_INET6,
_ => {
return Err(
global.throw_invalid_arguments(format_args!("firstFamily must be 4 or 6"))
);
}
};

let mut addrs: Vec<SockaddrStorage> = (0..len).map(|_| bun_core::ffi::zeroed()).collect();
let mut nodes: Vec<AddrInfo> = (0..len).map(|_| bun_core::ffi::zeroed()).collect();
Expand Down Expand Up @@ -2911,7 +2996,7 @@ pub mod internal {
unsafe { (*base.add(i)).ai_next = base.add(i + 1) };
}

let results = process_results(nodes.as_mut_ptr());
let results = process_results(nodes.as_mut_ptr(), first_family);

let out = JSValue::create_empty_array(global, results.len())?;
for (i, entry) in (0u32..).zip(results.iter()) {
Expand Down Expand Up @@ -6102,3 +6187,7 @@ export_host_fn!(
internal::seed_cache_for_testing,
"JS2Rust___src_runtime_dns_jsc_dns_rs__internal_seedCacheForTesting"
);
export_host_fn!(
internal::is_localhost_name_for_testing,
"JS2Rust___src_runtime_dns_jsc_dns_rs__internal_isLocalhostNameForTesting"
);
102 changes: 102 additions & 0 deletions test/js/bun/dns/dns-prefetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { dns } from "bun";
import { dnsCacheSeed, dnsIsLocalhostName } from "bun:internal-for-testing";
import { describe, expect, it, test } from "bun:test";
import { bunEnv, bunExe } from "harness";

// What the system says "localhost" is, unfiltered: the JS-facing lookup passes no
// AI_ADDRCONFIG, so this is the same /etc/hosts content the listeners below bind.
const localhostHasV6Loopback = (await dns.lookup("localhost").catch(() => [])).some(({ address }) => address === "::1");

// The DNS cache is process-global, so this runs in its own process to get
// clean counters. Docs promise that a failed connection evicts the host's
// cache entry; a dead host must never be served as a cache hit.
Expand Down Expand Up @@ -40,6 +45,103 @@ test("a failed connect evicts the host's DNS cache entry", async () => {
});
});

// The connect-path resolver (fetch(), Bun.connect(), WebSocket) passes
// AI_ADDRCONFIG to getaddrinfo(). glibc judges each family by the machine's
// non-loopback addresses, so on a host whose only IPv6 address is ::1 (a
// container, typically) it drops the `::1 localhost` line of /etc/hosts, while a
// listener bound to the name "localhost" (or to ::1) sits exactly there. For
// localhost names the resolver now adds the filtered-out loopback family behind
// the family AI_ADDRCONFIG did answer with.
//
// Which names get this, and how the merged list is ordered, are checked
// directly below because a real lookup only shows the difference on such a host.
// The end-to-end tests after that fail without the fix on such a host and are
// plain regression coverage everywhere else.
describe("loopback names and AI_ADDRCONFIG", () => {
test("names that get the filtered loopback family added back", () => {
const expected = {
"localhost": true,
"LOCALHOST": true,
"localhost.": true,
"app.localhost": true,
"app.localhost.": true,
"a.b.LocalHost": true,
"notlocalhost": false,
"localhost.example": false,
"localhost2": false,
"localhost..": false,
"127.0.0.1": false,
"": false,
};
expect(Object.fromEntries(Object.keys(expected).map(name => [name, dnsIsLocalhostName(name)]))).toEqual(expected);
});

test("the family AI_ADDRCONFIG answered with stays at the head of the unfiltered list", () => {
// /etc/hosts lists ::1 first; AI_ADDRCONFIG had answered with IPv4 only, so
// consumers that take the first entry keep connecting to 127.0.0.1 and ::1
// is added behind it. Symmetric for an IPv6-only answer, and unchanged
// (the list's own order) when no family is forced.
expect({
v4Answered: dnsCacheSeed("addrconfig-v4.invalid", ["::1", "127.0.0.1"], 4),
v6Answered: dnsCacheSeed("addrconfig-v6.invalid", ["127.0.0.1", "::1"], 6),
unforced: dnsCacheSeed("addrconfig-unforced.invalid", ["::1", "127.0.0.1"]),
}).toEqual({
v4Answered: [4, 6],
v6Answered: [6, 4],
unforced: [6, 4],
});
});

test.concurrent("fetch() reaches a server listening on the name it connects to", async () => {
await using server = Bun.serve({ port: 0, hostname: "localhost", fetch: () => new Response("ok") });
const response = await fetch(`http://localhost:${server.port}/`);
expect(await response.text()).toBe("ok");
});

// Only meaningful where the system resolver maps localhost to ::1 at all.
describe.skipIf(!localhostHasV6Loopback)("a server on ::1 is reachable through the name", () => {
test.concurrent("fetch()", async () => {
await using server = Bun.serve({
port: 0,
hostname: "::1",
fetch: (request, server) => new Response(server.requestIP(request)!.address),
});
const response = await fetch(`http://localhost:${server.port}/`);
expect(await response.text()).toBe("::1");
});

// getaddrinfo() is case-insensitive, so the name check has to be too.
test.concurrent.each(["localhost", "LOCALHOST"])("Bun.connect({ hostname: %j })", async hostname => {
using listener = Bun.listen({ port: 0, hostname: "::1", socket: { data() {} } });
using socket = await Bun.connect({ hostname, port: listener.port, socket: { data() {} } });
expect(socket.remoteAddress).toBe("::1");
});

test.concurrent("new WebSocket()", async () => {
await using server = Bun.serve({
port: 0,
hostname: "::1",
fetch(request, server) {
return server.upgrade(request) ? undefined : new Response(null, { status: 400 });
},
websocket: {
open(ws) {
ws.send(ws.remoteAddress);
},
message() {},
},
});
const { promise, resolve, reject } = Promise.withResolvers<string>();
const ws = new WebSocket(`ws://localhost:${server.port}/`);
ws.onmessage = event => resolve(String(event.data));
ws.onclose = event => reject(new Error(`closed before any message: ${event.code} ${event.reason}`));
expect(await promise).toBe("::1");
ws.onclose = null;
ws.close();
});
});
});

describe("dns.prefetch", () => {
it("should prefetch", async () => {
// A local server keeps the test off the external network. "localhost" is a
Expand Down