|
| 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