Align with Fetch on data: URLs
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 5 Dec 2018 09:26:27 +0000 (09:26 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 5 Dec 2018 09:26:27 +0000 (09:26 +0000)
https://bugs.webkit.org/show_bug.cgi?id=182325

Patch by Rob Buis <rbuis@igalia.com> on 2018-12-05
Reviewed by Alex Christensen.

LayoutTests/imported/w3c:

Update improved test expectations.

* web-platform-tests/fetch/api/basic/scheme-data.any-expected.txt:
* web-platform-tests/fetch/api/basic/scheme-data.any.worker-expected.txt:

Source/WebCore:

Do not accept data URLs that do not contain a comma
character, as specified in the relevant specs [1, 2].

Behavior matches Firefox and Chrome.

Test: web-platform-tests/fetch/api/basic/scheme-data.any.html

[1] https://tools.ietf.org/html/rfc2397
[2] https://fetch.spec.whatwg.org/#data-url-processor

* platform/network/DataURLDecoder.cpp:
(WebCore::DataURLDecoder::parseMediaType):
(WebCore::DataURLDecoder::DecodeTask::DecodeTask):
(WebCore::DataURLDecoder::DecodeTask::process):
(WebCore::DataURLDecoder::createDecodeTask):
(WebCore::DataURLDecoder::decode):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@238890 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/imported/w3c/ChangeLog
LayoutTests/imported/w3c/web-platform-tests/fetch/api/basic/scheme-data.any-expected.txt
LayoutTests/imported/w3c/web-platform-tests/fetch/api/basic/scheme-data.any.worker-expected.txt
Source/WebCore/ChangeLog
Source/WebCore/platform/network/DataURLDecoder.cpp

index f61b31a..6c7a978 100644 (file)
@@ -1,3 +1,15 @@
+2018-12-05  Rob Buis  <rbuis@igalia.com>
+
+        Align with Fetch on data: URLs
+        https://bugs.webkit.org/show_bug.cgi?id=182325
+
+        Reviewed by Alex Christensen.
+
+        Update improved test expectations.
+
+        * web-platform-tests/fetch/api/basic/scheme-data.any-expected.txt:
+        * web-platform-tests/fetch/api/basic/scheme-data.any.worker-expected.txt:
+
 2018-11-30  Ryosuke Niwa  <rniwa@webkit.org>
 
         title attribute on style & link elements should be ignored inside a shadow tree
index 618c844..65b2f32 100644 (file)
@@ -1,3 +1,4 @@
+CONSOLE MESSAGE: Fetch API cannot load data:notAdataUrl.com.
 
 PASS Fetching data:,response%27s%20body is OK 
 PASS Fetching data:,response%27s%20body is OK (same-origin) 
@@ -6,5 +7,5 @@ PASS Fetching data:text/plain;base64,cmVzcG9uc2UncyBib[...] is OK
 PASS Fetching data:image/png;base64,cmVzcG9uc2UncyBib2[...] is OK 
 PASS Fetching [POST] data:,response%27s%20body is OK 
 PASS Fetching [HEAD] data:,response%27s%20body is OK 
-FAIL Fetching [GET] data:notAdataUrl.com is KO assert_unreached: Should have rejected: undefined Reached unreachable code
+PASS Fetching [GET] data:notAdataUrl.com is KO 
 
index 618c844..c75f064 100644 (file)
@@ -6,5 +6,5 @@ PASS Fetching data:text/plain;base64,cmVzcG9uc2UncyBib[...] is OK
 PASS Fetching data:image/png;base64,cmVzcG9uc2UncyBib2[...] is OK 
 PASS Fetching [POST] data:,response%27s%20body is OK 
 PASS Fetching [HEAD] data:,response%27s%20body is OK 
-FAIL Fetching [GET] data:notAdataUrl.com is KO assert_unreached: Should have rejected: undefined Reached unreachable code
+PASS Fetching [GET] data:notAdataUrl.com is KO 
 
index 433e236..ecc907c 100644 (file)
@@ -1,3 +1,27 @@
+2018-12-05  Rob Buis  <rbuis@igalia.com>
+
+        Align with Fetch on data: URLs
+        https://bugs.webkit.org/show_bug.cgi?id=182325
+
+        Reviewed by Alex Christensen.
+
+        Do not accept data URLs that do not contain a comma
+        character, as specified in the relevant specs [1, 2].
+
+        Behavior matches Firefox and Chrome.
+
+        Test: web-platform-tests/fetch/api/basic/scheme-data.any.html
+
+        [1] https://tools.ietf.org/html/rfc2397
+        [2] https://fetch.spec.whatwg.org/#data-url-processor
+
+        * platform/network/DataURLDecoder.cpp:
+        (WebCore::DataURLDecoder::parseMediaType):
+        (WebCore::DataURLDecoder::DecodeTask::DecodeTask):
+        (WebCore::DataURLDecoder::DecodeTask::process):
+        (WebCore::DataURLDecoder::createDecodeTask):
+        (WebCore::DataURLDecoder::decode):
+
 2018-12-05  Frederic Wang  <fwang@igalia.com>
 
         Unreviewed build fix.
index 4605bea..59ddf07 100644 (file)
@@ -45,22 +45,56 @@ static WorkQueue& decodeQueue()
     return queue;
 }
 
+static Result parseMediaType(const String& mediaType)
+{
+    auto mimeType = extractMIMETypeFromMediaType(mediaType);
+    auto charset = extractCharsetFromMediaType(mediaType);
+
+    // https://tools.ietf.org/html/rfc2397
+    // If <mediatype> is omitted, it defaults to text/plain;charset=US-ASCII. As a shorthand,
+    // "text/plain" can be omitted but the charset parameter supplied.
+    if (mimeType.isEmpty()) {
+        mimeType = "text/plain"_s;
+        if (charset.isEmpty())
+            charset = "US-ASCII"_s;
+    }
+    return { mimeType, charset, !mediaType.isEmpty() ? mediaType : "text/plain;charset=US-ASCII", nullptr };
+}
+
 struct DecodeTask {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    DecodeTask(const String& urlString, StringView&& encodedData, bool isBase64, const ScheduleContext& scheduleContext, DecodeCompletionHandler&& completionHandler, Result&& result)
+    DecodeTask(const String& urlString, const ScheduleContext& scheduleContext, DecodeCompletionHandler&& completionHandler)
         : urlString(urlString.isolatedCopy())
-        , encodedData(WTFMove(encodedData))
-        , isBase64(isBase64)
         , scheduleContext(scheduleContext)
         , completionHandler(WTFMove(completionHandler))
-        , result(WTFMove(result))
     {
     }
 
+    bool process()
+    {
+        if (urlString.find(',') == notFound)
+            return false;
+        const char dataString[] = "data:";
+        const char base64String[] = ";base64";
+
+        ASSERT(urlString.startsWith(dataString));
+
+        size_t headerEnd = urlString.find(',', strlen(dataString));
+        size_t encodedDataStart = headerEnd == notFound ? headerEnd : headerEnd + 1;
+
+        encodedData = StringView(urlString).substring(encodedDataStart);
+        auto header = StringView(urlString).substring(strlen(dataString), headerEnd - strlen(dataString));
+        isBase64 = header.endsWithIgnoringASCIICase(StringView(base64String));
+        auto mediaType = (isBase64 ? header.substring(0, header.length() - strlen(base64String)) : header).toString();
+        result = parseMediaType(mediaType);
+
+        return true;
+    }
+
     const String urlString;
-    const StringView encodedData;
-    const bool isBase64;
+    StringView encodedData;
+    bool isBase64 { false };
     const ScheduleContext scheduleContext;
     const DecodeCompletionHandler completionHandler;
 
@@ -113,45 +147,12 @@ private:
 
 #endif // HAVE(RUNLOOP_TIMER)
 
-static Result parseMediaType(const String& mediaType)
-{
-    auto mimeType = extractMIMETypeFromMediaType(mediaType);
-    auto charset = extractCharsetFromMediaType(mediaType);
-
-    // https://tools.ietf.org/html/rfc2397
-    // If <mediatype> is omitted, it defaults to text/plain;charset=US-ASCII. As a shorthand,
-    // "text/plain" can be omitted but the charset parameter supplied.
-    if (mimeType.isEmpty()) {
-        mimeType = "text/plain"_s;
-        if (charset.isEmpty())
-            charset = "US-ASCII"_s;
-    }
-    return { mimeType, charset, !mediaType.isEmpty() ? mediaType : "text/plain;charset=US-ASCII", nullptr };
-}
-
 static std::unique_ptr<DecodeTask> createDecodeTask(const URL& url, const ScheduleContext& scheduleContext, DecodeCompletionHandler&& completionHandler)
 {
-    const char dataString[] = "data:";
-    const char base64String[] = ";base64";
-
-    auto urlString = url.string();
-    ASSERT(urlString.startsWith(dataString));
-
-    size_t headerEnd = urlString.find(',', strlen(dataString));
-    size_t encodedDataStart = headerEnd == notFound ? headerEnd : headerEnd + 1;
-
-    auto encodedData = StringView(urlString).substring(encodedDataStart);
-    auto header = StringView(urlString).substring(strlen(dataString), headerEnd - strlen(dataString));
-    bool isBase64 = header.endsWithIgnoringASCIICase(StringView(base64String));
-    auto mediaType = (isBase64 ? header.substring(0, header.length() - strlen(base64String)) : header).toString();
-
     return std::make_unique<DecodeTask>(
-        urlString,
-        WTFMove(encodedData),
-        isBase64,
+        url.string(),
         scheduleContext,
-        WTFMove(completionHandler),
-        parseMediaType(mediaType)
+        WTFMove(completionHandler)
     );
 }
 
@@ -184,10 +185,12 @@ void decode(const URL& url, const ScheduleContext& scheduleContext, DecodeComple
     ASSERT(url.protocolIsData());
 
     decodeQueue().dispatch([decodeTask = createDecodeTask(url, scheduleContext, WTFMove(completionHandler))]() mutable {
-        if (decodeTask->isBase64)
-            decodeBase64(*decodeTask);
-        else
-            decodeEscaped(*decodeTask);
+        if (decodeTask->process()) {
+            if (decodeTask->isBase64)
+                decodeBase64(*decodeTask);
+            else
+                decodeEscaped(*decodeTask);
+        }
 
 #if HAVE(RUNLOOP_TIMER)
         DecodingResultDispatcher::dispatch(WTFMove(decodeTask));