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

Ensure URL is relative before navigating via JavaScript's location.replace()

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

我正在考虑使用的JavaScript是

function onSuccessfulLogin() {
    location.replace(location.hash.substring(1) || 'default')
}

如何调整 onSuccessfulLogin 以确保散列 # 部分中提供的 URL 是相对的 URL,而不是以 javascript: 开头, https:, // 或任何其他绝对导航方案?

一个想法是评估 URL,并在导航之前查看 location.origin 是否保持不变。您能否建议如何执行此操作或更好的方法?

这是关于 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: 伪协议,所以您可以测试一下。

来自 Preventing Unvalidated Redirects and Forwards 上的 OWASP 推荐:

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.

所以一个安全的方法是将一些键映射到实际的 URLs:

// 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);
}

虽然我会坚持映射。