Safeguard
Security Guides•

Self-host third-party JavaScript or load it from a CDN?

Self-hosting gives you a reviewed, frozen copy and fewer data leaks to vendors. A CDN gives caching and less upkeep. Here is how to decide per script.

Safeguard Security Team
4 min read

Self-host scripts that are static and that you can review, such as libraries and fonts, because you control the bytes and the update moment. Load from a CDN only with an exact version and an integrity hash. Treat vendor scripts that must change remotely as a separate, higher risk group.

What changes when you self-host

The OWASP Third Party JavaScript Management cheat sheet lists mirroring vendor scripts in house as a way to prevent unauthorized alterations and to reduce the data sent to third parties. Two things follow.

First, the file only changes when you change it. A compromised CDN account or an expired domain cannot push new code to your visitors.

Second, you take on the update job. A copy you never refresh becomes an old, vulnerable library. Self-hosting trades supply chain surprise for maintenance debt, so it needs an owner.

What changes when you use a CDN

A CDN gives shared caching, global edge delivery and nothing to maintain. The risk is that the URL decides what runs. If the URL names a floating version, the content can change under you. If the URL names an exact version and carries an integrity hash, the browser refuses a changed file.

A decision table

Script typeSuggested approachWhy
UI framework or utility libraryBundle it or self-host an exact versionStatic, reviewable, scanned with your other dependencies
Web fonts and icon setsSelf-hostStatic, and avoids sending visitor data to a third party
PolyfillsSelf-host a reviewed buildAvoid a hosted service that can change behavior
Library from a public CDNExact version plus integrity hashCheap protection with a loud failure
Analytics and ad tagsVendor hosted, tightly scopedThey change by design, so add CSP and monitoring
Payment or identity widgetsVendor hosted, ideally in an iframeOften required by the provider

Steps to move a script in house

  1. Download the exact version you run today and record the source URL and date.
  2. Compare it with the vendor's published file or package so you know it is genuine.
  3. Add it to your repository or build, so it is covered by code review.
  4. Add the package to your dependency scanning so advisories reach you.
  5. Remove the old script tag and the CDN domain from your CSP.
  6. Set a recurring review, for example monthly, to bump the version.

Questions that settle edge cases

  • Does the vendor require its own hosted URL for support or licensing? If yes, you cannot self-host.
  • Does the script need frequent server side updates to work? If yes, a frozen copy will rot.
  • Would a compromised version be able to read form fields or session data on this page? If yes, lean toward self-hosting or an iframe.

Related reading

The CDN supply chain risks post covers how things go wrong, and polyfill.io domain change lessons shows a hosted script changing under its users.

Safeguard scans dependencies and generates SBOMs, so a library you moved into your build gets the same advisory checks as the rest. A free scan is at gold.safeguard.sh/scan.

Try it yourself

Click through it at your own pace, or open it full screen. Look at how a bundled library shows up as an inventory entry you can track.

Quick check

Is self-hosting always safer than a CDN?

No. It removes remote tampering, but an outdated copy keeps known flaws. You need a refresh routine.

Can I get CDN benefits and still be safe?

Yes, with an exact version in the URL and an integrity attribute, so the browser rejects any changed file.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.