From adf32381877928659c3831ab723dced8fea988c9 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Thu, 12 Dec 2024 12:38:47 -0800 Subject: [PATCH] allow head --- src/bun.js/webcore/response.zig | 5 +++++ src/s3.zig | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index 606fa045eacc4f..fcb70c3cf0027a 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -3230,6 +3230,11 @@ pub const Fetch = struct { // TODO: should we generate the content hash? presigned never uses content-hash, maybe only if a extra option is passed to avoid the cost var result = credentials.s3Request(url.hostname, url.path, method, null) catch |sign_err| { switch (sign_err) { + error.MissingCredentials => { + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, "missing s3 credentials", .{}, ctx); + is_error = true; + return JSPromise.rejectedPromiseValue(globalThis, err); + }, error.InvalidMethod => { const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, "method must be GET, PUT, DELETE when using s3 protocol", .{}, ctx); is_error = true; diff --git a/src/s3.zig b/src/s3.zig index d72e9462eca3f7..72f575373fa683 100644 --- a/src/s3.zig +++ b/src/s3.zig @@ -87,16 +87,20 @@ pub const AWSCredentials = struct { }; pub fn s3Request(this: *const @This(), bucket: []const u8, path: []const u8, method: bun.http.Method, content_hash: ?[]const u8) !SignResult { + if (this.accessKeyId.len == 0 or this.secretAccessKey.len == 0) return error.MissingCredentials; + const method_name = switch (method) { .GET => "GET", .POST, .PUT => "PUT", .DELETE => "DELETE", + .HEAD => "HEAD", else => return error.InvalidMethod, }; if (bucket.len == 0) return error.InvalidPath; // if we allow path.len == 0 it will list the bucket for now we disallow if (path.len == 0) return error.InvalidPath; + var path_buffer: [1024 + 63 + 2]u8 = undefined; // 1024 max key size and 63 max bucket name const normalizedPath = std.fmt.bufPrint(&path_buffer, "/{s}{s}", .{ bucket, if (strings.endsWith(path, "/")) path[0 .. path.len - 1] else path }) catch return error.InvalidPath;