GHSA-qw6m-8fw2-2v64 Budibase: NoSQL Injection via JSON Parameter Interpolation in MongoDB Query Execution
SummaryBudibase's MongoDB query execution endpoint (`POST /api/v2/queries/:queryId`) is vulnerable to NoSQL injection through user-supplied query parameters. The `enrichContext()` function interpolates parameter values into JSON query templates using Handlebars with `noEscaping: true`, then parses the result with `JSON.parse()`. An attacker can inject JSON metacharacters (`"`, `{`, `}`) into parameter values to alter the structure of MongoDB queries, bypassing intended filters to read, modify, or delete arbitrary documents.DetailsThe vulnerability exists because input validation and interpolation are misaligned. The `validateQueryInputs()` function blocks Handlebars template syntax (`{{}}`) but does not sanitize JSON structural characters:**packages/server/src/api/controllers/query/index.ts:57-69**After validation passes, `enrichContext()` performs raw string interpolation with escaping explicitly disabled:**packages/server/src/sdk/workspace/queries/queries.ts:105-108**The interpolated string is then parsed as JSON at line 122:**packages/server/src/sdk/workspace/queries/queries.ts:122**The parsed object flows directly into MongoDB driver calls with no further sanitization:**packages/server/src/integrations/mongodb.ts:509****packages/server/src/integrations/mongodb.ts:624**Consider a saved query with a JSON template like `{"username": "{{username}}"}`. If an attacker provides the parameter value `", "$ne": "` the interpolated string becomes `{"username": "", "$ne": ""}` — a valid JSON object that matches all documents where `username` is not empty, instead of matching a single specific user.The route requires only `PermissionType.QUERY, PermissionLevel.WRITE` (packages/server/src/api/routes/query.ts:27), which is available to regular app users — not restricted to builders or admins. Critically, the execute endpoint has no Joi schema validation on the request body, unlike the save and preview endpoints.PoC**Prerequisites:** A Budibase instance with a MongoDB datasource and a saved query that accepts a parameter interpolated into the query JSON (e.g., a `find` query with `{"username": "{{username}}"}`).**Step 1: Authenticate as a regular app user****Step 2: Execute the query normally (returns only matching document)****Step 3: Inject NoSQL operator to dump all documents**The injected value `", "$ne": "` transforms the query from `{"username": "alice"}` to `{"username": "", "$ne": ""}`, which matches all documents where username is not empty.**Step 4: Delete all documents via a delete query (if a delete-type query is saved)**Impact**Data exfiltration:** Any app user with query write permission can bypass intended query filters to read all documents in a MongoDB collection, including sensitive data belonging to other users or tenants.**Data modification:** Through `updateMany` queries, attackers can modify arbitrary documents in bulk by injecting broadened filters.**Data destruction:** Through `deleteMany` queries, attackers can delete all documents matching an injected filter, potentially wiping entire collections.**Authorization bypass:** The attack requires only `QUERY WRITE` permission, which is a standard app-level permission — not builder or admin access. This means any regular application user can exploit saved MongoDB queries they have access to execute.Recommended FixSanitize parameter values before interpolation by escaping JSON metacharacters. Apply this in `enrichContext()` before the `processStringSync` call:**packages/server/src/sdk/workspace/queries/queries.ts**Alternatively, adopt a parameterized query approach: instead of string interpolation into JSON, parse the template JSON first and then inject parameter values into the parsed object at the value level, preventing any structural modification of the query.Additionally, add Joi validation to the execute endpoint (`POST /api/v2/queries/:queryId`) to constrain the shape of incoming parameter values, consistent with the validation already present on the save and preview endpoints.
CVSS v3.1 score: 8.3