Javascript Node.js安全性-跨源访问?

Javascript Node.js安全性-跨源访问?,javascript,node.js,mongodb,security,heroku,Javascript,Node.js,Mongodb,Security,Heroku,我正在一个相当简单的节点服务器上工作,但我需要一些关于如何防止来自不良来源的访问的指导 我的服务器提供两个页面——页面名称显示在第三个代码块中 两个页面都连接到Mongo数据库 为了测试脚本注入,我使用以下代码并从简单的.html文件本地启动它: quizxhr = new XMLHttpRequest(); var url = 'https://cryptic-sierra-4333.herokuapp.com/putQuizData'; quizxhr.open("POST", url, t

我正在一个相当简单的节点服务器上工作,但我需要一些关于如何防止来自不良来源的访问的指导

我的服务器提供两个页面——页面名称显示在第三个代码块中

两个页面都连接到Mongo数据库

为了测试脚本注入,我使用以下代码并从简单的.html文件本地启动它:

quizxhr = new XMLHttpRequest();
var url = 'https://cryptic-sierra-4333.herokuapp.com/putQuizData';
quizxhr.open("POST", url, true);
quizxhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
quizxhr.onreadystatechange = createMarkers();
quizxhr.send(quizItemToSend);

为了防止其他来源访问数据库,我尝试使用以下行:

response.header("Access-Control-Allow-Origin", "https://cryptic-sierra-4333.herokuapp.com/", "https://cryptic-sierra-4333.herokuapp.com/admin");
这并没有阻止我的本地文件访问数据库

我花了一些时间研究可能的解决方案,但没有任何明显的解决方案。也许我没有很好地理解我的问题,或者我可能遗漏了一些关键词汇

如蒙协助,将不胜感激


先谢谢你

尝试将以下中间件添加到您的NodeJS/Express应用程序中。为了方便起见,我添加了一些注释:

// Add headers
app.use(function (req, res, next) {

// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'https://cryptic-sierra-4333.herokuapp.com/", "https://cryptic-sierra-4333.herokuapp.com/admin');

// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);

// Pass to next layer of middleware
next();
});

希望有帮助

本地文件可能来自同一网络。本地文件来自哪个网络?您是否从非网络位置对此进行了测试和/或您的本地请求欺骗了来自外国的请求?CORS用于停止或允许外部网络请求。访问控制头只会阻止您发送无法通过标准HTML表单发送的请求。在本例中,您使用application/x-www-form-urlencoded内容类型发出POST请求。使用普通HTML表单很容易做到这一点,因此浏览器不会阻止这一点。如果您将HTTP方法更改为PUT或将内容类型更改为application/json,您应该会看到浏览器阻止了请求。@Shilly我的应用程序正在Heroku上运行,我正在本地运行hack.html文件。运行本地hack后,刷新Heroku会显示新的数据库条目。我相信这已经足够测试一个外来源请求了,不是吗?@idbehold您能帮我了解一下如何在server.js文件中使用application/json编写内容类型的语法吗?目前我有:res.setHeader'Access-Control-Allow-Headers','X-request-With,content-type';感谢您提供此模板,但是我已经在我的server.js文件中实现了此代码,但Access Control Allow Credentials行除外。不幸的是,单凭这一点并不能解决我的问题。我遗漏了一个细节吗?
// Add headers
app.use(function (req, res, next) {

// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'https://cryptic-sierra-4333.herokuapp.com/", "https://cryptic-sierra-4333.herokuapp.com/admin');

// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);

// Pass to next layer of middleware
next();
});