Skip to content

Lesson 7 of 9 · 26m

Threat Modeling an Extension

Extensions sit between powerful APIs and hostile pages. Model injection, privilege escalation, data theft, and supply-chain risk before shipping.

A browser extension is a privileged component embedded in an adversarial environment. Web pages are untrusted input, websites can be compromised, dependencies ship whatever their maintainer published this week, and your background worker may hold tokens worth stealing. Security is not a final review item; it determines the architecture.

Draw the trust boundaries first

hostile page data ── DOM/URLs/messages ──► content script
content script ── validated envelope ──► background worker
     │                                             │
     ▼                                             ▼
page-visible UI                        privileged APIs + tokens

Anything crossing left-to-right is untrusted. Anything moving right-to-left may leak privileged information. If you cannot state which side of the line a function sits on, it will eventually sit on the wrong side.

XSS in an extension is worse than XSS on a page

Extension pages often have access to powerful APIs. Treat DOM injection in popup and options pages as a critical vulnerability:

// wrong: page-controlled text becomes executable HTML
results.innerHTML = `<li>${finding.title}</li>`;

// better: data stays data
const item = document.createElement("li");
item.textContent = finding.title;
results.append(item);

If rendering markdown, sanitize it with a maintained sanitizer and a strict allowlist. The MV3 CSP already blocks inline scripts in extension pages and disallows eval, but CSP is a safety net, not an excuse to concatenate HTML.

Declarative network APIs reduce privilege

When the feature is blocking or redirecting requests, prefer declarativeNetRequest over broad webRequest interception where possible. The rule artifact looks like this:

[
  {
    "id": 1,
    "priority": 1,
    "action": { "type": "block" },
    "condition": { "urlFilter": "||tracker.example^", "resourceTypes": ["script", "image"] }
  }
]

The declarative model tells the browser what you want without giving the extension a live view of every request body or credential-bearing header. Fewer capabilities mean less to compromise and less to justify in review.

Lock down exposed resources and bridges

web_accessible_resources makes files fetchable by matching pages. Do not expose templates containing secrets, privileged HTML, or implementation details the feature does not require. If the page uses window.postMessage to talk to a main-world bridge, validate event.origin, use a nonce or session channel where possible, and never relay arbitrary messages straight to the background worker.

Supply chain is part of the product

Pin dependencies with a lockfile, review permission changes during dependency updates, and build from source you control. A bundler plugin that injects analytics into the final zip is still your code as far as users and reviewers are concerned. Inspect the artifact, not just the repository.

Field notes

  • Never execute strings with eval, new Function, or inline handlers; treat innerHTML as a review alarm even for apparently internal input.
  • Store OAuth tokens with the narrowest scopes available and revoke them on disconnect.
  • An extension update can silently gain users but not trust; every permission increase deserves a fresh threat review.

Was this lesson helpful?

Sign in to rate