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

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

(即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以确保hash#部分中提供的URL是相对URL,而不是以javascript:,https:,//或任何其他绝对导航方案开头?

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

最佳答案 来自OWASP
Preventing Unvalidated Redirects and Forwards的建议:

It is recommended that any such destination input be mapped to a value, rather than the actual URL or portion of the URL, and that server side code translate this value to the target 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);
}

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

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

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

  location.replace(url);
}

我会坚持使用映射.

点赞