rspec/rules/S5146/java/rule.adoc

49 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-06-30 12:50:28 +02:00
include::../description.adoc[]
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:50:28 +02:00
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String location = req.getParameter("url");
resp.sendRedirect(location); // Noncompliant
}
----
2022-02-04 17:28:24 +01:00
[source,java]
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String location = req.getParameter("url");
resp.setStatus(302);
resp.setHeader("Location", location); // Noncompliant
}
----
2020-06-30 12:50:28 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:50:28 +02:00
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
2021-05-21 01:24:06 +00:00
String location = req.getParameter("url");
2020-06-30 12:50:28 +02:00
2021-05-21 01:24:06 +00:00
List<String> allowedHosts = new ArrayList<String>();
allowedUrls.add("https://www.domain1.com/");
allowedUrls.add("https://www.domain2.com/");
2020-06-30 12:50:28 +02:00
2021-05-21 01:24:06 +00:00
if (allowedUrls.contains(location))
2020-06-30 12:50:28 +02:00
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[]