Skip to content
10 changes: 9 additions & 1 deletion src/boringssl/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,18 @@ pub unsafe extern "C" fn Bun__X509__checkHost(
/// Certificate name bytes (IA5 / Latin-1) for error messages.
struct NameBytes<'a>(&'a [u8]);

/// Returns `true` iff `name` contains no characters that would require
/// escaping in a subjectAltName entry (Node's `IsSafeAltName`, non-UTF-8 path).
Comment thread
robobun marked this conversation as resolved.
Resolved
#[inline]
fn is_safe_alt_name(name: &[u8]) -> bool {
name.iter()
.all(|&c| (b' '..=b'~').contains(&c) && !matches!(c, b'"' | b'\\' | b',' | b'\''))
}

impl core::fmt::Display for NameBytes<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use core::fmt::Write;
if X509::is_safe_alt_name(self.0, false) {
if is_safe_alt_name(self.0) {
for &byte in self.0 {
f.write_char(char::from(byte))?;
}
Expand Down
3 changes: 2 additions & 1 deletion src/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5038,7 +5038,8 @@
}

// if is no redirect or if is redirect == "manual" just proceed
let is_redirect = status_code >= 300 && status_code <= 399;
// https://fetch.spec.whatwg.org/#redirect-status
let is_redirect = matches!(status_code, 301 | 302 | 303 | 307 | 308);

Check warning on line 5042 in src/http/lib.rs

View check run for this annotation

Claude / Claude Code Review

Inner status_code match arm made dead by narrowed is_redirect

Now that `is_redirect` is exactly `{301, 302, 303, 307, 308}`, the inner `match status_code { 302 | 301 | 307 | 308 | 303 => { ... } _ => {} }` at line 5049 is a redundant guard — its `_ => {}` arm is unreachable. Per REVIEW.md ('Delete dead code in the same PR that makes it dead … guards a new validator makes redundant'), the match wrapper can be collapsed and its body kept directly under the Follow-mode `if`.
Comment thread
robobun marked this conversation as resolved.
Resolved
if is_redirect {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if !is_proxy_connect_failure
&& self.redirect_type == FetchRedirect::Follow
Expand Down
57 changes: 57 additions & 0 deletions test/js/web/fetch/fetch-redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,63 @@ describe("fetch() follows a redirect on headers without waiting for the 3xx body
});
});

// https://fetch.spec.whatwg.org/#redirect-status
// A redirect status is 301, 302, 303, 307, or 308. Other 3xx statuses
// (300, 304, 305, 306) are not redirects and must be returned as-is even
// under redirect: "error".
describe("fetch() with redirect: 'error' only rejects WHATWG redirect statuses", () => {
async function serve(status: number, reason: string, extra = "") {
const server = net.createServer(socket => {
socket.on("error", () => {});
socket.once("data", () => {
socket.end(`HTTP/1.1 ${status} ${reason}\r\n${extra}Content-Length: 0\r\nConnection: close\r\n\r\n`);
});
});
await once(server.listen(0, "127.0.0.1"), "listening");
const { port } = server.address() as net.AddressInfo;
return { server, url: `http://127.0.0.1:${port}/` };
}

it.concurrent.each([
[300, "Multiple Choices", 'Location: /elsewhere\r\n'],
[304, "Not Modified", 'ETag: "v1"\r\n'],
[304, "Not Modified", 'ETag: "v1"\r\nLocation: /elsewhere\r\n'],
[305, "Use Proxy", 'Location: /elsewhere\r\n'],
[306, "unused", ""],
])("%d %s is returned, not rejected", async (status, reason, extra) => {
const { server, url } = await serve(status, reason, extra);
try {
const res = await fetch(url, { redirect: "error", headers: { "if-none-match": '"v1"' } });
expect({ status: res.status, redirected: res.redirected, body: await res.text() }).toEqual({
status,
redirected: false,
body: "",
});
} finally {
server.close();
}
});

it.concurrent.each([
[301, "Moved Permanently"],
[302, "Found"],
[303, "See Other"],
[307, "Temporary Redirect"],
[308, "Permanent Redirect"],
])("%d %s rejects with UnexpectedRedirect", async (status, reason) => {
const { server, url } = await serve(status, reason, "Location: /elsewhere\r\n");
try {
const outcome = await fetch(url, { redirect: "error" }).then(
res => ({ rejected: false as const, status: res.status }),
e => ({ rejected: true as const, code: e.code }),
);
expect(outcome).toEqual({ rejected: true, code: "UnexpectedRedirect" });
} finally {
server.close();
}
});
});

it("fetch() with redirect: 'manual' still exposes the 3xx response body", async () => {
const server = net.createServer(socket => {
socket.on("error", () => {});
Expand Down
Loading