禁用对Azure中Node.js源代码文件的访问
我正在用Node.js构建一个基本网站,我不希望任何人能够访问我的服务器端源代码(它包含数据库的登录凭据)。我的主应用程序位于运行应用程序的根目录中名为“app.js”的文件中。如果我浏览到mysite.com/app.js,就会提供源代码文件。有没有一种方法可以禁止使用Node.js访问某些文件,或者只是在一般情况下?该网站托管在Microsoft Azure上,如果这有什么不同的话(我的研究似乎表明Microsoft和Apache处理这个问题的方式不同)。2问题如下:禁用对Azure中Node.js源代码文件的访问,node.js,azure,Node.js,Azure,我正在用Node.js构建一个基本网站,我不希望任何人能够访问我的服务器端源代码(它包含数据库的登录凭据)。我的主应用程序位于运行应用程序的根目录中名为“app.js”的文件中。如果我浏览到mysite.com/app.js,就会提供源代码文件。有没有一种方法可以禁止使用Node.js访问某些文件,或者只是在一般情况下?该网站托管在Microsoft Azure上,如果这有什么不同的话(我的研究似乎表明Microsoft和Apache处理这个问题的方式不同)。2问题如下: 限制对静态文件的访问
- 限制对静态文件的访问
- 保护凭据信息
yourdomainname.com/myfile.js
的请求将映射到/path/to/static/assets/myfile.js
。研究他们的文档,看看如何解决这个问题
保护凭据信息
您需要将凭证信息(API密钥、数据库密码等)保留在代码repo之外。为此,您可以将
.env
文件与应用程序内服务一起使用。您可以在“应用程序设置”刀片中声明包括连接字符串在内的键值对。这将作为Java、Node、PHP和Python应用程序的环境变量提供。这样他们就安全了 基本上,Azure使用IIS为Node.js应用程序提供服务。因此,您需要将名为web.config
的IIS配置文件添加到应用程序的根文件夹中,以限制对服务器端源代码的访问
web.config
这在Azure中是如何托管的?虚拟机?应用程序服务?您可以使用环境变量或dotEnv包(通过将.env文件放入.gitignore中)。@Alex它托管在应用程序服务中。您是如何部署它的?只要有人知道my node.js文件的路径,就可以访问它们?我是否应该让路径真正变得模糊,以降低某些用户暴力强迫它的风险?理想情况下,yourdomainname.com/app.js会返回404。你误解了我的答案。关键是永远不要公开源文件。使用我指出的方法,没有人将永远无法访问您的源文件。你不需要混淆任何事情。我举的例子是为了向您展示,任何访问源文件的尝试都会导致nginx尝试从您在nginx config-path中放置的静态路径提供此文件,该路径应该不包含任何敏感源文件,而只包含可公开访问的资产(图像、css等)
<?xml version="1.0" encoding="utf-8"?>
<!--
This configuration file is required if iisnode is used to run node processes behind
IIS or IIS Express. For more information, visit:
https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->
<configuration>
<system.webServer>
<!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
<webSocket enabled="false" />
<handlers>
<!-- Indicates that the app.js file is a node.js site to be handled by the iisnode module -->
<add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<!-- Do not interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^app.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<!-- All other URLs are mapped to the node.js site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="app.js"/>
</rule>
</rules>
</rewrite>
<!-- bin directory has no special meaning in node.js and apps can be placed in it -->
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>
<!-- Make sure error responses are left untouched -->
<httpErrors existingResponse="PassThrough" />
<!--
You can control how Node is hosted within IIS using the following options:
* watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
* node_env: will be propagated to node as NODE_ENV environment variable
* debuggingEnabled - controls whether the built-in debugger is enabled
To debug your node.js application:
* set the debuggingEnabled option to "true"
* enable web sockets from the portal at https://manage.windowsazure.com/#Workspaces/WebsiteExtension/Website/aarontestnode/configure
* browse to https://aarontestnode.azurewebsites.net/app.js/debug/
See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
-->
<iisnode watchedFiles="web.config;*.js" debuggingEnabled="false" />
</system.webServer>
</configuration>