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

45 lines
1.0 KiB
Plaintext

include::../description.adoc[]
== Noncompliant Code Example
[source,java]
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
URL url = new URL(req.getParameter("url"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Noncompliant
}
----
== Compliant Solution
[source,java]
----
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))
throw new IOException();
URL url = uri.toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
}
----
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[]