polyfill.io now available on cdnjs: reduce your supply chain risk (2024)

2024-02-29

  • Sven Sauleau

  • Michael Tremante

3 min read

polyfill.io now available on cdnjs: reduce your supply chain risk

Polyfill.io is a popular JavaScript library that nullifies differences across old browser versions. These differences often take up substantial development time.

It does this by adding support for modern functions (via polyfilling), ultimately letting developers work against a uniform environment simplifying development. The tool is historically loaded by linking to the endpoint provided under the domain polyfill.io.

In the interest of providing developers with additional options to use polyfill, today we are launching an alternative endpoint under cdnjs. You can replace links to polyfill.io “as is” with our new endpoint. You will then rely on the same service and reputation that cdnjs has built over the years for your polyfill needs.

Our interest in creating an alternative endpoint was also sparked by some concerns raised by the community, and main contributors, following the transition of the domain polyfill.io to a new provider (Funnull).

The concerns are that any website embedding a link to the original polyfill.io domain, will now be relying on Funnull to maintain and secure the underlying project to avoid the risk of a supply chain attack. Such an attack would occur if the underlying third party is compromised or alters the code being served to end users in nefarious ways, causing, by consequence, all websites using the tool to be compromised.

Supply chain attacks, in the context of web applications, are a growing concern for security teams, and also led us to build a client side security product to detect and mitigate these attack vectors: Page Shield.

Irrespective of the scenario described above, this is a timely reminder of the complexities and risks tied to modern web applications. As maintainers and contributors of cdnjs, currently used by more than 12% of all sites, this reinforces our commitment to help keep the Internet safe.

polyfill.io on cdnjs

The full polyfill.io implementation has been deployed at the following URL:

https://cdnjs.cloudflare.com/polyfill/

The underlying bundle link is:

For minified: https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.jsFor unminified: https://cdnjs.cloudflare.com/polyfill/v3/polyfill.js

Usage and deployment is intended to be identical to the original polyfill.io site. As a developer, you should be able to simply “replace” the old link with the new cdnjs-hosted link without observing any side effects, besides a possible improvement in performance and reliability.

If you don’t have access to the underlying website code, but your website is behind Cloudflare, replacing the links is even easier, as you can deploy a Cloudflare Worker to update the links for you:

export interface Env {}export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { ctx.passThroughOnException(); const response = await fetch(request); if ((response.headers.get('content-type') || '').includes('text/html')) { const rewriter = new HTMLRewriter() .on('link', { element(element) { const rel = element.getAttribute('rel'); if (rel === 'preconnect') { const href = new URL(element.getAttribute('href') || '', request.url); if (href.hostname === 'polyfill.io') { href.hostname = 'cdnjs.cloudflare.com'; element.setAttribute('href', href.toString()); } } }, }) .on('script', { element(element) { if (element.hasAttribute('src')) { const src = new URL(element.getAttribute('src') || '', request.url); if (src.hostname === 'polyfill.io') { src.hostname = 'cdnjs.cloudflare.com'; src.pathname = '/polyfill' + src.pathname; element.setAttribute('src', src.toString()); } } }, }); return rewriter.transform(response); } else { return response; } },};

Instructions on how to deploy a worker can be found on our developer documentation.

You can also test the Worker on your website without deploying the worker. You can find instructions on how to do this in another blog post we wrote in the past.

Implemented with Rust on Cloudflare Workers

We were happy to discover that polyfill.io is a Rust project. As you might know, Rust has been a first class citizen on Cloudflare Workers from the start.

The polyfill.io service was hosted on Fastly and used their Rust library. We forked the project to add the compatibility for Cloudflare Workers, and plan to make the fork publicly accessible in the near future.

Worker

The https://cdnjs.cloudflare.com/polyfill/[...].js endpoints are also implemented in a Cloudflare Worker that wraps our Polyfill.io fork. The wrapper uses Cloudflare’s Rust API and looks like the following:

#[event(fetch)]async fn main(req: Request, env: Env, ctx: Context) -> Result<Response> { let metrics = {...}; let polyfill_store = get_d1(&req, &env)?; let polyfill_env = Arc::new(service::Env { polyfill_store, metrics }); // Run the polyfill.io entrypoint let res = service::handle_request(req2, polyfill_env).await; let status_code = if let Ok(res) = &res { res.status_code() } else { 500 }; metrics .requests .with_label_values(&[&status_code.to_string()]) .inc(); ctx.wait_until(async move { if let Err(err) = metrics.report_metrics().await { console_error!("failed to report metrics: {err}"); } }); res}

The wrapper only sets up our internal metrics and logging tools, so we can monitor uptime and performance of the underlying logic while calling the Polyfill.io entrypoint.

Storage for the Polyfill files

