Skip to content
SQL injection Write-up Intermediate

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.

pavel Follow

Published on gremwell.com

Spring applications quite often use @Query annotation. It helps to control requests executed by database servers, like customising what data to extract.

pavel, on gremwell.com

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

  1. Set up a Couchbase instance and create a bucket named 'demo'.
  2. Build and run the Spring application with the vulnerable @Query method.
  3. Send a request to the /vuln endpoint with a field value containing a single quote to trigger a database exception.
  4. 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.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.