rspec/rules/S5144/java/rule.adoc

45 lines
1.0 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 {
URL url = new URL(req.getParameter("url"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Noncompliant
}
----
== 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 {
String[] urlWhiteList = { "example.com", "www.example.com" };
String inputUrl = req.getParameter("url");
URI uri = new URI(inputUrl);
String remoteHost = uri.getHost();
if (!urlWhiteList.contains(remoteHost))
2020-06-30 12:50:28 +02:00
throw new IOException();
URL url = uri.toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
2020-06-30 12:50:28 +02:00
}
----
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[]