|
| 1 | +package cgs.interceptor; // Your package name |
| 2 | + |
| 3 | +import org.springframework.web.multipart.MultipartHttpServletRequest; |
| 4 | +import org.springframework.web.servlet.HandlerInterceptor; |
| 5 | +import org.springframework.web.servlet.ModelAndView; |
| 6 | + |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import javax.servlet.http.HttpServletResponse; |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.Enumeration; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +/** |
| 14 | + * JavaSecurityInterceptor |
| 15 | + * |
| 16 | + * This interceptor validates HTTP requests to safeguard against security vulnerabilities |
| 17 | + * such as SQL Injection, Cross-Site Scripting (XSS), and malicious file uploads. |
| 18 | + * It performs validation checks on request parameters and file uploads, rejecting |
| 19 | + * any that appear to be malicious or invalid. |
| 20 | + * |
| 21 | + * Author: Deepak Kumar |
| 22 | + * Email: imchahardeepak@gmail.com |
| 23 | + * GitHub: https://github.com/imdeepakchahar/java-security-interceptor |
| 24 | + */ |
| 25 | +public class JavaSecurityInterceptor implements HandlerInterceptor { |
| 26 | + |
| 27 | + // Pattern to detect SQL injection keywords in request parameters. |
| 28 | + private static final String SQL_INJECTION_PATTERN = |
| 29 | + "(?i).*\\b(select|insert|drop|update|delete|exec|union|create|alter|truncate|declare|--|\\/\\*|\\*\\/|;|where|having|limit|group)\\b.*"; |
| 30 | + |
| 31 | + // Pattern to detect dangerous characters such as <, >, ', ", %, and &. |
| 32 | + private static final String DANGEROUS_CHARACTERS_PATTERN = ".*[<>'\"%&].*"; |
| 33 | + |
| 34 | + // Pattern to ensure input is valid UTF-8 encoded. |
| 35 | + private static final String UTF8_PATTERN = "[\\u0000-\\u007F]+"; |
| 36 | + |
| 37 | + // Pattern to identify potential XSS payloads in the input. |
| 38 | + private static final String XSS_PATTERN = "<.*?>"; |
| 39 | + |
| 40 | + // Patterns to detect newline characters and null bytes in input. |
| 41 | + private static final String NEWLINE_PATTERN = "[\\r\\n]"; |
| 42 | + private static final String NULL_BYTE_PATTERN = "%00"; |
| 43 | + |
| 44 | + // List of allowed file extensions for uploaded files. |
| 45 | + private static final String[] ALLOWED_FILE_EXTENSIONS = {"jpg", "jpeg", "png", "pdf", "docx"}; |
| 46 | + |
| 47 | + /** |
| 48 | + * Pre-handle method to validate HTTP requests before they reach the controller. |
| 49 | + * |
| 50 | + * @param request the HTTP request object |
| 51 | + * @param response the HTTP response object |
| 52 | + * @param handler the handler for the request |
| 53 | + * @return true if the request passes all validations, false otherwise |
| 54 | + * @throws Exception in case of any unexpected errors |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
| 58 | + Enumeration<String> parameterNames = request.getParameterNames(); |
| 59 | + |
| 60 | + // Validate uploaded files for allowed extensions. |
| 61 | + if (!validateFileInput(request)) { |
| 62 | + response.sendError(400, "Invalid file type detected."); |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + // Iterate through all request parameters to validate their content. |
| 67 | + while (parameterNames.hasMoreElements()) { |
| 68 | + String paramName = parameterNames.nextElement(); |
| 69 | + String paramValue = request.getParameter(paramName); |
| 70 | + |
| 71 | + if (containsSQLInjection(paramValue)) { |
| 72 | + response.sendError(400, "SQL Injection detected in parameter: " + paramName); |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + if (containsDangerousCharacters(paramValue)) { |
| 77 | + response.sendError(400, "Dangerous characters detected in parameter: " + paramName); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (containsXSS(paramValue)) { |
| 82 | + response.sendError(400, "XSS attack detected in parameter: " + paramName); |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + if (!isValidUTF8(paramValue)) { |
| 87 | + response.sendError(400, "Invalid UTF-8 encoding detected in parameter: " + paramName); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + if (containsNewLine(paramValue) || containsNullByte(paramValue)) { |
| 92 | + response.sendError(400, "Invalid characters detected in parameter: " + paramName); |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Validates uploaded files for allowed extensions. |
| 102 | + * |
| 103 | + * @param request the HTTP request object |
| 104 | + * @return true if all files have valid extensions, false otherwise |
| 105 | + * @throws IOException in case of file handling errors |
| 106 | + */ |
| 107 | + private boolean validateFileInput(HttpServletRequest request) throws IOException { |
| 108 | + if (request instanceof MultipartHttpServletRequest) { |
| 109 | + MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; |
| 110 | + |
| 111 | + java.util.Iterator<String> fileNames = multiRequest.getFileNames(); |
| 112 | + while (fileNames.hasNext()) { |
| 113 | + String fileName = fileNames.next(); |
| 114 | + String fileExtension = getFileExtension(fileName); |
| 115 | + |
| 116 | + if (!isValidFileExtension(fileExtension)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Extracts the file extension from a filename. |
| 126 | + * |
| 127 | + * @param filename the name of the file |
| 128 | + * @return the file extension, or an empty string if none is found |
| 129 | + */ |
| 130 | + private String getFileExtension(String filename) { |
| 131 | + if (filename == null || filename.isEmpty()) return ""; |
| 132 | + int lastIndexOfDot = filename.lastIndexOf("."); |
| 133 | + return (lastIndexOfDot == -1) ? "" : filename.substring(lastIndexOfDot + 1); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Checks if the file extension is allowed. |
| 138 | + * |
| 139 | + * @param fileExtension the file extension to check |
| 140 | + * @return true if the extension is allowed, false otherwise |
| 141 | + */ |
| 142 | + private boolean isValidFileExtension(String fileExtension) { |
| 143 | + for (String allowedExtension : ALLOWED_FILE_EXTENSIONS) { |
| 144 | + if (allowedExtension.equalsIgnoreCase(fileExtension)) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + } |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Validates input for SQL injection patterns. |
| 153 | + * |
| 154 | + * @param input the input string to validate |
| 155 | + * @return true if the input contains SQL injection patterns, false otherwise |
| 156 | + */ |
| 157 | + private boolean containsSQLInjection(String input) { |
| 158 | + return input != null && Pattern.compile(SQL_INJECTION_PATTERN).matcher(input).matches(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Checks if the input contains dangerous characters. |
| 163 | + * |
| 164 | + * @param input the input string to validate |
| 165 | + * @return true if dangerous characters are detected, false otherwise |
| 166 | + */ |
| 167 | + private boolean containsDangerousCharacters(String input) { |
| 168 | + return input != null && Pattern.compile(DANGEROUS_CHARACTERS_PATTERN).matcher(input).matches(); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Validates input for Cross-Site Scripting (XSS) patterns. |
| 173 | + * |
| 174 | + * @param input the input string to validate |
| 175 | + * @return true if XSS patterns are detected, false otherwise |
| 176 | + */ |
| 177 | + private boolean containsXSS(String input) { |
| 178 | + return input != null && Pattern.compile(XSS_PATTERN).matcher(input).matches(); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Checks if the input is valid UTF-8 encoded. |
| 183 | + * |
| 184 | + * @param input the input string to validate |
| 185 | + * @return true if the input is valid UTF-8 encoded, false otherwise |
| 186 | + */ |
| 187 | + private boolean isValidUTF8(String input) { |
| 188 | + return input == null || Pattern.compile(UTF8_PATTERN).matcher(input).matches(); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Checks if the input contains newline characters. |
| 193 | + * |
| 194 | + * @param input the input string to validate |
| 195 | + * @return true if newline characters are detected, false otherwise |
| 196 | + */ |
| 197 | + private boolean containsNewLine(String input) { |
| 198 | + return input != null && Pattern.compile(NEWLINE_PATTERN).matcher(input).matches(); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Checks if the input contains null byte characters. |
| 203 | + * |
| 204 | + * @param input the input string to validate |
| 205 | + * @return true if null byte characters are detected, false otherwise |
| 206 | + */ |
| 207 | + private boolean containsNullByte(String input) { |
| 208 | + return input != null && input.contains(NULL_BYTE_PATTERN); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
| 213 | + // This method can be used for additional processing after the controller handles the request. |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
| 218 | + // This method can be used for cleanup activities after the request has been completed. |
| 219 | + } |
| 220 | +} |
0 commit comments