include::../description.adoc[]

== Noncompliant Code Example

[source,javascript]
----
function redirect(req, res) {
  const url = req.query.url; // user-controlled input

  res.redirect(url); // Noncompliant
}

function setLocationHeader(req, res) {
  const url = req.query.url; // user-controlled input

  res.location(url); // Noncompliant
  res.sendStatus(302);
}
----

== Compliant Solution

Validate the URL with an allowlist:

[source,javascript]
----
function isValidUrl(url) {
  if (url.startsWith("https://www.safe.com/")) {
    return true;
  }

  return false;
}

function redirect(req, res) {
  const url = req.query.url; // user-controlled input

  if (isValidUrl(url)) {
    res.redirect(url);
  }
}
----

include::../see.adoc[]
ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

include::../highlighting.adoc[]

endif::env-github,rspecator-view[]
