diff options
author | 2023-02-14 18:43:18 -0500 | |
---|---|---|
committer | 2023-02-14 15:43:18 -0800 | |
commit | 6e1a52691a370058e97ddb84c8f7c49e5bbf1e7e (patch) | |
tree | 22c61cce8c78027edf549203deb41d2abdd9a5b4 /src | |
parent | ef75cd46588ab54e14ad286000ef0357a9b76298 (diff) | |
download | bun-6e1a52691a370058e97ddb84c8f7c49e5bbf1e7e.tar.gz bun-6e1a52691a370058e97ddb84c8f7c49e5bbf1e7e.tar.zst bun-6e1a52691a370058e97ddb84c8f7c49e5bbf1e7e.zip |
Reject with error when invalid fetch() body (#2047)
* Reject with error when invalid fetch() body
Resolves #2014
* Make sure the test actually throws an exception
* Update fetch error paths to return TypeErrors
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/webcore/response.zig | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index de88cfca9..2c3b1ee31 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -538,6 +538,7 @@ pub const Fetch = struct { pub const fetch_error_no_args = "fetch() expects a string but received no arguments."; pub const fetch_error_blank_url = "fetch() URL must not be a blank string."; + pub const fetch_error_unexpected_body = "fetch() request with GET/HEAD/OPTIONS method cannot have body."; const JSTypeErrorEnum = std.enums.EnumArray(JSType, string); pub const fetch_type_error_names: JSTypeErrorEnum = brk: { var errors = JSTypeErrorEnum.initUndefined(); @@ -842,8 +843,8 @@ pub const Fetch = struct { var globalThis = ctx.ptr(); if (arguments.len == 0) { - const fetch_error = fetch_error_no_args; - return JSPromise.rejectedPromiseValue(globalThis, ZigString.init(fetch_error).toErrorInstance(globalThis)).asRef(); + const err = JSC.toTypeError(.ERR_MISSING_ARGS, fetch_error_no_args, .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); } var headers: ?Headers = null; @@ -1046,8 +1047,8 @@ pub const Fetch = struct { var url_zig = proxy_str.getZigString(globalThis); if (url_zig.len == 0) { - const fetch_error = fetch_error_blank_url; - return JSPromise.rejectedPromiseValue(globalThis, ZigString.init(fetch_error).toErrorInstance(globalThis)).asRef(); + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, fetch_error_blank_url, .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); } if (proxy_arg.isNull()) { @@ -1100,8 +1101,8 @@ pub const Fetch = struct { }; if (url_slice.len == 0) { - const fetch_error = fetch_error_blank_url; - return JSPromise.rejectedPromiseValue(globalThis, ZigString.init(fetch_error).toErrorInstance(globalThis)).asRef(); + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, fetch_error_blank_url, .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); } url = ZigURL.parse(url_slice.slice()); @@ -1115,8 +1116,8 @@ pub const Fetch = struct { }; if (url_slice.len == 0) { - const fetch_error = fetch_error_blank_url; - return JSPromise.rejectedPromiseValue(globalThis, ZigString.init(fetch_error).toErrorInstance(globalThis)).asRef(); + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, fetch_error_blank_url, .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); } url = ZigURL.parse(url_slice.slice()); @@ -1131,8 +1132,8 @@ pub const Fetch = struct { }; if (url_slice.len == 0) { - const fetch_error = fetch_error_blank_url; - return JSPromise.rejectedPromiseValue(globalThis, ZigString.init(fetch_error).toErrorInstance(globalThis)).asRef(); + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, fetch_error_blank_url, .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); } url = ZigURL.parse(url_slice.slice()); @@ -1140,12 +1141,18 @@ pub const Fetch = struct { } } else { const fetch_error = fetch_type_error_strings.get(js.JSValueGetType(ctx, arguments[0])); - exception.* = ZigString.init(fetch_error).toErrorInstance(globalThis).asObjectRef(); + const err = JSC.toTypeError(.ERR_INVALID_ARG_TYPE, "{s}", .{fetch_error}, ctx); + exception.* = err.asObjectRef(); return null; } var deferred_promise = JSC.C.JSObjectMakeDeferredPromise(globalThis, null, null, null); + if (!method.hasRequestBody() and body.size() > 0) { + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, fetch_error_unexpected_body, .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); + } + // var resolve = FetchTasklet.FetchResolver.Class.make(ctx: js.JSContextRef, ptr: *ZigType) _ = FetchTasklet.queue( default_allocator, |