rspec/rules/S5146/java/rule.adoc
2022-02-04 16:28:24 +00:00

49 lines
1.1 KiB
Plaintext

include::../description.adoc[]
== Noncompliant Code Example
[source,java]
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String location = req.getParameter("url");
resp.sendRedirect(location); // Noncompliant
}
----
[source,java]
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String location = req.getParameter("url");
resp.setStatus(302);
resp.setHeader("Location", location); // Noncompliant
}
----
== Compliant Solution
[source,java]
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String location = req.getParameter("url");
List<String> allowedHosts = new ArrayList<String>();
allowedUrls.add("https://www.domain1.com/");
allowedUrls.add("https://www.domain2.com/");
if (allowedUrls.contains(location))
resp.sendRedirect(location); // Compliant
}
----
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[]