diff options
author | 2023-04-28 13:54:09 -0700 | |
---|---|---|
committer | 2023-04-28 13:54:22 -0700 | |
commit | 396416a91fed5166f3c4e9c340a3f2af5367bb78 (patch) | |
tree | 3e7a204f77fb7fd70eda91fe40c8a54fc1bc6aba | |
parent | 4515a6373e22246e2ec6df4475dc63a965bf9c4a (diff) | |
download | bun-396416a91fed5166f3c4e9c340a3f2af5367bb78.tar.gz bun-396416a91fed5166f3c4e9c340a3f2af5367bb78.tar.zst bun-396416a91fed5166f3c4e9c340a3f2af5367bb78.zip |
Fix crash with invalid input in fetch()
-rw-r--r-- | src/bun.js/webcore/response.zig | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index 9c262252a..e7160aee0 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -951,7 +951,7 @@ pub const Fetch = struct { var args = JSC.Node.ArgumentsSlice.from(script_ctx, arguments); defer args.deinit(); - var url: ZigURL = undefined; + var url = ZigURL{}; var first_arg = args.nextEat().?; var body: AnyBlob = AnyBlob{ .Blob = .{}, @@ -1302,6 +1302,18 @@ pub const Fetch = struct { var promise = JSPromise.Strong.init(globalThis); var promise_val = promise.value(); + if (url.isEmpty()) { + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, fetch_error_blank_url, .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); + } + + if (url.protocol.len > 0) { + if (!(url.isHTTP() or url.isHTTPS())) { + const err = JSC.toTypeError(.ERR_INVALID_ARG_VALUE, "protocol must be http: or https:", .{}, ctx); + return JSPromise.rejectedPromiseValue(globalThis, err).asRef(); + } + } + 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(); @@ -1479,7 +1491,7 @@ pub const FetchEvent = struct { var existing_response: ?*Response = arguments[0].?.value().as(Response); if (existing_response == null) { - switch (JSValue.fromRef(arg).jsType()) { + switch (JSValue.fromRef(arg).jsTypeLoose()) { .JSPromise => { this.pending_promise = JSValue.fromRef(arg); }, |