Skip to content

Commit 8df71c6

Browse files
Demo - Unsafe Consumption of APIs
1 parent c76117b commit 8df71c6

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.itbulls.learnit.onlinestore.web.owasp.uca.problem;
2+
3+
import jakarta.servlet.*;
4+
import jakarta.servlet.http.*;
5+
import java.io.*;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
9+
public class VulnerablePaymentServlet extends HttpServlet {
10+
11+
@Override
12+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13+
String paymentData = request.getParameter("paymentData");
14+
String apiUrl = "https://paymentgateway.com/process";
15+
16+
// Vulnerable code: blindly following redirects
17+
URL url = new URL(apiUrl);
18+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
19+
connection.setRequestMethod("POST");
20+
connection.setDoOutput(true);
21+
connection.getOutputStream().write(paymentData.getBytes("UTF-8"));
22+
23+
int responseCode = connection.getResponseCode();
24+
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM) {
25+
String newLocation = connection.getHeaderField("Location");
26+
// Vulnerability: blindly following redirection
27+
URL redirectUrl = new URL(newLocation);
28+
HttpURLConnection redirectConnection = (HttpURLConnection) redirectUrl.openConnection();
29+
redirectConnection.setRequestMethod("POST");
30+
redirectConnection.setDoOutput(true);
31+
redirectConnection.getOutputStream().write(paymentData.getBytes("UTF-8"));
32+
}
33+
34+
// Read and process the response
35+
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
36+
String inputLine;
37+
StringBuilder responseString = new StringBuilder();
38+
while ((inputLine = in.readLine()) != null) {
39+
responseString.append(inputLine);
40+
}
41+
in.close();
42+
43+
// Send the response back to the client
44+
response.getWriter().write(responseString.toString());
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.itbulls.learnit.onlinestore.web.owasp.uca.solution;
2+
3+
import jakarta.servlet.*;
4+
import jakarta.servlet.http.*;
5+
import java.io.*;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
9+
public class SecurePaymentServlet extends HttpServlet {
10+
11+
private static final String[] ALLOWED_REDIRECT_DOMAINS = {"paymentgateway.com"};
12+
13+
@Override
14+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
15+
String paymentData = request.getParameter("paymentData");
16+
String apiUrl = "https://paymentgateway.com/process";
17+
18+
// Establish connection to payment gateway
19+
URL url = new URL(apiUrl);
20+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
21+
connection.setRequestMethod("POST");
22+
connection.setDoOutput(true);
23+
connection.getOutputStream().write(paymentData.getBytes("UTF-8"));
24+
25+
int responseCode = connection.getResponseCode();
26+
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM) {
27+
String newLocation = connection.getHeaderField("Location");
28+
29+
// Validate the redirect URL
30+
URL redirectUrl = new URL(newLocation);
31+
if (isAllowedRedirectDomain(redirectUrl)) {
32+
// Only follow the redirect if it's to a trusted domain
33+
HttpURLConnection redirectConnection = (HttpURLConnection) redirectUrl.openConnection();
34+
redirectConnection.setRequestMethod("POST");
35+
redirectConnection.setDoOutput(true);
36+
redirectConnection.getOutputStream().write(paymentData.getBytes("UTF-8"));
37+
38+
// Read and process the redirect response
39+
BufferedReader in = new BufferedReader(new InputStreamReader(redirectConnection.getInputStream()));
40+
String inputLine;
41+
StringBuilder responseString = new StringBuilder();
42+
while ((inputLine = in.readLine()) != null) {
43+
responseString.append(inputLine);
44+
}
45+
in.close();
46+
47+
// Send the redirect response back to the client
48+
response.getWriter().write(responseString.toString());
49+
} else {
50+
// Handle untrusted redirect
51+
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Redirect to an untrusted domain.");
52+
}
53+
} else {
54+
// Read and process the original response
55+
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
56+
String inputLine;
57+
StringBuilder responseString = new StringBuilder();
58+
while ((inputLine = in.readLine()) != null) {
59+
responseString.append(inputLine);
60+
}
61+
in.close();
62+
63+
// Send the response back to the client
64+
response.getWriter().write(responseString.toString());
65+
}
66+
}
67+
68+
// Helper method to check if the domain of the redirect URL is allowed
69+
private boolean isAllowedRedirectDomain(URL url) {
70+
for (String domain : ALLOWED_REDIRECT_DOMAINS) {
71+
if (url.getHost().endsWith(domain)) {
72+
return true;
73+
}
74+
}
75+
return false;
76+
}
77+
}

0 commit comments

Comments
 (0)