Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 0e722cf

Browse files
committed
Bug 1948543 - Do not use file extension when sniffing mime type r=necko-reviewers,willdurand,kershaw,devtools-reviewers,bomsy,geckoview-reviewers,nalexander
This patch adds a network.sniff.use_extension pref. When false, the file extension of a URL will only be used to sniff file:// Differential Revision: https://phabricator.services.mozilla.com/D246585
1 parent 993235b commit 0e722cf

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

browser/components/extensions/test/browser/browser_ext_search_favicon.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ SearchTestUtils.init(this);
2626
const FAKE_ICON_DATA = "RmFrZSBpY29uIGRhdGE=";
2727

2828
// Base64-encoded "HTTP icon data".
29-
const HTTP_ICON_DATA = "SFRUUCBpY29uIGRhdGE=";
29+
const HTTP_ICON_DATA =
30+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC";
3031
const HTTP_ICON_URL = "http://example.org/ico.png";
3132

3233
const IMAGE_DATA_URI =
@@ -37,6 +38,7 @@ add_setup(async () => {
3738
hosts: ["example.org"],
3839
});
3940
server.registerPathHandler("/ico.png", (request, response) => {
41+
response.setHeader("Content-Type", "image/png");
4042
response.write(atob(HTTP_ICON_DATA));
4143
});
4244

devtools/client/debugger/test/mochitest/browser_dbg-pretty-print-line-breaks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
requestLongerTimeout(2);
1010

1111
const httpServer = createTestHTTPServer();
12-
httpServer.registerContentType("html", "text/html");
1312

1413
httpServer.registerPathHandler("/doc_line_breaks.html", (request, response) => {
1514
response.setStatusLine(request.httpVersion, 200, "OK");
15+
response.setHeader("Content-Type", "text/html");
1616
response.write(
1717
`TEST with line breaks\r\n<script>(function(){\r\n})('test')</script>`
1818
);

mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/TestServer.kt

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.mozilla.geckoview.test.util
33
import android.content.Context
44
import android.content.res.AssetManager
55
import android.os.SystemClock
6-
import android.webkit.MimeTypeMap
76
import com.koushikdutta.async.ByteBufferList
87
import com.koushikdutta.async.http.server.AsyncHttpServer
98
import com.koushikdutta.async.http.server.AsyncHttpServerRequest
@@ -14,6 +13,7 @@ import org.json.JSONObject
1413
import java.io.FileNotFoundException
1514
import java.math.BigInteger
1615
import java.security.MessageDigest
16+
import java.util.Locale
1717
import java.util.Random
1818
import java.util.Vector
1919

@@ -54,10 +54,31 @@ class TestServer @JvmOverloads constructor(
5454

5555
val assetsCallback = HttpServerRequestCallback { request, response ->
5656
try {
57-
val mimeType = MimeTypeMap.getSingleton()
58-
.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(request.path))
59-
val name = request.path.substring("/assets/".count())
60-
val asset = assets.open(name).readBytes()
57+
val fileName = request.path.substring("/assets/".length)
58+
val asset = assets.open(fileName).readBytes()
59+
60+
val extension = fileName.substringAfterLast('.', "")
61+
val mimeType = when (extension.lowercase(Locale.ROOT)) {
62+
"html", "htm" -> "text/html"
63+
"js" -> "application/javascript"
64+
"css" -> "text/css"
65+
"json" -> "application/json"
66+
"webmanifest" -> "application/manifest+json"
67+
"png" -> "image/png"
68+
"jpg", "jpeg" -> "image/jpeg"
69+
"gif" -> "image/gif"
70+
"webp" -> "image/webp"
71+
"svg" -> "image/svg+xml"
72+
"ico" -> "image/x-icon"
73+
"mp4" -> "video/mp4"
74+
"webm" -> "video/webm"
75+
"mp3" -> "audio/mpeg"
76+
"pdf" -> "application/pdf"
77+
"sjs" -> "application/javascript"
78+
else -> "application/octet-stream"
79+
}
80+
81+
response.headers.set("Content-Type", mimeType)
6182

6283
customHeaders?.forEach { (header, value) ->
6384
response.headers.set(header, value)

modules/libpref/init/StaticPrefList.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13373,6 +13373,14 @@
1337313373
value: false
1337413374
mirror: always
1337513375

13376+
# If set to true, Firefox will use the file extension of a URL
13377+
# to sniff its content type. When false, nsUnknownDecoder will
13378+
# use the file extension only for file URLs
13379+
- name: network.sniff.use_extension
13380+
type: RelaxedAtomicBool
13381+
value: false
13382+
mirror: always
13383+
1337613384
# Enables the predictive service.
1337713385
- name: network.predictor.enabled
1337813386
type: bool

netwerk/streamconv/converters/nsUnknownDecoder.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nsQueryObject.h"
2525
#include "nsComponentManagerUtils.h"
2626
#include "nsServiceManagerUtils.h"
27+
#include "mozilla/StaticPrefs_network.h"
2728

2829
#include <algorithm>
2930

@@ -480,9 +481,14 @@ void nsUnknownDecoder::DetermineContentType(nsIRequest* aRequest) {
480481
return;
481482
}
482483

484+
nsCOMPtr<nsIURI> uri;
485+
NS_GetFinalChannelURI(channel, getter_AddRefs(uri));
486+
483487
// We don't know what this is yet. Before we just give up, try
484488
// the URI from the request.
485-
if (SniffURI(aRequest)) {
489+
if ((StaticPrefs::network_sniff_use_extension() ||
490+
(uri && uri->SchemeIs("file"))) &&
491+
SniffURI(aRequest)) {
486492
#ifdef DEBUG
487493
MutexAutoLock lock(mMutex);
488494
NS_ASSERTION(!mContentType.IsEmpty(),
@@ -558,7 +564,7 @@ bool nsUnknownDecoder::SniffForHTML(nsIRequest* aRequest) {
558564

559565
bool nsUnknownDecoder::SniffForXML(nsIRequest* aRequest) {
560566
// First see whether we can glean anything from the uri...
561-
if (!SniffURI(aRequest)) {
567+
if (!StaticPrefs::network_sniff_use_extension() || !SniffURI(aRequest)) {
562568
// Oh well; just generic XML will have to do
563569
MutexAutoLock lock(mMutex);
564570
mContentType = TEXT_XML;

0 commit comments

Comments
 (0)