← Java / Spring · Contents

JAVA-077 / Advanced

Dynamic table names using MyBatis <include>/${}

A03:2021 Injection · CWE-89 SQL Injection

Why is this a problem?

Table and column names cannot be bound, so input becomes an identifier directly.

How to fix it

java
String table = Map.of("user", "users", "order", "orders").get(type);  // Server-side allowlist
if (table == null) throw new IllegalArgumentException();
xml
SELECT * FROM ${tableName} WHERE id = #{id}   <!-- tableName must pass the allowlist -->

Further reading