45 lines
1.1 KiB
Plaintext
Raw Normal View History

=== How to fix it in Java SE
include::../../common/fix/code-rationale.adoc[]
2022-10-18 16:03:10 +02:00
==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String location = req.getParameter("url");
URL url = new URL(location);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
}
----
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String location = req.getParameter("url");
List<String> allowedHosts = new ArrayList<String>();
allowedHosts.add("https://trusted1.example.com/");
allowedHosts.add("https://trusted2.example.com/");
URL url = new URL(location);
if (allowedHosts.contains(location))
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
}
----
include::../../common/fix/how-does-this-work.adoc[]
The compliant code example uses such an approach.
=== Pitfalls
include::../../common/pitfalls/starts-with.adoc[]