javascript - Express 中公用文件夹之外的文件安全吗?

标签 javascript express security

我正在使用 Express 构建一个网站。

我有一个带有表单的页面来添加文章。我的网站上没有敏感数据,但我不希望人们能够发布文章。

所以我的问题是:人们可以访问不在公共(public)文件夹内的任何文件吗?

// Public directory
web.use(express.static(path.join(__dirname, 'public')));

将使用 token 访问页面( token 将被加密并存储在 json 文件中(公共(public)文件夹外部)

像这样:

// json file import
var data = require('../data/fileThatIsSafe.json');

// page render
web.get('/myPage/:token?', function(res, req) {
    if(encrypt(req.params.token) == data.token) {
        res.render('myPage');
    }
}

最佳答案

我无法准确说出 token 与您的问题有什么关系,但如果您只是询问您的 express.static() 代码是否安全:

web.use(express.static(path.join(__dirname, 'public')));

然后,答案是 express.static() (默认情况下)不允许访问您传递的目录之外的任何文件。在您的示例中,它仅提供对 public 目录层次结构的访问。如果你看doc对于 express.static(),您将看到具有以下设置的 dotfiles 选项:

Possible values for this option are:

"allow"  - No special treatment for dotfiles.
"deny"   - Deny a request for a dotfile, respond with 403, then call next().
"ignore" - Act as if the dotfile does not exist, respond with 404, then call next().

此选项的默认值为“ignore”,并将返回 404。

So my question is: can people have access to any file that is not inside the public folder ?

不通过您的 express.static() 中间件。因此,人们可以访问您服务器上其他文件的唯一方法是您有明确允许他们的其他路线 请求这些文件。

在您显示的另一条路由中,似乎确实需要在发送内容之前随路由一起发送适当的 token 。当 token 不匹配时,特定的路由处理程序需要发送某种响应(可能是错误响应)。

关于javascript - Express 中公用文件夹之外的文件安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60856126/

相关文章:

javascript - Service Worker 更新,但旧文件仍被缓存

javascript - 使用nodejs和jade的Angular文件夹结构

javascript - Javascript 中是否有类似于 Python 中的 .format() 的函数?

angularjs - 在没有 Mongoose 用户的情况下使用 Passport-facebook(MEAN 堆栈中没有 Mongo)

security - https 和 SSL 可以保护您的邮件地址不被收集并用作垃圾邮件?

javascript - 从 Node 模块在 VueJS 中提供 firebaseui.css 的问题

node.js - Express.js - 任何显示文件/目录列表的方式?

sql-server-2005 - 用户无权运行 DBCC DBREINDEX

java - 如何正确使用 Bouncy CaSTLe 的 OAEPEncoding for RSA(轻量级 API)

javascript - Node JS 回调