aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Liz <accs@liz3.net> 2023-09-28 01:15:45 +0200
committerGravatar GitHub <noreply@github.com> 2023-09-27 16:15:45 -0700
commit8608b5286ad9cbda51bd086467c728407644d4b2 (patch)
tree090321b8f5c0b8661ce2fd4b315a36bd4c4b0ba4
parenta5f76e690ac49ac273d41feef801f54e4346cef0 (diff)
downloadbun-8608b5286ad9cbda51bd086467c728407644d4b2.tar.gz
bun-8608b5286ad9cbda51bd086467c728407644d4b2.tar.zst
bun-8608b5286ad9cbda51bd086467c728407644d4b2.zip
fix: implement correct behaviour for urls with blob: scheme (#5825)
* fix: implement correct behaviour for urls with blob: scheme Urls using the blob scheme can have special behaviour if their origin itself is a url. This fixes that by parsing the subdomain and if valid and the schemes(protocols) are valid returns its origin. Ive used node.js here a lot to make sure its behaviour is copied 1:1 and enabled the automated tests for it. Fixes https://github.com/oven-sh/bun/issues/5805 * fix: subUrl can be const, we are not modifying it * style: add spaces after `if` keyword
-rw-r--r--src/bun.js/bindings/URLDecomposition.cpp9
-rw-r--r--test/js/web/url/url.test.ts6
2 files changed, 12 insertions, 3 deletions
diff --git a/src/bun.js/bindings/URLDecomposition.cpp b/src/bun.js/bindings/URLDecomposition.cpp
index d3f11b27f..21168f70f 100644
--- a/src/bun.js/bindings/URLDecomposition.cpp
+++ b/src/bun.js/bindings/URLDecomposition.cpp
@@ -35,7 +35,14 @@ String URLDecomposition::origin() const
if (fullURL.protocolIsInHTTPFamily() or fullURL.protocolIsInFTPFamily() or fullURL.protocolIs("ws"_s) or fullURL.protocolIs("wss"_s))
return fullURL.protocolHostAndPort();
-
+ if (fullURL.protocolIsBlob()) {
+ const String& path = fullURL.path().toString();
+ const URL subUrl { URL {}, path };
+ if (subUrl.isValid()) {
+ if (subUrl.protocolIsInHTTPFamily() or subUrl.protocolIsInFTPFamily() or subUrl.protocolIs("ws"_s) or subUrl.protocolIs("wss"_s) or subUrl.protocolIsFile())
+ return subUrl.protocolHostAndPort();
+ }
+ }
return "null"_s;
}
diff --git a/test/js/web/url/url.test.ts b/test/js/web/url/url.test.ts
index 7ddda80ef..f19a132df 100644
--- a/test/js/web/url/url.test.ts
+++ b/test/js/web/url/url.test.ts
@@ -52,8 +52,7 @@ describe("url", () => {
expect(url.protocol).toBe("mailto:");
expect(url.origin).toBe("null");
});
- it.skip("should work with blob urls", () => {
- // TODO
+ it("blob urls", () => {
var url = new URL("blob:https://example.com/1234-5678");
expect(url.protocol).toBe("blob:");
expect(url.origin).toBe("https://example.com");
@@ -72,6 +71,9 @@ describe("url", () => {
url = new URL("blob:ws://example.com");
expect(url.protocol).toBe("blob:");
expect(url.origin).toBe("ws://example.com");
+ url = new URL("blob:file:///folder/else/text.txt");
+ expect(url.protocol).toBe("blob:");
+ expect(url.origin).toBe("file://");
});
it("prints", () => {
expect(Bun.inspect(new URL("https://example.com"))).toBe(`URL {