Open Bug 2040251 Opened 5 days ago Updated 19 hours ago

Inconsistent MV3 behavior - DNR modifyHeaders with browser.webRequest.onBeforeSendHeaders

Categories

(WebExtensions :: Compatibility, defect)

Firefox 151
defect

Tracking

(firefox151 affected, firefox152 affected, firefox153 affected)

Tracking Status
firefox151 --- affected
firefox152 --- affected
firefox153 --- affected

People

(Reporter: cyfung1031, Unassigned)

Details

Attachments

(1 file)

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);
}

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

Actual results:

Firefox MV3:

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

Expected results:

Chrome MV3:

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

The Bugbug bot thinks this bug should belong to the 'WebExtensions::Untriaged' component, but is not confident enough to move the bug to that component.

Component: Untriaged → General
Component: General → Compatibility
Product: Firefox → WebExtensions

I reproduced the issue on the latest Nightly (153.0a1/20260521211559), Beta (152.0b1/20260520111910) and Release (151.0.1/20260520211922) under Windows 11 and Ubuntu 25.10.
For comparison, per the STR, I checked on the latest Google Chrome (148.0.7778.179) as well, where the issue does not occur.

In Firefox, clicking the extension button will log the following in the extension console:

[x-my-custom-dynamic] Not visible for request 552
[x-my-custom-session] Not visible for request 552
[x-my-custom-static] Not visible for request 552
[x-my-custom-unmatched] Value is "UNMATCHED" for request 552

In Chrome, doing the same, logs the following:

[x-my-custom-dynamic] Value is "DYNAMIC" for request 800
[x-my-custom-session] Value is "SESSION" for request 800
[x-my-custom-static] Value is "STATIC" for request 800
[x-my-custom-unmatched] Value is "UNMATCHED" for request 800
Status: UNCONFIRMED → NEW
Ever confirmed: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: