← Java / Spring · Contents

JAVA-008 / Beginner

Wildcard injection in Spring Data method names or @Query LIKE

A03:2021 Injection · CWE-89 SQL Injection (wildcard abuse)

Why is this a problem?

User-supplied % and _ expand the search scope and can cause large queries.

How to fix it

java
String escaped = q.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_");
repo.findByNameContainingIgnoreCase(escaped);  // Escape input and limit length

Further reading