在通过JavaScript的location.replace()导航之前确保URL是相对的

时间:2016-05-30 17:49:24

标签: javascript url xss relative

我有一个登录页面https://example.com/login#destination,其中destination是用户在需要登录时尝试导航到的目标网址。
(即https://example.com/destination

我正在考虑使用的JavaScript是

function onSuccessfulLogin() {
    location.replace(location.hash.substring(1) || 'default')
}
  • 这会导致XSS漏洞,攻击者提供链接
    https://example.com/login#javascript:..

  • 此外,我需要在登录后阻止导航到相似的网站 https://example.com/login#https://looks-like-example.com
    https://example.com/login#//looks-like-example.com

如何调整onSuccessfulLogin以确保哈希#部分中提供的网址是相对网址,而不是以javascript:https:,{{1开头或者任何其他绝对导航方案?

一种想法是评估URL,并在导航之前查看//是否保持不变。你能建议怎么做,或者更好的方法吗?

2 个答案:

答案 0 :(得分:1)

来自OWASP关于Preventing Unvalidated Redirects and Forwards的建议:

  

建议将任何此类目标输入映射到值而不是URL的实际URL或部分,并且该服务器端代码将此值转换为目标URL。

因此,一种安全的方法是将一些密钥映射到实际的URL:

// https://example.com/login#destination

var keyToUrl = {
  destination: 'https://example.com/destination',
  defaults: 'https://example.com/default'
};

function onSuccessfulLogin() {
  var hash = location.hash.substring(1);
  var url = keyToUrl[hash] || keyToUrl.defaults;

  location.replace(url);
}

您还可以考虑仅提供网址的路径部分,并在代码中附加主机名:

// https://example.com/login#destination

function onSuccessfulLogin() {
  var path = location.hash.substring(1);
  var url = 'https://example.com/' + path;

  location.replace(url);
}

我会坚持使用映射。

答案 1 :(得分:0)

这是关于XSS漏洞的非常好点。

我相信所有协议只使用英文字母字符,因此像/^[a-z]+:/i这样的正则表达式会检查这些字符。或者,如果我们感觉更具包容性,/^[^:\/?]+:/只允许/?后跟:。然后我们可以将它与/^\/\/结合起来测试一个无协议的URL,它给了我们:

// Either
var rexIsProtocol = /(?:^[a-z]+:)|(?:^\/\/)/i;
// Or
var rexIsProtocol = /(?:^[^:\/?]+:)|(?:^\/\/)/i;

然后测试是这样的:

var url = location.hash.substring(1).trim(); // trim to deal with whitespace
if (rexIsProtocol.test(url)) {
    // It starts with a protocol
} else {
    // It doesn't
}

那就是说,我认为你唯一需要特别困扰的是javascript:伪protcol,所以你可能只是测试它。