Skip to content

Commit

Permalink
Cherry-pick c9d2edf. rdar://problem/110134509
Browse files Browse the repository at this point in the history
    [ios] Prioritize text/plain MIME type over file extension for XML file types
    https://bugs.webkit.org/show_bug.cgi?id=257299
    rdar://107379119

    Reviewed by David Kilzer.

    When we receive a file with a text/plain resource, we try harder to find a
    better match for the file content, but this can potentially lead to an issue
    with some file types like XML. Therefore, if the server says that a file should
    be treated as text/plain, and we decide that it is actually XML or SVG, we
    prefer using text/plain.

    * LayoutTests/http/tests/mime/resources/.htaccess: Added.
    * LayoutTests/http/tests/mime/resources/svg-with-html.svg: Added.
    * LayoutTests/http/tests/mime/resources/xml-with-html.xml: Added.
    * LayoutTests/http/tests/mime/svg-with-html-expected.txt: Added.
    * LayoutTests/http/tests/mime/svg-with-html.html: Added.
    * LayoutTests/http/tests/mime/xml-with-html-expected.txt: Added.
    * LayoutTests/http/tests/mime/xml-with-html.html: Added.
    * Source/WebCore/platform/network/ios/WebCoreURLResponseIOS.mm:
    (WebCore::shouldPreferTextPlainMIMEType):
    (WebCore::adjustMIMETypeIfNecessary):

    Canonical link: https://commits.webkit.org/259548.786@safari-7615-branch

Identifier: 245886.884@safari-7613.4.1.0-branch
  • Loading branch information
sysrqb authored and MyahCobbs committed Jun 22, 2023
1 parent 4795348 commit fb80de3
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 1 deletion.
6 changes: 6 additions & 0 deletions LayoutTests/http/tests/mime/resources/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Files xml-with-html.xml>
ForceType "text/plain"
</Files>
<Files svg-with-html.svg>
ForceType "text/plain"
</Files>
4 changes: 4 additions & 0 deletions LayoutTests/http/tests/mime/resources/svg-with-html.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions LayoutTests/http/tests/mime/resources/xml-with-html.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version=\'1.0\' encoding=\'utf-8\'?>
<html xmlns:html="http://www.w3.org/1999/xhtml">
<html:script>console.log("Fail");</html:script>
</html>
2 changes: 2 additions & 0 deletions LayoutTests/http/tests/mime/svg-with-html-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
svg-with-html.svg has MIME type text/plain

24 changes: 24 additions & 0 deletions LayoutTests/http/tests/mime/svg-with-html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<script>
if (window.testRunner) {
testRunner.waitUntilDone();
testRunner.dumpResourceResponseMIMETypes();
testRunner.dumpAsText();
}
</script>
<body>
<script>
function runTests() {
let iframe = document.createElement("iframe");
iframe.src = 'resources/svg-with-html.svg';
if (window.testRunner) {
iframe.onerror = testRunner.notifyDone();
iframe.onload = testRunner.notifyDone();
}
document.body.appendChild(iframe);
}
runTests();
</script>
</body>
</html>

2 changes: 2 additions & 0 deletions LayoutTests/http/tests/mime/xml-with-html-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
xml-with-html.xml has MIME type text/plain

24 changes: 24 additions & 0 deletions LayoutTests/http/tests/mime/xml-with-html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<script>
if (window.testRunner) {
testRunner.waitUntilDone();
testRunner.dumpResourceResponseMIMETypes();
testRunner.dumpAsText();
}
</script>
<body>
<script>
function runTests() {
let iframe = document.createElement("iframe");
iframe.src = 'resources/xml-with-html.xml';
if (window.testRunner) {
iframe.onerror = testRunner.notifyDone();
iframe.onload = testRunner.notifyDone();
}
document.body.appendChild(iframe);
}
runTests();
</script>
</body>
</html>

7 changes: 6 additions & 1 deletion Source/WebCore/platform/network/ios/WebCoreURLResponseIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

namespace WebCore {

static inline bool shouldPreferTextPlainMIMEType(const String& mimeType, const String& proposedMIMEType)
{
return ("text/plain"_s == mimeType) && ((proposedMIMEType == "text/xml"_s) || (proposedMIMEType == "application/xml"_s) || (proposedMIMEType == "image/svg+xml"_s));
}

void adjustMIMETypeIfNecessary(CFURLResponseRef response, bool isMainResourceLoad)
{
auto type = CFURLResponseGetMIMEType(response);
Expand All @@ -63,7 +68,7 @@ void adjustMIMETypeIfNecessary(CFURLResponseRef response, bool isMainResourceLoa
updatedType = (__bridge CFStringRef)quickLookType.get();
else if (auto extension = filePathExtension(response))
updatedType = preferredMIMETypeForFileExtensionFromUTType(extension.get());
if (updatedType && (!type || CFStringCompare(type, updatedType.get(), kCFCompareCaseInsensitive) != kCFCompareEqualTo)) {
if (updatedType && !shouldPreferTextPlainMIMEType(type, updatedType.get()) && (!type || CFStringCompare(type, updatedType.get(), kCFCompareCaseInsensitive) != kCFCompareEqualTo)) {
CFURLResponseSetMIMEType(response, updatedType.get());
return;
}
Expand Down

0 comments on commit fb80de3

Please sign in to comment.