RegexSubstitution in declarativeNetRequest

957 views
Skip to first unread message

Browser Extenstion

unread,
Jul 18, 2022, 7:42:55 AM7/18/22
to Chromium Extensions
I'm using declarativeNetRequest to redirect some domain. This is example of the rule:
[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "https://new-domain.com?string=\\0"  
      }
    },
    "condition": {
      "regexFilter": "^https://domain.com(.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]

The format of url in condition regexFilter is (it always contains word "string"): https://domain.com/some_data?string=........
So I want to redirect with regexSubstitution the data comes after string=.
In other words:
https://domain.com/new/test/1?string=my_test_with_regex&success=hope_true --> https://new-domain.com?string=my_test_with_regex&success=hope_true

Is it possible?
If yes, how I can use regex with "\\0" variable?

wOxxOm

unread,
Jul 18, 2022, 11:11:29 AM7/18/22
to Chromium Extensions, Browser Extenstion
\\0 inserts everything matched by regexFilter i.e. in your case the entire source URL.

You should use \\1 to insert the first parenthesized group. However, since regex is slow and very restricted in this API it's best to apply a faster condition first via requestDomains (Chrome 101+) and urlFilter:

[{
  "id": 1,

  "action": {
    "type": "redirect",
    "redirect": {
      "transform": {
        "host": "new-domain.com",
        "path": ""
      }
    },
  },
  "condition": {
    "urlFilter": "?string=",
    "resourceTypes": ["main_frame"],
    "requestDomains": ["domain.com"]
  }
}]


Note that this example redirects only main_frame requests i.e. navigation, not XHR or fetch or images.

Browser Extenstion

unread,
Jul 18, 2022, 4:31:14 PM7/18/22
to Chromium Extensions, wOxxOm, Browser Extenstion
This solution works only if string is a first argument (?string=)
It not works for &string=.

In the question I wrote that I need to redirect to another new-domain. But, actually the "new domain" comes from the string.
It something like this:
https://domain.com?param&string=https://some-other-domain.com/additional_information.

So I need to redirect the request to https://some-other-domain.com/additional_information.

wOxxOm

unread,
Jul 18, 2022, 5:04:02 PM7/18/22
to Chromium Extensions, Browser Extenstion, wOxxOm
Then you'll need to use regex:

[{
  "id": 1,
  "action": {
    "type": "redirect",
    "redirect": {
      "regexSubstitution": "\\1"
    }
  },
  "condition": {
    "regexFilter": "[?&]string=([^&]+)",

    "resourceTypes": ["main_frame"],
    "requestDomains": ["domain.com"]
  }
}]

Note that this will only work if the parameter value is not percent-encoded, otherwise the URL will be invalid like https%3a%2f%2fother-domain.

Browser Extenstion

unread,
Jul 21, 2022, 10:38:53 AM7/21/22
to Chromium Extensions, wOxxOm, Browser Extenstion
Unfortunately, it does not work.


  "id": 1,
  "action": {
    "type": "redirect",
    "redirect": {
      "regexSubstitution": "\\1"
    }
  },
  "condition": {
    "regexFilter": "[?&]string=([^&]+)",

    "resourceTypes": ["main_frame"],
    "requestDomains": ["example.com"]
  }


https://example.com/?string=https://google.com --> https://example.com/https://google.com
And not to https://google.com as expected.

wOxxOm

unread,
Jul 21, 2022, 3:21:34 PM7/21/22
to Chromium Extensions, Browser Extenstion, wOxxOm
It means regexFilter should match the entire source URL e.g. "^.*?[?&]string=([^&]+).*"
Reply all
Reply to author
Forward
0 new messages