I'm trying to match pattern for sql injection prevention. However, this test case failed.
@Test void testDropPattern() { var string = " {\r\n" + " \"fullName\": \"Peter Parker\",\r\n" + " \"email\": \"[email protected]\",\r\n" + " \"birthDate\": \"2000-12-31\",\r\n" + " \"gender\": \"M', '2000-12-31'); DROP table users --\"\r\n" + " }\r\n" + "\r\n"; var regex = "(?i)(.*)(\\b)+DROP(\\b)+\\s.*(.*)"; var pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); assertTrue(pattern.matcher(string).matches()); } It is broken because there is newline character (\r\n) at the end of the string. This is the original json body with newline.
{ "fullName": "Peter Parker", "email": "[email protected]", "birthDate": "2000-12-31", "gender": "M', '2000-12-31'); DROP table users --" } What regex that can be used to handle this use case?