Format String in Adonisjs with Cockroachdb
Format String in Adonisjs with Cockroachdb — how this specific combination creates or exposes the vulnerability
Format string vulnerabilities arise when user-controlled input is passed directly to formatting functions such as printf-style APIs without proper sanitization or type-safe parameterization. In AdonisJS, which is built on Node.js, the risk typically surfaces through database query construction or logging utilities that interpolate strings rather than using parameterized queries. When using CockroachDB with AdonisJS, the danger is not in CockroachDB itself but in how the application builds SQL strings. For example, if a developer constructs raw queries by concatenating user input into SQL text, an attacker may supply format specifiers like %s, %x, or even format metadata characters that can leak stack contents or cause crashes when the logging or query execution layer attempts to interpret them.
Consider an endpoint in AdonisJS that builds a SQL string using string interpolation before sending it to CockroachDB:
const userInput = req.input('search');
const query = `SELECT * FROM profiles WHERE name = '${userInput}'`;
const results = await Database.raw(query);
If userInput contains format-like sequences (e.g., %s), and the logging or database driver performs any internal formatting, it may interpret these specifiers and read from the stack or heap. CockroachDB, being wire-protocol compatible with PostgreSQL, will typically reject such inputs as invalid SQL syntax; however, the risk lies in the application layer's handling of data before it reaches CockroachDB. For instance, if AdonisJS utilities or third-party logging middleware apply console.log-style formatting to request data containing user-controlled format strings, the runtime may disclose memory contents or cause denial-of-service behavior.
The LLM/AI Security checks in middleBrick specifically test for system prompt leakage and output exposure. While this scenario is not about LLMs, it underscores the importance of ensuring that any data flowing to or from CockroachDB through AdonisJS does not rely on unsafe string construction. Even without direct code execution, format strings can lead to information disclosure or instability, which middleBrick would flag under Data Exposure and Input Validation checks.
To verify whether your AdonisJS application is vulnerable when interacting with CockroachDB, middleBrick can scan your endpoint without authentication. It will identify whether raw query construction is present and whether format-like patterns in inputs could be abused in the logging or execution path.
Cockroachdb-Specific Remediation in Adonisjs — concrete code fixes
Remediation centers on avoiding string interpolation for SQL and using type-safe query builders or parameterized queries. AdonisJS provides the Database module with built-in parameterization support. The following example demonstrates the correct way to query CockroachDB without risking format string issues:
const userInput = req.input('search');
const results = await Database.from('profiles')
.where('name', userInput)
.limit(10);
If you must use raw queries, always use parameterized bindings instead of string concatenation:
const userInput = req.input('search');
const results = await Database.raw(
'SELECT * FROM profiles WHERE name = $1 LIMIT $2',
[userInput, 10]
);
In CockroachDB, positional parameters like $1, $2 are supported, aligning with PostgreSQL-style placeholders. This ensures that user input is treated strictly as data and never as part of the SQL command structure, eliminating format string risks at the query layer.
Additionally, ensure that any logging or diagnostic output within your AdonisJS application sanitizes data before formatting. For example, avoid directly logging request payloads using console.log('Search:', searchTerm) if searchTerm comes from the client. Instead, use structured logging with explicit field names and validate input against expected patterns.
For comprehensive protection, combine these practices with middleBrick’s continuous monitoring (Pro plan) or CI/CD integration (GitHub Action). The scanner will flag unsafe query patterns and provide prioritized findings with remediation guidance, helping you maintain secure interactions with CockroachDB in AdonisJS.