Container Escape in Express with Cockroachdb
Container Escape in Express with Cockroachdb — how this specific combination creates or exposes the vulnerability
A container escape in an Express service that uses CockroachDB typically occurs when an attacker who has compromised the application layer leverages database interactions to break out of the container’s runtime constraints. This combination is notable because CockroachDB exposes a PostgreSQL-compatible wire protocol, and Express applications often interact with it using standard PostgreSQL clients. If the application dynamically constructs SQL queries using unsanitized user input, an attacker can exploit this to execute operating system commands via database extensions or procedural languages supported by CockroachDB, such as using crdb_internal or leveraging external network capabilities to reach the host filesystem or other containers.
For example, an endpoint that builds a query using string concatenation can become a vector for command execution through database functions if the underlying container grants the process network and filesystem access. Consider an Express route that accepts a table name and passes it directly into a SQL string:
app.get('/users/:table', async (req, res) => {
const { table } = req.params;
const result = await pool.query(`SELECT * FROM ${table}`);
res.json(result.rows);
});
If the container runs with elevated privileges or shares the host network, crafted input can lead to operations that read sensitive host files or probe internal services, effectively escaping the container boundary. The risk is amplified when the CockroachDB connection string is mounted as a volume or exposed via environment variables within the container, allowing lateral movement. Because middleBrick tests input validation and property authorization across 12 checks in parallel, it can identify endpoints where user-controlled data reaches database queries without sanitization, highlighting paths that could facilitate container escape in this specific stack.
Additionally, unauthenticated LLM endpoints in Express applications that proxy to CockroachDB can expose system prompts or internal logic that describe database interactions, aiding an attacker in crafting escape techniques. MiddleBrick’s LLM security checks specifically scan for such leakage and active prompt injection probes, providing findings that map to OWASP API Top 10 and help prioritize fixes for this attack surface.
Cockroachdb-Specific Remediation in Express — concrete code fixes
Remediation centers on strict input validation, parameterized queries, and limiting database permissions within the container. Always use prepared statements or ORM query builders that treat user input as data, not executable code. For CockroachDB in Express, prefer the pg client with parameterized statements:
const { Pool } = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
app.get('/users/:id', async (req, res) => {
const { id } = req.params;
const result = await pool.query('SELECT * FROM users WHERE id = $1', [id]);
res.json(result.rows);
});
If dynamic table or column names are unavoidable, validate them against an allowlist before use:
const allowedTables = ['users', 'products', 'orders'];
app.get('/data/:table', async (req, res) => {
const { table } = req.params;
if (!allowedTables.includes(table)) {
return res.status(400).json({ error: 'Invalid table' });
}
const result = await pool.query(`SELECT * FROM ${table}`);
res.json(result.rows);
});
Ensure the container runs with a non-root user and minimal filesystem capabilities. In your Dockerfile, set:
FROM node:18-alpine
RUN adduser --disabled-password appuser
USER appuser
WORKDIR /app
COPY --chown=appuser:appuser . .
CMD ["node", "server.js"]
Also restrict CockroachDB user permissions to the least privilege necessary—avoid granting superuser rights to the application role. Use role-based access control and network policies to limit which pods can reach the database endpoint. middleBrick’s continuous monitoring in the Pro plan can track configuration drift and alert if new endpoints bypass these safeguards, while the GitHub Action can fail builds when insecure patterns are detected in pull requests.
Finally, for applications leveraging CockroachDB’s advanced features, ensure procedural languages and extensions are disabled unless explicitly required. This reduces the attack surface that could be leveraged for container escape through the database layer.