Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A Chrome extension content script cannot read another site's response

ErrorCross-Origin Read Blocking (CORB) blocked cross-origin response https://api.example.com/price with MIME type application/json.

Short answer

Since Chrome 85 a content script's request is treated as a request from the web page it runs in, so the page's CORS rules apply and the extension's host permissions do not help. Make the fetch in the extension's service worker, list the host in host_permissions, and send the result back with chrome.runtime.sendMessage.

  • Applies to Chrome 85 and later, Manifest V3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the error in the DevTools console of the web page, not of the extension. If the origin in the message is the website's own origin, the request was made by the content script. Then list what the manifest declares, so you know which cause below fits.

    grep -n -A6 '"content_scripts"\|"host_permissions"\|"background"' manifest.json
  2. 2Match what you see, then jump to its fix

    1. 1The console error names the page's origin (for example https://shop.example.net), not chrome-extension://...The fetch runs inside the content scriptFix
    2. 2After the move, the same CORS error now shows in the service worker's consoleThe host is missing from host_permissionsFix
    3. 3The request succeeds in the service worker log, but the content script never receives the dataThe message listener does not return true, so the reply never arrivesFix

What does the error look like?

  • Cross-Origin Read Blocking (CORB) blocked cross-origin response https://api.example.com/price with MIME type application/json.
  • Access to fetch at 'https://api.example.com/price' from origin 'https://shop.example.net' has been blocked by CORS policy
  • The same fetch works from the extension's popup or service worker but fails from the content script

Cause 1 of 3

The fetch runs inside the content script

1Check

A content script makes its requests on behalf of the page it is injected into, so the page's same-origin rules apply to it. Look for fetch or XMLHttpRequest calls in the files listed under content_scripts.

grep -rn "fetch(\|XMLHttpRequest" src/content/

2Fix

Move the network call to the service worker and let the content script ask for the data with a message. Send a small, fixed request (an id), never a URL, and let the worker build the URL itself.

// content-script.js
chrome.runtime
  .sendMessage({ type: 'queryPrice', itemId: 12345 })
  .then((price) => console.log(price));

// service-worker.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.type !== 'queryPrice') return;
  const url = `https://api.example.com/price?itemId=${encodeURIComponent(request.itemId)}`;
  fetch(url)
    .then((r) => r.json())
    .then((data) => sendResponse(data))
    .catch((err) => sendResponse({ error: String(err) }));
  return true; // keeps the channel open for the async reply
});

3Verify

Reload the extension on chrome://extensions, reload the page and trigger the action. The content script should log the data, and the page console should show no CORS or CORB message.

// content script
chrome.runtime.sendMessage({ type: 'queryPrice', itemId: 12345 }).then(console.log);

Confirmed by chromium.org

Did this fix it?

Cause 2 of 3

The host is missing from host_permissions

1Check

The service worker only escapes the web page's rules for hosts the extension asked for. Open manifest.json and check that the API host is in host_permissions with the exact scheme: HTTP and HTTPS need separate entries.

grep -n -A4 '"host_permissions"' manifest.json

2Fix

Add the API host as a fully qualified host or a match pattern. Anything after the host is ignored.

{
  "manifest_version": 3,
  "host_permissions": [
    "https://api.example.com/"
  ]
}

3Verify

Reload the extension. The service worker's fetch should now return the response body.

// chrome://extensions > your extension > Inspect views: service worker
await (await fetch('https://api.example.com/price?itemId=12345')).text();

Confirmed by developer.chrome.com

Did this fix it?

Cause 3 of 3

The message listener does not return true, so the reply never arrives

1Check

By default sendResponse has to be called synchronously. A fetch finishes later, so the channel has already closed. Look at the end of your onMessage listener for a literal true.

grep -n -B2 -A10 "onMessage.addListener" src/service-worker.js

2Fix

Return a literal true (not just a truthy value) from the listener when you answer asynchronously. Newer Chrome versions also accept a returned promise, and return true keeps working there too.

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.type !== 'queryPrice') return;
  fetch('https://api.example.com/price?itemId=12345')
    .then((r) => r.json())
    .then((data) => sendResponse(data));
  return true;
});

3Verify

The content script's sendMessage call now resolves with the object the worker sent.

chrome.runtime.sendMessage({ type: 'queryPrice' }).then(console.log);

Confirmed by developer.chrome.com

Did this fix it?

How do I stop it coming back?

  • Keep every cross-origin request in the service worker, and keep content scripts to reading and changing the page.
  • Let content scripts send intent-specific messages with an id, never a URL, so a hostile page cannot forge a request to an arbitrary address.
  • Request the smallest host_permissions entries the extension needs, one per scheme and host.
  • Never put fetched text into innerHTML; use textContent or JSON.parse.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

A POST to a Google Apps Script web app returns 405 or a CORS error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.

3 causes, each with a fix

Web apps

The cors-anywhere.herokuapp.com demo proxy stopped working

Missing required request header. Must specify one of: origin,x-requested-with

The public cors-anywhere.herokuapp.com server is only a demo, is rate limited, and since February 2021 requires an opt-in.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.