N
N
Nadim Zakirov2022-04-07 04:53:21
JavaScript
Nadim Zakirov, 2022-04-07 04:53:21

How to pass Basic Auth via declarativeNetRequest?

Purely hypothetically, nothing prevents me from creating a rule where a header is added for all outgoing requests:

Proxy-Authorization: Basic {мой логин:пароль в base64 кодировке}

I plan to use declarativeNetRequest for this, which can be used with Manifest V3 , which is important for me.

I tried writing a test extension. My mainifest.json file is the following:
{
  
  "name" : "Тест",
  
  "manifest_version" : 3,
  
  "version" : "1",
  
  "description" : "Тестовое расширение",
  
  "permissions" : [ "declarativeNetRequest" ],
  
  "host_permissions": [ "*://*/*" ],
  
  "declarative_net_request": {
    "rule_resources": [{
      "id": "ruleset_1",
      "enabled": true,
      "path": "rules.json"
    }]
  }
  
}

In the rules,json file , I added the rule I needed:
[
  {
  "id": 1,
  "priority": 2,
  "action": {
    "type": "modifyHeaders",
    "requestHeaders": [
      { "header": "Proxy-Authorization", "operation": "set", "value": "Basic ajHJjdkjHdsdkjsd" }
    ]
  },
  "condition": { "urlFilter": "*", "resourceTypes": [ "main_frame", "sub_frame", "stylesheet", "script", "image", "font", "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket", "webtransport", "webbundle", "other" ] }
  }
]

I installed the extension, went to my site, I see in the console that the title is successfully leaving. It would seem success, but not everything is so simple. If you install an http proxy in the browser through the extension worker, then the Proxy-Authorization header is stupidly not sent for the proxy! As a result, this infuriating authentication window pops up.

How can I force the browser to send this header, including when making requests through a proxy?

It seems to me that the problem is either in my file with the rule, maybe I somehow specify condition incorrectly and as a result the rule does not cover all types of requests, or I do not correctly specify host_permissions in the manifestbecause of which, again, the rule cannot work for requests through a proxy. Unfortunately, I don’t know what to do next, I have already tried almost all the solutions to the problem that came to my mind. Tell me the solution, who knows.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question