Agent Wranglers

DSK-16Recommended

Treat messages from web pages and content scripts as untrusted

Validate every message the background script receives, and check its sender before acting. Let web pages message the extension only from hosts listed under externally_connectable.

How to do it

Content scripts run inside web pages, and Chrome warns that a hostile page may compromise the process they run in. Treat a message from a content script as a request from a stranger. Check sender.id, sender.origin and the action against a fixed list. Never pass a received string to scripting.executeScript, innerHTML or tabs.update with a URL you did not check. Assume anything you send to a content script can leak to the page. This is the same idea as check the sender and content of every message to the main process.

Examples

JSON example

{
  "externally_connectable": {
    "matches": ["https://app.example.com/*"]
  }
}

How to check

A test sends each message type with an unknown action, a wrong field type and a sender from an unlisted host. The background script refuses all three and changes nothing. externally_connectable.matches lists exact hosts with no *://*/*.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/desktop-apps/browser-extensions/untrusted-extension-messages">Agent Wranglers desktop apps, extensions and CLIs playbook</a>. In code you write, add a comment next to it with the same link.