Improper Spring @Query Usage Allows N1QL Injection
A Spring application used a @Query annotation that mixed SpEL and N1QL placeholders, allowing an attacker to break out of a string literal and inject N1QL commands. This could let an attacker extract arbitrary data from the Couchbase bucket.
Spring applications quite often use @Query annotation. It helps to control requests executed by database servers, like customising what data to extract.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the @Query annotation used the #{[1]} SpEL syntax to insert a parameter directly into the query string without proper escaping, unlike parameterised queries with $2.
Impact
An attacker could extract arbitrary data from the same Couchbase bucket, as demonstrated by extracting datastores information.
What the attacker needed
Access to the vulnerable endpoint; Couchbase backend with N1QL
The fix
The application should use parameterised queries with $ placeholders instead of SpEL syntax that inserts parameters directly into the query string.
How it was found
- Set up a Couchbase instance and create a bucket named 'demo'.
- Build and run the Spring application with the vulnerable @Query method.
- Send a request to the /vuln endpoint with a field value containing a single quote to trigger a database exception.
- Use N1QLMap with a prepared request file to automate extraction of data from the Couchbase bucket.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
The application built database queries by inserting user input directly into a string, without properly separating the input from the query code. An attacker could send a special character (like a single quote) to break out of the intended string and add their own database commands, potentially reading any data in the database.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.