Summary
Marten contains SQL injection vulnerabilities in several LINQ and tenant-management paths that interpolate runtime strings into single-quoted SQL literals without reliable escaping or parameterization. If an attacker can influence a dictionary key, tenant value, or related filter input, they may bypass authorization, extract data, or, in some database configurations, modify data.
Affected context
The issue affects Marten deployments using vulnerable LINQ, dictionary, event-store, or tenant-management paths where runtime values are incorporated into generated PostgreSQL statements. Relevant contexts include dictionary indexer keys in Where filters, selected projection or tenant-partition operations, and certain ContainsKey calls when the Newtonsoft.Json serializer is used. The related System.Text.Json handling for ContainsKey is not affected according to the advisory.
Exploit preconditions
- Attack surface: Application endpoints, background operations, or APIs that expose affected Marten query and tenant-management functionality
- Required access: The ability to supply or influence a runtime filter value, dictionary key, tenant identifier, or related operation input
- Trigger point: SQL generation and execution in affected LINQ, event-store, dictionary, or tenant-partition code paths
- Why exploitation is possible: Runtime strings are inserted into single-quoted SQL literals without consistent parameterization or escaping, allowing input to alter the intended SQL expression
- Additional condition for data modification: The database driver and deployment configuration must permit the resulting statement behavior, including any applicable restrictions on batched statements
Why this happens
The underlying bug is an unsafe SQL construction pattern. A runtime-controlled string is interpolated into a single-quoted SQL literal instead of being represented as a bound database parameter or safely encoded according to the database driver's rules.
This behavior maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'). The same class of flaw appears across multiple sinks, including dictionary item filters, projection parsing, tenant partition management, tenant deletion operations, and related event-loading logic. Validation performed after SQL text construction cannot reliably compensate for unsafe query generation.
Potential impact
- An attacker may bypass tenant or filter-based authorization controls.
- Sensitive records may be disclosed through blind data-extraction behavior.
- Query results may be altered, causing incorrect application decisions or data exposure.
- Data may be modified or deleted where statement batching and database permissions allow it.
- Cross-tenant isolation may be weakened.
- Compromised application credentials may provide broader database access than intended.
General mitigation guidance
Mitigation generally focuses on ensuring that all runtime values remain data rather than SQL syntax.
Common defensive patterns include:
- Use parameterized queries and expression translators that bind values through the database provider.
- Centralize SQL generation so every literal-producing path follows the same safe parameter-binding policy.
- Avoid manual quoting, string interpolation, and concatenation when constructing SQL.
- Apply strict allowlists for structural inputs such as field names, operation types, and tenant selectors; treat values as untrusted by default.
- Prefer serializers and query paths that preserve typed values without converting them into executable SQL fragments.
- Enforce least-privilege database roles, restrict write and administrative permissions, and separate tenant data access where practical.
- Disable unnecessary multi-statement behavior and apply database policies that limit destructive operations.
How teams detect issues like this
Detection can combine source review, automated analysis, and runtime monitoring.
- Use static analysis rules to identify SQL strings built through interpolation, concatenation, or formatting with runtime values.
- Review LINQ translators, tenant-management code, event loaders, and serializer-specific branches for generated SQL literals.
- Flag manual quote handling and APIs that accept raw SQL fragments without parameter metadata.
- Add code-review policies requiring parameter binding for all user-, tenant-, and request-derived database values.
- Test authorization boundaries with negative cases involving special characters and cross-tenant identifiers, without relying on production data.
- Monitor database logs for unexpected statement structure, unusual quoting patterns, multi-statement requests, and access across tenant boundaries.
- Neutral automated security review tools may help identify suspicious SQL construction paths before deployment.

