Web functionality from an untrusted source

Web functionality loaded from an untrusted source

Description

In 2024, polyfill.io served malicious JavaScript. A change of domain ownership or a compromised host can change third-party scripts, styles or modules. Once loaded, a script runs with the page’s permissions.

Potential impact

  • A script can read accessible cookies, local storage, OAuth tokens, or personal and payment information entered into forms.
  • A compromised script can capture input or replace login and payment forms on every page that loads it.

Remediation

  • Remove resources from untrusted domains such as polyfill.io. Check feature support in the browsers you serve and load only the polyfills they need.
  • Self-host a verified, fixed version when possible. If you need a CDN, use a trusted provider, pin the version and set integrity and crossorigin on resources that support SRI. Calculate the hash from a verified copy.
  • For dynamic loading with import(), createElement or code that fetches and executes scripts, keep the domain allow-list in code or build configuration. Restrict who can change a URL at runtime.
  • Limit CSP’s script-src to 'self' and required sources. For nonce- or 'sha256-...' hash-based policies, consider 'strict-dynamic' where needed. Monitor third-party file changes and review dependencies regularly.

Examples

Before

javascript
// server.js (Node/Express) - before
import express from 'express';
const app = express();

// Configurable through an environment variable, with polyfill.io as the default.
// A compromised supplier can deliver malicious scripts to users.
app.get('/', (req, res) => {
  const polyfillUrl = process.env.POLYFILL_URL || 'https://polyfill.io/v3/polyfill.min.js?features=default';
  res.send(`<!doctype html>
  <html>
    <head>
      <meta charset="utf-8">
      <title>App</title>
      <script src="${polyfillUrl}"></script>
    </head>
    <body>
      <h1>Hello</h1>
    </body>
  </html>`);
});

app.listen(3000);

After

javascript
// server.js (Node/Express) - after
import express from 'express';
const app = express();

// 1) Self-host: deploy a verified fixed-version file to /assets during the build.
app.use('/assets', express.static('public/assets', { immutable: true, maxAge: '1y' }));

app.get('/', (req, res) => {
  // Serve a verified file at a fixed path without relying on an external domain.
  // For an external CDN: pin the exact version and set integrity + crossorigin.
  res.setHeader('Content-Security-Policy', "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'");
  res.send(`<!doctype html>
  <html>
    <head>
      <meta charset="utf-8">
      <title>App</title>
      <script src="/assets/polyfills-4.8.0.min.js" defer></script>
    </head>
    <body>
      <h1>Hello</h1>
    </body>
  </html>`);
});

app.listen(3000);

Verify the polyfill during the build. The example then serves that file from a fixed /assets path and permits only same-origin scripts through CSP.

References