大网数据广告活动
网站运营与故障问答

Can you restrict a CDN to a list of domains/URLs?

· 52CE 我爱测

I would like to serve a JS file by CDN, but I want to be able to control the sites/pages that are allowed to use it. (Using the CDN. I know people can still just copy the file if they wanted to.) Is there a way to control what si…

问题原文

原帖链接 · Sarudora · CC BY-SA 4.0

I would like to serve a JS file by CDN, but I want to be able to control the sites/pages that are allowed to use it. (Using the CDN. I know people can still just copy the file if they wanted to.)

Is there a way to control what site/url can request the file? I only have found things like preventing hotlinking (= only allowing the main domain)

I guess the alternative would be just serving the file by CDN and having it send a request to some API and which will then check if it is allowed to use the file. This would cause me & them bandwidth and another request, which, if possible, would be great to keep low. I could serve the file by myself, but I am expecting high request numbers, so I thought a CDN would be better.

已采纳解答

原帖链接 · Maximillian Laumeister · CC BY-SA 4.0

Many CDNs have built-in hotlinking protection that you can turn on. The method of configuring it differs between each CDN provider, so you'll want to look at your specific CDN provider's support articles to see how they do it.

If built-in hotlink protection is unavailable at your CDN, you could always include a one-liner like this in the critical path of your JavaScript file if you're concerned:

if (window.location.hostname !== 'www.yoursite.example') return;

That will make the JS file totally useless to anyone who tries to hotlink it from a different site.

All in all though, I don't think stuff like that is all that necessary for JavaScript files, because nobody really hotlinks them from untrusted domains like they do with images, due to the security ramifications.

At worst case, hotlinking your JS file could give you near-complete power over their website and its content; at best case (if they had enough foresight to link your JS using SRI but not enough foresight to avoid hotlinking JS altogether), they would give you the power to break the script on their site at any time by moving, removing, or editing it.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-11-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。