Bug 1574174 - add immutable guard for fetch response headers. r=edenchuang,necko-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D162209
This commit is contained in:
sunil mayya 2022-11-25 12:27:15 +00:00
Родитель 3cc7287baf
Коммит 6eabd0d3de
2 изменённых файлов: 27 добавлений и 0 удалений

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

@ -649,6 +649,13 @@ void MainThreadFetchResolver::OnResponseAvailableInternal(
}
mResponse = new Response(go, std::move(aResponse), mSignalImpl);
// response headers received from the network should be immutable
// all response header settings must be done before this point
// see Bug 1574174
ErrorResult result;
mResponse->Headers_()->SetGuard(HeadersGuardEnum::Immutable, result);
MOZ_ASSERT(!result.Failed());
BrowsingContext* bc = inner ? inner->GetBrowsingContext() : nullptr;
bc = bc ? bc->Top() : nullptr;
if (bc && bc->IsLoading()) {
@ -725,6 +732,14 @@ class WorkerFetchResponseRunnable final : public MainThreadWorkerRunnable {
RefPtr<Response> response =
new Response(global, mInternalResponse.clonePtr(),
mResolver->GetAbortSignalForTargetThread());
// response headers received from the network should be immutable,
// all response header settings must be done before this point
// see Bug 1574174
ErrorResult result;
response->Headers_()->SetGuard(HeadersGuardEnum::Immutable, result);
MOZ_ASSERT(!result.Failed());
promise->MaybeResolve(response);
} else {
if (fetchObserver) {

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

@ -11,6 +11,18 @@ test(function() {
assert_true(responseError.headers.entries().next().done, "Headers should be empty");
}, "Check response returned by static method error()");
promise_test (async function() {
let response = await fetch("../resources/data.json");
try {
response.headers.append('name', 'value');
} catch (e) {
assert_equals(e.constructor.name, "TypeError");
}
assert_not_equals(response.headers.get("name"), "value", "response headers should be immutable");
}, "Ensure response headers are immutable");
test(function() {
const headers = Response.error().headers;