Manifest V2 -> V3 - programatic modification of response headers

75 views
Skip to first unread message

Kayvan Mojarrad

unread,
Jun 10, 2024, 9:15:51 AMJun 10
to Chromium Extensions
Hi,

I have a manifest v2 Chrome extension that uses blocking chrome.webRequest.onHeadersReceived to rewrite part of the Location header on responses from a matching url.

The use case is to intercept the redirection from an externally hosted login page (aws cognito hosted ui), and to modify the url host to localhost during development.

The redirect url has a query param appended (?code=xxxx) - code parameter is used in authorization_code grant type flow in order to acquire the tokens.

For this reason, I need to change the host in the Location header while preserving the query params.

I have not been able to find a way to do this using the new manifest v3 chrome.declarativeNetRequest.

Would anyone know if there is a way to do the same thing as the code below but in manifest v3?

Thanks

const config = {filter: 'https://example.com/*', rewrite: 'http://localhost:5137'};

chrome.webRequest.onHeadersReceived.addListener(
  rewriteLocationHost,
  {
    urls: [config.filter]
  },
  ['blocking', 'responseHeaders', 'extraHeaders']
)

function rewriteLocationHost(data) {
  if (data.statusCode === 301 || data.statusCode === 302) {
    const header = data.responseHeaders.find(v => v.name.toLowerCase() === 'location')
    if (header) {
      header.value = changeHost(header.value, config.rewrite)
    }
  }
  return {responseHeaders: data.responseHeaders}
}

function changeHost(url, newHost) {
  
const u = new URL(url)
  const i = url.indexOf(u.host) + u.host.length + 1
  const newUrl = newHost + url.substring(i)
  return newUrl
}

woxxom

unread,
Jun 10, 2024, 10:10:42 AMJun 10
to Chromium Extensions, Kayvan Mojarrad
No, this use case is broken in a conventional ManifestV3 extension that's not installed via policy, just like dozens of other advanced applications of webRequestBlocking that were dismissed as negligible by Chromium team. Hopefully, https://crbug.com/40794461 for regex find+replace inside headers of response or request will be implemented soon, given the fact that the developer working on DNR apparently has finished implementing https://crbug.com/40727004.

Kayvan Mojarrad

unread,
Jun 10, 2024, 10:18:37 AMJun 10
to Chromium Extensions, woxxom, Kayvan Mojarrad
Thanks, will keep an eye on https://crbug.com/40794461.
Reply all
Reply to author
Forward
0 new messages