27 lines
510 B
Plaintext
27 lines
510 B
Plaintext
|
|
== Noncompliant Code Example
|
|
|
|
Example of DOM open redirect vulnerability (``++http://vulnerable/page.html#\https://www.attacker.com/++``):
|
|
|
|
----
|
|
document.location = document.location.hash.slice(1);
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
The URL can be validated with an allowlist:
|
|
|
|
----
|
|
function isValidUrl(url) {
|
|
if(url.startsWith("https://www.example.com/")) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
if(isValidUrl(document.location.hash.slice(1))) {
|
|
document.location = document.location.hash.slice(1);
|
|
}
|
|
----
|