Preventing SQL injection in queries, identifiers and stored procedures
Preventing SQL injection in queries, identifiers and stored procedures connects a security requirement to the behaviour of a real feature. The relevant controls need to remain effective across custom code, integrations and the production configuration. Understanding SQL injection in queries and identifiers and stored procedures helps establish which security property the feature needs and where that property is actually enforced.
What does this involve?
Input validation defines the structures, sizes and meanings an application expects to receive. It must happen before information influences a sensitive operation. Validation does not replace safe interpreter APIs, output encoding or permission checks, because those controls address different boundaries.
Impact: A successful SQL injection attack can, depending on database features and privileges, read data from the database, modify database data (insert/update/delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file existing on the DBMS file system or write files into the file system, and, in some cases, execute operating system commands.
A related class of vulnerability affects object-relational mapping (ORM) frameworks, such as JPA/Hibernate, which have their own query language (JPQL/HQL). Applications using these frameworks might be vulnerable to injections of these query languages:
Why it matters
Consider a realistic failure: a dynamic sorting or reporting feature reintroduces SQL structure assembled from untrusted input.
Untrusted input can change application behaviour, consume excessive resources or reach data and execution paths outside the intended feature. The practical result depends on where the value is used and which privileges are available there.
Implementation guidance should be applied to a real feature and its deployment. Framework defaults, custom handlers and integrated services all influence whether the intended protection remains effective.
How penetration testers check for it
Trace the relevant feature from its entry point to the data or operation it protects, including custom code and alternate paths.
- Trace input from every relevant entry point to its use.
- Check types, lengths, ranges and ambiguous representations.
- Compare parsing across components and alternate request formats.
- Use controlled invalid inputs to establish effects rather than assumptions.
How to fix it
Use the appropriate maintained mechanisms and verify the result in the actual production configuration.
- use fixed application-owned choices for dynamic table names, column names, sort order (ASC, DESC), etc.
- Use static SQL with parameters, or separately bind data values in internal dynamic queries instead of concatenating them into SQL text. Microsoft's secure dynamic SQL guidance calls for reviewing procedures that construct SQL and parameterizing their commands.
- for structural parts that cannot be parameterized (table names, column names, sort direction), map user choices to a strict allow-list of fixed, application-owned values.
- some databases have configuration which might affect the escaping behaviour (such as MySQL NO_BACKSLASH_ESCAPES).
- Reject unexpected structures before sensitive processing.
- Apply resource bounds and predictable failure handling.
Blocking a few known attack strings is not a complete validation strategy. The intended data model and downstream interpretation determine which protections are needed.
Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.
Related issues
- Building LDAP queries without injection vulnerabilities
- Preventing interpreter injection in Java application code
- SQL and NoSQL injection through API filters
Read about our methodology or contact pentested.com to discuss secure development and code review.