Not able to modify headers in chrome extension using `declarative_net_request` 's `rule_resources` for websockets

298 Views Asked by At

I am trying to modify origin header in a websocket creation process in a chrome extension. I have declarativeNetRequestWithHostAccess in permissions. I am trying to use a rule of type modifyHeaders. It perfectly modifies the headers for fetch() calls but not for websockets. What am I missing?

Here is the rule ie src/rules/example.json:

[
  {
    "id": 2,
    "priority": 1,
    "action": {
      "type": "modifyHeaders",
      "requestHeaders": [
        {
          "header": "origin",
          "operation": "set",
          "value": "https://www.example.ai"
        },
        {
          "header": "referer",
          "operation": "set",
          "value": "https://www.example.ai"
        }
      ]
    },
    "condition": {
      "urlFilter": "example",
      "isUrlFilterCaseSensitive": false,
      "resourceTypes": ["xmlhttprequest", "websocket"]
    }
  }
]

Here is manifest.json

...
...
  "declarative_net_request": {
    "rule_resources": [
      {
        "id": "ruleset_example",
        "enabled": true,
        "path": "src/rules/example.json"
      }
    ]
  },
1

There are 1 best solutions below

0
ishandutta2007 On

As @wOxxOm mentioned,

moving "wss://*/*" from optional_host_permissions to host_permissions solved it.

I guess for optional_host_permissions I had to call chrome.permissions.request({ origins: ['wss://example.com'] }) as well which I was not doing.