How to add `Referer` header to network calls being made in extensions made using manifest V3?

218 Views Asked by At

I had developed a chrome extension using manifest v3. I am opening an iframe in the content script and from the iframe few network calls are being made. I see that those calls do not have Referer header with them. To resolve this I had used declarativeNetRequest.

I have this in my manifest.json

 "permissions": ["scripting", "activeTab", "contextMenus", "storage", "tabs", "declarativeNetRequestWithHostAccess",
  "declarativeNetRequestFeedback"]

Using this piece of code in my service worker to add Referer header

async function addRefererToRequestHeader() {
  const ruleId = Math.floor(Math.random() * 1000000); // Generate a random number as the rule ID

  const rules = [{
    id: ruleId,
    action: {
      type: 'modifyHeaders',
      requestHeaders: [
        {
          header: 'New-Referer',
          operation: 'set',
          value: 'https://example.com/*',
        },
      ],
    },
    condition: {
      resourceTypes: ['sub_frame'],
      urlFilter: 'https://example.com/*',
    },
  }];

  try {
    await browser.declarativeNetRequest.updateDynamicRules({
      removeRuleIds: [],
      addRules: rules,
    });

    console.log('Rules added successfully.');
  } catch (error) {
    console.error('Error adding rules:', error);
  }
}

But apparently the call to https://example.com/* doesn't include any Referer header. Is it even possible to add Referer header? If so, how? What are the security implications of doing so?

PS: I am using webextension polyyfills browser

0

There are 0 best solutions below