Stop CDN Bandwidth Abuse with 302 Redirects (No More Surprise Bills)

Every website owner loves the speed boost and scalability of a Content Delivery Network (CDN), but malicious traffic "brushing" (automated abuse of CDN resources) can turn those benefits into hefty bills. In a brushing attack, bad actors repeatedly request your CDN-hosted files (images, videos, etc.) to inflate bandwidth usage and costs. This article explores how to prevent CDN abuse and save on costs. We’ll start with a common (but flawed) solution—embedding an auth key in client-side JavaScript—and then introduce a safer alternative: server-side 302 redirects with an auth key.

The Common Approach: Client-Side Auth Key (Easy but Insecure)

A quick fix many developers try is to embed a secret token or auth key in the front-end code, using it to authorize CDN requests. For example, your page’s JavaScript might include a key and append it to resource URLs (e.g. https://cdn.example.com/file.png?authKey=12345​). This approach is straightforward and keeps casual scrapers out. However, it has serious vulnerabilities and downsides:

So, Embedding auth secrets in the front-end is not a robust solution. It’s easy to implement but leaves a gaping hole – whatever is in the user’s JS can and will be discovered. Continuously changing keys and tinkering with front-end logic is a brittle game of whack-a-mole. We need a better way to protect CDN content that doesn’t rely on the honor system or constant manual updates.

A Better Alternative: Server-Side 302 Redirect with Auth

Instead of giving the client a key to access the CDN, keep that key and logic on your server. The idea is to have your server act as a gatekeeper: the client requests the resource from your server, and then your server responds with a HTTP 302 redirect (a temporary redirect) to the actual CDN URL, including an auth token or signature. This way, the browser is automatically sent to the CDN to fetch the content, but only after your server has decided it’s OK.

Here’s how this flow works step by step:

This approach dramatically improves security and flexibility:

To visualize the difference between the two approaches, let's compare them side by side:

Comparison of two methods for protecting CDN content.
Comparison of two methods for protecting CDN content.

On the left, the client-side key approach is shown: the browser loads a page that contains a JavaScript auth key, then uses that key to request content directly from the CDN. The content is delivered, but an attacker can easily extract the key from the page or network requests and abuse it by scripting repeated CDN calls, racking up costs (as indicated by the red dashed arrows). On the right, the server-side 302 redirect approach keeps the key hidden on the server. The browser asks the web server for the resource, and the server responds with a 302 redirect (temporary redirect) including a one-time auth token in the URL. The browser then fetches from the CDN with that token, getting the content. In this model, the server can enforce security checks (e.g., block malicious bots making mass requests), and the client’s code remains simple.

Conclusion: CDNs Are Worth It—Just Protect Them

Should you avoid using a CDN because of abuse risks? Definitely not. CDNs are extremely valuable for speeding up content delivery and reducing load on your servers. They bring content closer to users and can even lower costs if used wisely (serving cached content cheaply). The solution is not to abandon CDNs, but to fortify them.

By implementing measures like server-side redirects with auth (and other best practices like CDN rate limiting or WAF rules), you ensure that your CDN works for you and your users, not for attackers. Malicious traffic brushing can be stopped in its tracks, saving you potentially huge bandwidth charges and service slowdowns. Meanwhile, real users still get the fast, smooth experience they expect.

In summary, use a CDN, but don’t leave it wide open. Combine it with smart server-side controls: keep your auth keys secret, decide who gets access to what, and leverage HTTP redirects to seamlessly route users to the right content. This way, you can enjoy the performance benefits of a CDN safely and cost-effectively. With a bit of upfront effort in setting up these protections, you’ll sleep easier knowing that both your website and your wallet are safeguarded from malicious traffic.

你可能也感兴趣