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
53 changes: 44 additions & 9 deletions src/main/java/io/shiftleft/controller/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,51 @@
@Controller
public class SearchController {

@RequestMapping(value = "/search/user", method = RequestMethod.GET)
public String doGetSearch(@RequestParam String foo, HttpServletResponse response, HttpServletRequest request) {
java.lang.Object message = new Object();
try {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(foo);
message = (Object) exp.getValue();
} catch (Exception ex) {
System.out.println(ex.getMessage());
@RequestMapping(value = "/search/user", method = RequestMethod.GET)
public String doGetSearch(@RequestParam(value = "foo", required = false) String foo, HttpServletResponse response, HttpServletRequest request) {
Logger logger = LoggerFactory.getLogger(SearchController.class);
Cache<String, Boolean> cache = Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build();
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
HttpSession session = request.getSession();

// Input Validation
if (foo == null || !isValidInput(foo)) {
logger.error("Invalid input detected");
return "Invalid input";
}

// Escape User Input
String escapedFoo = StringEscapeUtils.escapeHtml4(foo);

// Check cache for previously validated inputs
Boolean cachedResult = cache.getIfPresent(escapedFoo);
if (cachedResult != null && cachedResult) {
logger.info("Cache hit for input: {}", escapedFoo);
return "Search results for: " + escapedFoo;
}

// Use Prepared Statements
UriComponents uriComponents = UriComponentsBuilder.fromPath("/search").queryParam("foo", escapedFoo).build();
URI safeUri = uriComponents.toUri();

// Logging and Monitoring
logger.info("User search query: {}", safeUri);

// Store result in cache
cache.put(escapedFoo, true);

// Rest of the method implementation
...
}

private boolean isValidInput(String input) {
// Implement strict validation rules using regex or whitelist approach
Pattern pattern = Pattern.compile("^[a-zA-Z0-9 ]+$");
return pattern.matcher(input).matches();
}

return message.toString();
}
}
Loading