Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 2 deletions src/dotenv/env_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,12 @@ impl Loader {

let mut endpoint: Box<[u8]> = Box::default();
let mut insecure_http = false;
if let Some(endpoint_) = self
if let Some(endpoint_url) = self
.get(b"S3_ENDPOINT")
.or_else(|| self.get(b"AWS_ENDPOINT"))
.and_then(URL::from_s3_endpoint)
{
let url = URL::parse(endpoint_);
let url = endpoint_url.url();
endpoint = Box::from(url.host_with_path());
insecure_http = url.is_http();
}
Expand Down
7 changes: 3 additions & 4 deletions src/runtime/webcore/s3/credentials_jsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ pub(crate) fn get_credentials_with_options(
if str.tag() != BunStringTag::Empty && str.tag() != BunStringTag::Dead {
let utf8 = str.to_utf8();
let endpoint = utf8.slice();
let url = URL::parse(endpoint);
let normalized_endpoint = url.host_with_path();
if !normalized_endpoint.is_empty() {
if let Some(endpoint_url) = URL::from_s3_endpoint(endpoint) {
let url = endpoint_url.url();
new_credentials.credentials.endpoint =
Box::<[u8]>::from(normalized_endpoint);
Box::<[u8]>::from(url.host_with_path());

// Default to https://
// Only use http:// if the endpoint specifically starts with 'http://'
Expand Down
21 changes: 21 additions & 0 deletions src/url/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,27 @@
Ok(OwnedURL { href: owned })
}

/// An S3 endpoint, `[scheme://]host[:port][/prefix]` with `https` as the default scheme,
/// normalized by WTF::URL so that `host_with_path()` names the host `new URL(endpoint)`
/// names. `parse` alone ends the authority only at `/` and `?`, so it reads
/// `http://a:1#@b:2` and `http://a:1\@b:2` as host `b:2`.
///
/// Returns `None` when `parse` finds no host, and keeps the input as written when WTF::URL
/// rejects it, so this accepts exactly the endpoints `parse` accepts.
pub fn from_s3_endpoint(input: &[u8]) -> Option<OwnedURL> {
let as_written = URL::parse(input);
if as_written.host_with_path().is_empty() {
return None;
}
let normalized = if as_written.protocol.is_empty() {
let with_scheme = [b"https://".as_slice(), input].concat();
URL::from_string(&BunString::borrow_utf8(&with_scheme))
} else {
URL::from_string(&BunString::borrow_utf8(input))
};
Some(normalized.unwrap_or_else(|_| OwnedURL::from_href(Box::from(input))))

Check failure on line 350 in src/url/lib.rs

View check run for this annotation

Claude / Claude Code Review

from_s3_endpoint regresses on 'user:@host:port' — userinfo leaks into stored endpoint

`from_s3_endpoint` regresses on `http://user:@host:port` (empty password): the stored endpoint changes from `host:port` to `user@host:port`. WHATWG serialization drops the empty-password `:` so the href becomes `http://user@host:port/`, and `URL::parse` on that string skips userinfo (its guard requires `first_at > first_colon`, but the only `:` left is the port). The username then leaks into the `Host` header and presigned URL — the same class the `user:p@ss@` test guards against; add `"http:/
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
}

pub fn display_protocol(&self) -> &[u8] {
if !self.protocol.is_empty() {
return self.protocol;
Expand Down
55 changes: 55 additions & 0 deletions test/js/bun/s3/s3-list-encode-overflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,61 @@ describe("S3Client region option", () => {
});
});

describe("S3Client endpoint option", () => {
const options = {
accessKeyId: "test",
secretAccessKey: "test",
bucket: "bucket",
region: "us-east-1",
};

// presign() builds the request URL from the stored endpoint exactly like a
// request would, without opening a connection, so the ports below are never dialed.
it.each([
"http://127.0.0.1:1#@127.0.0.1:2",
"http://127.0.0.1:1\\@127.0.0.1:2",
"http://127.0.0.1:1?@127.0.0.1:2",
"http://user:p@ss@127.0.0.1:1",
"http://127.0.0.1:1/x/../prefix/",
"http://127.1:1",
"https://acct.supabase.co/storage/v1/s3",
"https://[::1]:9000",
])("signs for the origin and path that new URL(%j) names", endpoint => {
const expected = new URL(endpoint);
const presigned = new S3Client({ ...options, endpoint }).presign("key.txt");
// Compared as text: new URL(presigned) would hide a leaked userinfo, a raw
// dot-segment or a non-canonical IPv4 spelling again.
expect(presigned.split("?")[0]).toBe(`${expected.origin}${expected.pathname.replace(/\/$/, "")}/bucket/key.txt`);
});

it("defaults to https when the endpoint has no scheme", () => {
const presigned = new S3Client({ ...options, endpoint: "s3.example.com" }).presign("key.txt");
expect(presigned.split("?")[0]).toBe("https://s3.example.com/bucket/key.txt");
});

it("applies the same parsing to S3_ENDPOINT", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `console.log(new URL(Bun.s3.presign("key.txt")).host)`],
env: {
...bunEnv,
AWS_ENDPOINT: undefined,
S3_ENDPOINT: "http://127.0.0.1:1#@127.0.0.1:2",
S3_ACCESS_KEY_ID: "test",
S3_SECRET_ACCESS_KEY: "test",
S3_BUCKET: "bucket",
S3_REGION: "us-east-1",
},
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stdout).toBe("127.0.0.1:1\n");
expect(stderr).toBe("");
expect(exitCode).toBe(0);
});
});

describe("S3 endpoints without a region component", () => {
it("defaults the signing region to us-east-1", async () => {
await using proc = Bun.spawn({
Expand Down
Loading