简体   繁体   English

如何阻止对我的 JavaScript 文件的直接访问?

[英]How can I block direct access to my JavaScript files?

I use Minify to minify and cache all my script requests.我使用 Minify 来缩小和缓存我的所有脚本请求。 I only want my users to be able to access the minified versions of the JavaScript files.我只希望我的用户能够访问 JavaScript 文件的缩小版本。

Minify lies at www.example.com/min and my scripts are at www.example.com/scripts . Minify 位于www.example.com/min ,我的脚本位于www.example.com/scripts How can I block direct access to doc_root/scripts which is where my unminified JavaScript files lie.如何阻止对doc_root/scripts的直接访问,这是我未缩小的 JavaScript 文件所在的位置。 I'd rather not put them out of the document root but it's an option.我宁愿不把它们放在文档根目录之外,但这是一个选择。

Please note that I'm using Zend Framework, so the actual root of my application is shifted to www.example.com/public .请注意,我使用的是 Zend Framework,因此我的应用程序的实际根目录转移到www.example.com/public An htaccess file handles the rewrite. htaccess 文件处理重写。

Can't you just use an .htaccess file inside doc_root/scripts to prevent all access over the web to .js files over HTTP?难道你不能只在doc_root/scripts中使用 .htaccess 文件来防止通过 web 访问 HTTP 上的.js文件吗?

It won't stop minify, since that provides indirect access.它不会停止缩小,因为它提供了间接访问。

So in doc_root/scripts/.htaccess , something along the lines of所以在doc_root/scripts/.htaccess中,类似于

<Files ~ "\.js$">
    order allow,deny
    deny from all
</Files>

Note that the location of the .htaccess file matters in this case.请注意,在这种情况下, .htaccess文件的位置很重要。

You effectively can't block end-user facing code.您实际上无法阻止面向最终用户的代码。 Even if you served it with PHP or another server-side language and blocked direct requests, it's of course still possible to read it directly with a number of tools.即使您使用 PHP 或其他服务器端语言提供它并阻止直接请求,当然仍然可以使用许多工具直接读取它。

You should code with this in mind and be mindful with javascript comments, business knowledge, etc.您应该牢记这一点,并注意 javascript 注释、业务知识等。

UPDATE:更新:

However, if you're talking about code that doesn't ever need to be accessed by an end-user, you could as you mentioned move it out of the server root, or you can block the files in your directory (or an entire directory).但是,如果您谈论的是最终用户不需要访问的代码,您可以像您提到的那样将其移出服务器根目录,或者您可以阻止目录中的文件(或整个目录)。 It's easy with Apache's .htaccess.使用 Apache 的 .htaccess 很容易。

order deny, allow
deny from all

You could also redirect the source files to the minified versions with mod_rewrite in your .htaccess file.您还可以在 .htaccess 文件中使用 mod_rewrite 将源文件重定向到缩小版本。

RewriteEngine On
RewriteRule /scripts/(.*)$ /min/$1 [L,NC]

This answer is little bit newer, than question (only several years, that's nothing)这个答案比问题要新一点(只有几年,没什么)

You cannot deny access to JavaScript file, because they wont't be accessible from <script> tag.您不能拒绝访问 JavaScript 文件,因为它们无法从<script>标记访问。

But I found a workaround:但我找到了解决方法:


RewriteEngine On

RewriteRule ^.*\.js$ /invalid.html [R=301,L]

Place it in your .htaccess file in your home folder of web.将它放在 web 主文件夹中的 .htaccess 文件中。 (under htdocs or public_html). (在 htdocs 或 public_html 下)。 This will automatically redirect everyone from it.这将自动重定向每个人。 So they don't see it.所以他们看不到。

Depends on the server you're using.取决于您使用的服务器。 Assuming it's Apache, you can add this to your .htaccess file:假设它是 Apache,您可以将其添加到 .htaccess 文件中:

<Directory ~ "\scripts">
Order allow,deny
Deny from all
</Directory>

Or something to that effect..或者类似的东西..

The only way is to check referers, and not everyone sends them, or sends a real one.唯一的方法是检查推荐人,并不是每个人都发送它们,或者发送一个真实的。 In other words, you can't block direct access to anyone who really wants something.换句话说,您不能阻止对任何真正想要某物的人的直接访问。 It's impossible to determine with 100% accuracy if a request is a direct one or is being done via a <script src=....> type request.无法 100% 准确确定请求是直接请求还是通过<script src=....>类型请求完成。

For your Javascript to actually run the user's browser must be able to read it ultimately.为了使您的 Javascript 实际运行,用户的浏览器必须最终能够读取它。 As such there's no real way to "block" access to your scripts folder (well to be precise you can but that would break your website since the browser would not see the files in order to run them.)因此,没有真正的方法可以“阻止”对您的脚本文件夹的访问(准确地说,您可以这样做,但这会破坏您的网站,因为浏览器不会看到这些文件以运行它们。)

One solution could be obfuscation, which makes the javascript code harder to read / understand but ultimately the user will see the code, and with a bit of persevering reverse engineering it can be de-obfuscated.一种解决方案可能是混淆,这会使 javascript 代码更难阅读/理解,但最终用户会看到代码,并且通过一些坚持不懈的逆向工程,它可以被去混淆。

Another thing i've seen someone do is creating an "empty" js.html page, and insert all their javascript into script tags in the page (embedded, not external), and from his main page make ann ajax request to js.html and embed it at the bottom of the page. Another thing i've seen someone do is creating an "empty" js.html page, and insert all their javascript into script tags in the page (embedded, not external), and from his main page make ann ajax request to js.html并将其嵌入页面底部。 kind of a round about way but the user will not see the js when viewing the source unless using developper tools such as firebug.一种迂回的方式,但用户在查看源代码时不会看到 js,除非使用诸如 firebug 之类的开发工具。

Note that the last option also might cause some delay depending on the abount of code you are loading.请注意,最后一个选项也可能会导致一些延迟,具体取决于您加载的代码量。 but here the key is not blocking access to your scripts, but just making them harder to obtain / read / copy.但这里的关键不是阻止对您的脚本的访问,而是让它们更难获取/读取/复制。

Edit: oops, misread as well.编辑:哎呀,也误读了。 I think the best solution in this case would be to go with an htaccess file in your scripts folder denying all access我认为在这种情况下最好的解决方案是 go 在您的脚本文件夹中使用 htaccess 文件拒绝所有访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM