Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions api/src/main/java/org/openmrs/util/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ public String post(Map<String, String> parameters) {

// get redirect url from "location" header field
String newUrl = connection.getHeaderField("Location");
connection = (HttpURLConnection)new URL(newUrl).openConnection();
java.net.URL redirectedUrl = new java.net.URL(newUrl);

// Validate redirect URL against original URL
if (!redirectedUrl.getProtocol().equalsIgnoreCase(url.getProtocol())
|| !redirectedUrl.getHost().equalsIgnoreCase(url.getHost())
|| (redirectedUrl.getPort() != -1
? redirectedUrl.getPort() != url.getPort()
: url.getPort() != -1)) {
throw new java.io.IOException("Redirect to disallowed URL: " + newUrl);
}

connection = (HttpURLConnection) redirectedUrl.openConnection();

log.info("Redirection to : " + newUrl);

Expand All @@ -86,7 +97,7 @@ public String post(Map<String, String> parameters) {
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

wr = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
wr.write(data.toString());
wr.flush();
Expand Down