All the polyfill files are stored in a key-value store powered by Cloudflare D1. This allows us to fetch as many polyfill files as we need with a single SQL query, as opposed to the original implementation doing one KV get() per file.

For performance, we have one Cloudflare D1 instance per region and the SQL queries are routed to the nearest database.

cdnjs for your JavaScript libraries

cdnjs is hosting over 6k JavaScript libraries as of today. We are looking for ways to improve the service and provide new content. We listen to community feedback and welcome suggestions on our community forum, or cdnjs on GitHub.

Page Shield is also available to all paid plans. Log in to turn it on with a single click to increase visibility and security for your third party assets.

Cloudflare's connectivity cloud protects entire corporate networks, helps customers build Internet-scale applications efficiently, accelerates any website or Internet application, wards off DDoS attacks, keeps hackers at bay, and can help you on your journey to Zero Trust.

Visit 1.1.1.1 from any device to get started with our free app that makes your Internet faster and safer.

To learn more about our mission to help build a better Internet, start here. If you're looking for a new career direction, check out our open positions.

CDNJSJavaScriptSupply Chain Attacks

Related posts

September 09, 2024 9:00 PM

More NPM packages on Cloudflare Workers: Combining polyfills and native code to support Node.js APIs

Workers now supports more NPM packages and Node.js APIs using an overhauled hybrid compatibility layer....

    By
  • James M Snell,

  • Igor Minar,

  • James Culveyhouse,

  • Mike Nomitch

Cloudflare Workers,Node.js,Serverless,JavaScript

June 26, 2024 8:23 PM

Automatically replacing polyfill.io links with Cloudflare’s mirror for a safer Internet

polyfill.io, a popular JavaScript library service, can no longer be trusted and should be removed from websites...

    By
  • Matthew Prince,

  • John Graham-Cumming,

  • Michael Tremante

CDNJS,JavaScript,Vulnerabilities,Application Security,Application Services,Supply Chain Attacks,Attacks,Better Internet

November 14, 2023 2:00 PM

Streaming and longer context lengths for LLMs on Workers AI

Workers AI now supports streaming text responses for the LLM models in our catalog, including Llama-2, using server-sent events...

    By
  • Jesse Kipp,

  • Celso Martinho

Workers AI,Cloudflare Workers,Developer Platform,JavaScript,Serverless,1.1.1.1

September 28, 2023 1:00 PM

Running Serverless Puppeteer with Workers and Durable Objects

We’ve heard from developers that configuring and maintaining their own serverless browser automation systems can be quite painful. The Workers Browser Rendering API solves this...

    By
  • Tanushree Sharma

Birthday Week,Product News,Durable Objects,JavaScript,Developers

polyfill.io now available on cdnjs: reduce your supply chain risk (2024)

References

Top Articles
23 delicious foods you should eat in China
Ji Dan Bing (Chinese Breakfast Pancake, 鸡蛋饼)
How To Start a Consignment Shop in 12 Steps (2024) - Shopify
Craigslist Niles Ohio
What are Dietary Reference Intakes?
Bloxburg Image Ids
Craigslist In Fredericksburg
What's New on Hulu in October 2023
Locate Td Bank Near Me
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
Aquatic Pets And Reptiles Photos
Best Restaurants Ventnor
General Info for Parents
Kinkos Whittier
Premier Reward Token Rs3
Connect U Of M Dearborn
boohoo group plc Stock (BOO) - Quote London S.E.- MarketScreener
Jenn Pellegrino Photos
Aucklanders brace for gales, hail, cold temperatures, possible blackouts; snow falls in Chch
Po Box 35691 Canton Oh
Who called you from +19192464227 (9192464227): 5 reviews
Milanka Kudel Telegram
Maxpreps Field Hockey
Conscious Cloud Dispensary Photos
E32 Ultipro Desktop Version
Kentuky Fried Chicken Near Me
Wsbtv Fish And Game Report
What Equals 16
EVO Entertainment | Cinema. Bowling. Games.
Publix Near 12401 International Drive
Is Poke Healthy? Benefits, Risks, and Tips
Best Restaurants Ventnor
Bfri Forum
Wells Fargo Bank Florida Locations
Chadrad Swap Shop
Wisconsin Volleyball Team Leaked Uncovered
Brenda Song Wikifeet
Jay Gould co*ck
Of An Age Showtimes Near Alamo Drafthouse Sloans Lake
Edward Walk In Clinic Plainfield Il
Indiana Immediate Care.webpay.md
Devotion Showtimes Near The Grand 16 - Pier Park
Cal Poly 2027 College Confidential
10 Rarest and Most Valuable Milk Glass Pieces: Value Guide
Bill Manser Net Worth
About Us
Www.homedepot .Com
Roller Znen ZN50QT-E
Craigslist Pets Lewiston Idaho
Superecchll
Otter Bustr
8663831604
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6494

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.