Skip to content

Inconsistency: Execution order for DNR modifyHeaders and webRequest.onBeforeSendHeaders #1004

@cyfung1031

Description

@cyfung1031

What is the correct execution order for DNR modifyHeaders and webRequest.onBeforeSendHeaders?
Why there is difference between Chrome and Firefox?
I filed it as a Firefox bug as well. https://bugzilla.mozilla.org/show_bug.cgi?id=2040251

Steps to reproduce:

This is about the behavioral difference between Firefox MV3 and Chrome MV3.

The demo code is upload to https://github.com/cyfung1031/MV3-Header-Probe

In Extension Event Page / Service Worker.

async function setup() {
  const rule = {
    id: RULE_ID,
    action: {
      type: "modifyHeaders",
      requestHeaders: [
        {
          header: CUSTOM_HEADER_ID,
          operation: "remove",
        },
      ],
    },
    priority: 1,
    condition: {
      resourceTypes: ["xmlhttprequest"],
      tabIds: [-1],
    },
  };

  await updateSessionRules({
    removeRuleIds: [RULE_ID],
    addRules: [rule],
  });

  api.webRequest.onBeforeSendHeaders.addListener(
    (details) => {
      const lastError = getLastError();
      if (lastError) {
        console.error(lastError);
        return;
      }

      if (details.tabId !== -1) {
        return;
      }

      const requestId = details.requestId;

      const requestHeaders = details.requestHeaders ?? [];
      const myID = requestHeaders.find((header) => header.name.toLowerCase() === CUSTOM_HEADER_ID)?.value;

      if (myID) {
        console.log(`[${CUSTOM_HEADER_ID}] The request id for "${myID}" is ${requestId}`);
      } else {
        console.log(`[${CUSTOM_HEADER_ID}] Not visible for request ${requestId}`);
      }

      const myRef = requestHeaders.find((header) => header.name.toLowerCase() === CUSTOM_HEADER_REF)?.value;

      if (myRef) {
        console.log(`[${CUSTOM_HEADER_REF}] The request id for "${myRef}" is ${requestId}`);
      } else {
        console.log(`[${CUSTOM_HEADER_REF}] Not visible for request ${requestId}`);
      }

      console.dir({ requestId, requestHeaders });
    },
    {
      urls: ["<all_urls>"],
      types: ["xmlhttprequest"],
      tabId: -1,
    },
    ["requestHeaders"],
  );
}

async function makeNetworkRequest() {
  const response = await fetch("https://httpbun.com/get", {
    method: "GET",
    headers: {
      [CUSTOM_HEADER_ID]: "A1234",
      [CUSTOM_HEADER_REF]: "B5678",
    },
  });

  console.log("Sample request completed", response.status);
}

(Full coding: https://github.com/cyfung1031/MV3-Header-Probe/raw/refs/heads/main/src/background.js)

at the beginning, do setup()
then call makeNetworkRequest(), e.g. by action.onClicked

Firefox MV3:

[x-my-custom-id] Not visible for request 1211
[x-my-custom-ref] The request id for "B5678" is 1211

Chrome MV3:

[x-my-custom-id] The request id for "A1234" is 214
[x-my-custom-ref] The request id for "B5678" is 214

Metadata

Metadata

Assignees

No one assigned

    Labels

    inconsistencyInconsistent behavior across browsersneeds-triage: chromeChrome needs to assess this issue for the first timeneeds-triage: firefoxFirefox needs to assess this issue for the first timeneeds-triage: safariSafari needs to assess this issue for the first timetopic: dnrRelated to declarativeNetRequesttopic: webRequestIssues related to observing and modifying network requests using the webRequest API.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions