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
8 changes: 4 additions & 4 deletions src/dotenv/env_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ impl Loader {

let mut endpoint: Box<[u8]> = Box::default();
let mut insecure_http = false;
if let Some(endpoint_) = self
if let Some(parsed) = self
.get(b"S3_ENDPOINT")
.or_else(|| self.get(b"AWS_ENDPOINT"))
.and_then(URL::parse_s3_endpoint)
{
let url = URL::parse(endpoint_);
endpoint = Box::from(url.host_with_path());
insecure_http = url.is_http();
endpoint = parsed.host_with_path;
insecure_http = parsed.is_http;
}

let bucket: Box<[u8]> = self
Expand Down
9 changes: 3 additions & 6 deletions src/runtime/webcore/s3/credentials_jsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,12 @@ 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() {
new_credentials.credentials.endpoint =
Box::<[u8]>::from(normalized_endpoint);
if let Some(parsed) = URL::parse_s3_endpoint(endpoint) {
new_credentials.credentials.endpoint = parsed.host_with_path;

// Default to https://
// Only use http:// if the endpoint specifically starts with 'http://'
new_credentials.credentials.insecure_http = url.is_http();
new_credentials.credentials.insecure_http = parsed.is_http;

new_credentials.changed_credentials = true;
} else if !endpoint.is_empty() {
Expand Down
62 changes: 61 additions & 1 deletion src/url/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::cell::RefCell;

use bun_collections::bit_set::{ArrayBitSet, num_masks_for};
use bun_core::{self, fmt as bun_fmt};
use bun_core::{String as BunString, Tag as BunStringTag, strings};
use bun_core::{OwnedString, String as BunString, Tag as BunStringTag, strings};
use bun_paths::resolve_path::{self, platform};
use bun_wyhash::hash as wyhash;

Expand Down Expand Up @@ -149,6 +149,30 @@ pub mod whatwg {
URL__deinit(self)
}
}

/// A `URL` this handle owns; `Drop` frees it.
pub struct Parsed(core::ptr::NonNull<URL>);

impl Parsed {
pub fn from_utf8(input: &[u8]) -> Option<Self> {
URL::from_utf8(input).map(Self)
}
}

impl core::ops::Deref for Parsed {
type Target = URL;
fn deref(&self) -> &URL {
// SAFETY: `from_utf8` returned a live heap `WTF::URL` that only `Drop` frees.
unsafe { self.0.as_ref() }
}
}

impl Drop for Parsed {
fn drop(&mut self) {
// SAFETY: this handle is the only owner of the `WTF::URL`, so it is deleted once.
unsafe { self.0.as_mut() }.deinit();
}
}
}
// Re-export the free helpers at crate root so lower-tier callers can write
// `bun_url::join(...)` / `bun_url::href_from_string(...)` (install, http, bake, js_parser).
Expand Down Expand Up @@ -234,6 +258,13 @@ impl OwnedURL {
}
}

/// What `S3Credentials` keeps of an endpoint. See `URL::parse_s3_endpoint`.
pub struct S3Endpoint {
/// `host[:port][/prefix]`, the form `URL::host_with_path` returns.
pub host_with_path: Box<[u8]>,
pub is_http: bool,
}

impl<'a> URL<'a> {
/// Detach the borrow-checker lifetime from a `URL`.
///
Expand Down Expand Up @@ -329,6 +360,35 @@ impl<'a> URL<'a> {
Ok(OwnedURL { href: owned })
}

/// `input` is `[scheme://]host[:port][/prefix]`, `https` by default. `None` when it has no host.
pub fn parse_s3_endpoint(input: &[u8]) -> Option<S3Endpoint> {
let as_written = URL::parse(input);
if as_written.host_with_path().is_empty() {
return None;
}
let normalized = if as_written.protocol.is_empty() {
whatwg::Parsed::from_utf8(&[b"https://".as_slice(), input].concat())
} else {
whatwg::Parsed::from_utf8(input)
};
let Some(url) = normalized else {
return Some(S3Endpoint {
host_with_path: Box::from(as_written.host_with_path()),
is_http: as_written.is_http(),
});
};
let is_http = OwnedString::new(url.protocol()).eql_comptime(b"http");
// `whatwg::URL::hostname` is the host with its port.
let mut host_with_path = OwnedString::new(url.hostname()).to_utf8_bytes();
let pathname = OwnedString::new(url.pathname());
let path = pathname.to_utf8();
host_with_path.extend_from_slice(strings::without_suffix_comptime(path.slice(), b"/"));
Some(S3Endpoint {
host_with_path: host_with_path.into_boxed_slice(),
is_http,
})
}

pub fn display_protocol(&self) -> &[u8] {
if !self.protocol.is_empty() {
return self.protocol;
Expand Down
57 changes: 57 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,63 @@ 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://user:@127.0.0.1:1",
"http://user@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