gpt4 book ai didi

node.js - 使用 url.parse 检查 redirectURL 是否仅在域内安全?

转载 作者:搜寻专家 更新时间:2023-11-01 00:40:01 25 4
gpt4 key购买 nike

我只需要在公共(public)域 example.local 内进行安全重定向。因此,例如以下内容是有效的:http://blog.example.localhttp://www.example.local/dashboard 而以下内容则不是:http://blog.example.local http://blog.example.local.fake.com

var url = require('url');

app.post('/login', function (req, res, next) {
var redirect = req.query.redirect,
targetUrl = url.parse(redirect);

if (targetUrl.host !== 'example.local') {
return next(new Error('Open redirect attack detected'));
}

return res.redirect(redirect);
});

在生产中使用它是否足够安全,或者我应该修复一些东西吗?

最佳答案

在您的代码中,如果 targetUrl = 'http://blog.example.local',您的应用将返回错误“检测到打开重定向攻击”,因为 targetUrl.host 将为“blog.example” .local'

请稍微更改您的代码以

var expectedHost = [
'blog.example.local',
'example.local'
];

if (expectedHost.indexOf(targetUrl.host) === -1) {
return next(new Error('Open redirect attack detected'));
}

那么一切都会好起来的

关于node.js - 使用 url.parse 检查 redirectURL 是否仅在域内安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com