Bug 1806776 - Set scheme for http-first mode redirect instead of find and replace; r=ckerschb,freddyb

Differential Revision: https://phabricator.services.mozilla.com/D167667
This commit is contained in:
june wilde 2023-03-13 14:42:44 +00:00
Родитель d3d2c2d1b5
Коммит b4ec10058e
4 изменённых файлов: 56 добавлений и 17 удалений

Просмотреть файл

@ -504,11 +504,20 @@ nsHTTPSOnlyUtils::PotentiallyDowngradeHttpsFirstRequest(nsIChannel* aChannel,
nsresult rv = aChannel->GetURI(getter_AddRefs(uri)); nsresult rv = aChannel->GetURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, nullptr); NS_ENSURE_SUCCESS(rv, nullptr);
// Only downgrade if the current scheme is (a) https or (b) view-source:https
nsAutoCString spec; nsAutoCString spec;
nsCOMPtr<nsIURI> newURI;
// Only downgrade if the current scheme is (a) https or (b) view-source:https
if (uri->SchemeIs("https")) { if (uri->SchemeIs("https")) {
rv = uri->GetSpec(spec); rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, nullptr); NS_ENSURE_SUCCESS(rv, nullptr);
rv = NS_NewURI(getter_AddRefs(newURI), spec);
NS_ENSURE_SUCCESS(rv, nullptr);
rv = NS_MutateURI(newURI).SetScheme("http"_ns).Finalize(
getter_AddRefs(newURI));
NS_ENSURE_SUCCESS(rv, nullptr);
} else if (uri->SchemeIs("view-source")) { } else if (uri->SchemeIs("view-source")) {
nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(uri); nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(uri);
if (!nestedURI) { if (!nestedURI) {
@ -520,27 +529,23 @@ nsHTTPSOnlyUtils::PotentiallyDowngradeHttpsFirstRequest(nsIChannel* aChannel,
if (!innerURI || !innerURI->SchemeIs("https")) { if (!innerURI || !innerURI->SchemeIs("https")) {
return nullptr; return nullptr;
} }
rv = NS_MutateURI(innerURI).SetScheme("http"_ns).Finalize(
getter_AddRefs(innerURI));
NS_ENSURE_SUCCESS(rv, nullptr);
nsAutoCString innerSpec; nsAutoCString innerSpec;
rv = innerURI->GetSpec(innerSpec); rv = innerURI->GetSpec(innerSpec);
NS_ENSURE_SUCCESS(rv, nullptr); NS_ENSURE_SUCCESS(rv, nullptr);
spec.Append("view-source:"); spec.Append("view-source:");
spec.Append(innerSpec); spec.Append(innerSpec);
rv = NS_NewURI(getter_AddRefs(newURI), spec);
NS_ENSURE_SUCCESS(rv, nullptr);
} else { } else {
return nullptr; return nullptr;
} }
// Change the scheme to http
if (spec.Find("https://") < 0) {
MOZ_ASSERT(false, "how can we end up here not dealing with an https: URI?");
return nullptr;
}
spec.ReplaceSubstring("https://", "http://");
nsCOMPtr<nsIURI> newURI;
rv = NS_NewURI(getter_AddRefs(newURI), spec);
NS_ENSURE_SUCCESS(rv, nullptr);
// Log downgrade to console // Log downgrade to console
NS_ConvertUTF8toUTF16 reportSpec(uri->GetSpecOrDefault()); NS_ConvertUTF8toUTF16 reportSpec(uri->GetSpecOrDefault());
AutoTArray<nsString, 1> params = {reportSpec}; AutoTArray<nsString, 1> params = {reportSpec};

Просмотреть файл

@ -44,10 +44,38 @@ add_task(async function() {
"view-source:http://" "view-source:http://"
); );
await runTest(
"URL with query 'downgrade' should be http and leave query params untouched:",
`view-source:${TEST_PATH_HTTP}/file_downgrade_view_source.sjs?downgrade&https://httpsfirst.com`,
`view-source:${TEST_PATH_HTTP}/file_downgrade_view_source.sjs?downgrade&https://httpsfirst.com`,
"view-source:http://"
);
await runTest( await runTest(
"URL with query 'upgrade' should be https:", "URL with query 'upgrade' should be https:",
`view-source:${TEST_PATH_HTTP}/file_downgrade_view_source.sjs?upgrade`, `view-source:${TEST_PATH_HTTP}/file_downgrade_view_source.sjs?upgrade`,
`view-source:${TEST_PATH_HTTPS}/file_downgrade_view_source.sjs?upgrade`, `view-source:${TEST_PATH_HTTPS}/file_downgrade_view_source.sjs?upgrade`,
"view-source:https://" "view-source:https://"
); );
await runTest(
"URL with query 'upgrade' should be https:",
`view-source:${TEST_PATH_HTTPS}/file_downgrade_view_source.sjs?upgrade`,
`view-source:${TEST_PATH_HTTPS}/file_downgrade_view_source.sjs?upgrade`,
"view-source:https://"
);
await runTest(
"URL with query 'upgrade' should be https and leave query params untouched:",
`view-source:${TEST_PATH_HTTP}/file_downgrade_view_source.sjs?upgrade&https://httpsfirst.com`,
`view-source:${TEST_PATH_HTTPS}/file_downgrade_view_source.sjs?upgrade&https://httpsfirst.com`,
"view-source:https://"
);
await runTest(
"URL with query 'upgrade' should be https and leave query params untouched:",
`view-source:${TEST_PATH_HTTPS}/file_downgrade_view_source.sjs?upgrade&https://httpsfirst.com`,
`view-source:${TEST_PATH_HTTPS}/file_downgrade_view_source.sjs?upgrade&https://httpsfirst.com`,
"view-source:https://"
);
}); });

Просмотреть файл

@ -53,6 +53,12 @@ add_task(async function() {
"http://" "http://"
); );
await runPrefTest(
"http://httpsfirst.com/?https://httpsfirst.com",
"Should downgrade after error and leave query params untouched.",
"http://httpsfirst.com/?https://httpsfirst.com"
);
await runPrefTest( await runPrefTest(
"http://domain.does.not.exist", "http://domain.does.not.exist",
"Should not downgrade on dnsNotFound error.", "Should not downgrade on dnsNotFound error.",

Просмотреть файл

@ -5,26 +5,26 @@ function handleRequest(request, response) {
response.setHeader("Cache-Control", "no-cache", false); response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Content-Type", "text/html", false); response.setHeader("Content-Type", "text/html", false);
let query = request.queryString; let query = request.queryString.split("&");
let scheme = request.scheme; let scheme = request.scheme;
if (scheme === "https") { if (scheme === "https") {
if (query === "downgrade") { if (query.includes("downgrade")) {
response.setStatusLine("1.1", 400, "Bad Request"); response.setStatusLine("1.1", 400, "Bad Request");
response.write("Bad Request\n"); response.write("Bad Request\n");
return; return;
} }
if (query === "upgrade") { if (query.includes("upgrade")) {
response.write("view-source:https://"); response.write("view-source:https://");
return; return;
} }
} }
if (scheme === "http" && query === "downgrade") { if (scheme === "http" && query.includes("downgrade")) {
response.write("view-source:http://"); response.write("view-source:http://");
return; return;
} }
// We should arrive here when the redirection was downraded successful // We should arrive here when the redirection was downraded successful
response.write("unexpected"); response.write("unexpected scheme and query given");
} }