-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The flag turns on at the router. The factory below it does not.\n\nThe mode switch is one environment variable: `ENABLE_MULTI_TENANT=true`. The `.env.example` in v2.51.1 says, in full, \"When enabled, n8n API tools will be available for all sessions, and instance configuration will be determined from HTTP headers.\" That is the entire pre-patch specification of what the flag does. The line after it reads `Default: false (single-tenant mode using environment variables)`.\n\nSingle-tenant mode is the original product. The operator runs n8n-MCP next to their own n8n, sets `N8N_API_URL` and `N8N_API_KEY` in the operator's process environment, and every MCP tool call routes through that one credential to that one instance. Multi-tenant mode was added later. It reuses the same handler stack and the same credential factory; the only thing the multi-tenant code path does differently is read tenant identity off the request headers.\n\nReading the flag is local to `src/http-server-single-session.ts`. The `POST /mcp` handler extracts `headers['x-n8n-url']` into `hasUrl` and `headers['x-n8n-key']` into `hasKey`. If `hasUrl || hasKey`, the handler builds an `InstanceContext`, validates it with `validateInstanceContext()`, and passes the context to `handleRequest`. If neither header is present, `instanceContext` stays `undefined` and `handleRequest(req, res, undefined)` runs anyway.\n\nThe handler stack downstream of `handleRequest` was written for the single-tenant code path. `getN8nApiClient(context)` in `src/mcp/handlers-n8n-manager.ts` is one of its entry points. The pre-patch shape, at v2.51.1 line 277:\n\n```typescript\nexport function getN8nApiClient(context?: InstanceContext): N8nApiClient | null {\n if (context?.n8nApiUrl && context?.n8nApiKey) {\n // ... build instance-specific client from context ...\n return client;\n }\n\n // Fall back to default singleton from environment\n logger.info('Falling back to environment configuration for n8n API client');\n const config = getN8nApiConfig();\n // ... build env-derived client ...\n return defaultApiClient;\n}\n```\n\nThe first branch is the multi-tenant path: a context with both fields builds a per-tenant client. The fall-through is the single-tenant path: no caller config, use the operator's env. The factory has no third branch. It has no parameter for \"we are in multi-tenant mode, fail closed instead of falling through.\" It does not read `process.env.ENABLE_MULTI_TENANT`. The flag is read above, at the router, where header extraction happens. It is not read here, where the credential decision happens. This is the [Fail Open Intercept](/patterns/fail-open-intercept) shape: the gate cannot identify a tenant for the request and forwards the request anyway, against the operator's default client.\n\nWhen a tenant request lands without headers and `getN8nApiClient(undefined)` runs, the factory's `Falling back to environment configuration` log line fires at log level INFO, not WARN, not ERROR, and the function returns a fully constructed `N8nApiClient` pointing at the operator's n8n with the operator's API key. The tenant's next MCP tool call executes against the operator's instance.\n\n## The request flow when both tenant headers are absent\n\nExploitation requires no exploit primitive. The PoC is the absence of two HTTP headers.\n\nThe tenant authenticates to whichever bearer the multi-tenant deployment uses to identify tenants on its frontend (the bearer is per-tenant identity, not per-instance credential). It then sends:\n\n```http\nPOST /mcp HTTP/1.1\nAuthorization: Bearer \nContent-Type: application/json\n\n{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"n8n_list_workflows\",\"arguments\":{}}}\n```\n\nNo `x-n8n-url`. No `x-n8n-key`. `extractMultiTenantHeaders(req)` returns both as `undefined`. The pre-patch HTTP handler's `if (hasUrl || hasKey)` branch is skipped. `instanceContext` is `undefined`. `handleRequest` dispatches the tool call to `handleListWorkflows(args, undefined)`. That handler calls `ensureApiConfigured(undefined)`, which calls `getN8nApiClient(undefined)`, which falls through to env and returns the operator's client.\n\nThe server then issues `GET https://operator-n8n.example.com/api/v1/workflows` with `X-N8N-API-KEY: ` and hands every workflow on the operator's instance back to the tenant. Subsequent tool calls run against the same client: `n8n_create_workflow`, `n8n_delete_workflow`, `n8n_manage_credentials`, `n8n_trigger_webhook`. The operator's n8n audit log records the requests as the operator's own MCP service session.\n\n`n8n_create_workflow` is the obvious primitive. n8n workflows can perform arbitrary HTTP calls, run JavaScript in Code nodes, exfiltrate credentials stored in n8n's credential store, and trigger any webhook the operator's instance exposes. CVE-2026-45707 rates as CVSS 8.1 with `PR:L` because the precondition is \"authenticated MCP tenant.\" Every other condition the design assumed (the headers will always be there; tenants will never call without them) was documentation.\n\nThe single-header case fails the same way. If a request arrives with `x-n8n-url` set and `x-n8n-key` absent, the HTTP handler enters the `if (hasUrl || hasKey)` branch and builds a candidate context with `n8nApiUrl` set and `n8nApiKey` undefined. `validateInstanceContext()` only validates fields that are not `undefined`; the undefined key field is skipped, validation passes, the partial context flows to `handleRequest`. The factory's first branch (`context?.n8nApiUrl && context?.n8nApiKey`) requires both, so it falls through to env. The operator's credentials authorize the call again. The \"supplied only one of them\" failure mode the advisory names lives at the factory, not at the HTTP edge; the post-patch flag check in `getN8nApiClient` is what catches it.\n\n## The two-layer fix was three commits\n\nThe merge commit on v2.51.2 is titled \"Merge commit from fork\" and squashes three security commits authored in response to GHSA-jxx9-px88-pj69. The first commit closes the bug. The second and third close the bug again, in different files, after the reviewer reading each patch noticed one more leak path.\n\n**Commit 1, the two-layer fix.** The HTTP edge gets the explicit guard:\n\n```typescript\nif (process.env.ENABLE_MULTI_TENANT === 'true' && !hasUrl && !hasKey) {\n logger.warn('Multi-tenant request missing tenant headers', {\n hasUrl: false,\n hasKey: false\n });\n res.status(400).json({\n jsonrpc: '2.0',\n error: {\n code: -32602,\n message: 'Multi-tenant headers required'\n },\n id: req.body?.id ?? null\n });\n return;\n}\n```\n\nThat is layer one. Layer two is `getN8nApiClient`, with the factory finally reading the flag the router was already reading:\n\n```typescript\n// SECURITY (GHSA-jxx9-px88-pj69): never fall back to process-level credentials\n// when multi-tenant mode is enabled. A missing or incomplete tenant context\n// must result in no client, not the operator's N8N_API_KEY.\nif (process.env.ENABLE_MULTI_TENANT === 'true') {\n logger.warn('Refusing env-credential fallback in multi-tenant mode');\n return null;\n}\n```\n\nThe commit message names layer two as \"protecting every caller of the factory.\" That phrasing presumes the codebase has more callers than the documented HTTP flow. It does.\n\n**Commit 2, the reviewer reads the response payloads.** The follow-up commit lists two paths the first fix did not touch. `src/triggers/handlers/base-handler.ts` has its own context-or-env fallback in `getBaseUrl()` and `getApiKey()`, the same shape as the factory, written separately for the trigger-handler code path. In multi-tenant mode without context, both methods returned the operator's URL and key. The fix adds the same flag check to both. The commit message calls this \"the latent footgun any future caller that instantiates a handler outside `ensureApiConfigured()` would otherwise reintroduce.\"\n\nThe same commit identifies four MCP tool handlers, `handleHealthCheck`, `handleDiagnostic`, `handleDeployTemplate`, and `handleAuditInstance`, that called `getN8nApiConfig()?.baseUrl` and put the result in their response payloads. In multi-tenant mode with tenant context, the response leaked the operator's URL where the tenant's URL was the correct answer; without context, it leaked the operator's URL period. The fix is a new helper, `resolveN8nApiConfigForResponse(context)`, that centralizes the decision and falls back to env only in single-tenant mode. The four call sites get rewritten to use the helper.\n\n**Commit 3, the reviewer reads the response payload one more time.** The third commit message begins, \"Reviewer caught a third leak path the prior commit (f3c9371) missed: `handleDiagnostic`'s response includes a verbatim copy of `process.env.N8N_API_URL` and a boolean derived from `N8N_API_KEY` under `data.environment.*`, separate from `data.apiConfiguration.*` that the helper already covers.\" The previous commit had centralized one of the two leak paths inside `handleDiagnostic`. The other path, a dictionary literal named `envVars` that read directly from `process.env`, was visually adjacent to the rewritten code and was not rewritten:\n\n```typescript\nconst isMultiTenant = process.env.ENABLE_MULTI_TENANT === 'true';\nconst envVars = {\n N8N_API_URL: isMultiTenant ? null : (process.env.N8N_API_URL || null),\n N8N_API_KEY: isMultiTenant ? null : (process.env.N8N_API_KEY ? '***configured***' : null),\n NODE_ENV: process.env.NODE_ENV || 'production',\n // ...\n};\n```\n\nThe same commit notes that the existing regression test only stringified `data.apiConfiguration` and therefore missed the second leak. The test is strengthened to stringify the full `result.data` and to assert against both `'https://n8n.test.com'` and `'***configured***'`.\n\nThree commits. Four code paths. One pattern. The fall-through to `process.env` reflex was written once into the single-tenant codebase and duplicated, with minor variation, across the credential factory, the trigger-handler base class, the helper that fills response payloads, and the dictionary literal that fills one specific response payload twice. Each reviewer pass found one. The patch is not one place. The patch is every place.\n\n## The trust artifact is the operator's N8N_API_KEY\n\nn8n-MCP's multi-tenant mode is an MCP server that brokers AI-assistant access to many tenants' n8n instances through one deployed service. The operator runs the service. The operator pays the bills. The operator also wires their own n8n into the process environment, because the documentation says the way to give the server a working n8n connection is `N8N_API_URL` and `N8N_API_KEY`. Setting those variables is how the operator stands up the deployment.\n\nThat credential is the trust artifact. It authorizes the MCP server to manage workflows on the operator's n8n. The operator's threat model places it inside the trust boundary of the deployment, not on the tenant-facing edge. When a tenant request lands without headers and the factory falls through, the operator's own credential is what authorizes the tenant's next workflow creation, credential read, or audit-log review.\n\nThis is not credential theft. The operator's API key is being used by the MCP server, which is the system the operator deployed and the system that holds the key. The bug is that the system was supposed to use that key on behalf of the operator and instead uses it on behalf of whichever tenant sent the empty headers. The trust the operator placed in their own n8n by exporting `N8N_API_KEY` to the process environment was inverted into a tenant primitive at the moment `ENABLE_MULTI_TENANT=true` flipped the same factory function from \"the operator is the only caller\" to \"anyone is a possible caller, and missing context still means the operator.\" This is the [Trust Inversion](/patterns/trust-inversion) shape applied to a credential the operator exported into a process that they no longer control single-handed.\n\nThe pre-patch `.env.example` documents what multi-tenant does. It does not document what multi-tenant does when the headers are missing. Neither did the code. The post-patch `.env.example` adds three sentences that should have been there from the first commit that named the flag:\n\n> Requests MUST supply both x-n8n-url and x-n8n-key headers; requests without complete tenant headers are rejected with HTTP 400. The process-level N8N_API_URL / N8N_API_KEY are NOT used as a fallback.\n\nThe documentation update is the testimony. The pre-patch behavior was unspecified because the developer assumed the headers would always be there. The factory was unguarded because the flag was assumed to be a routing concern. The gate that should have ended the request at the HTTP edge instead ran the request as the operator, and four call sites in the same codebase repeated the same single-tenant-fallback reflex on the same flag the router was already reading.\n\nPoC: [GHSA-jxx9-px88-pj69](https://github.com/advisories/GHSA-jxx9-px88-pj69).","closing_line":"A multi-tenant gate that falls back to single-tenant defaults when the tenant cannot be identified is not a multi-tenant gate. It is a single-tenant gate with a per-request preference.","hook_md":"n8n-MCP's HTTP server reads `x-n8n-url` and `x-n8n-key` off each inbound request and uses them to construct a per-tenant client to that tenant's n8n instance. When both headers are absent, the call into `getN8nApiClient(undefined)` in `src/mcp/handlers-n8n-manager.ts` does not return `null`. It returns a client built from the operator's process-level `N8N_API_URL` and `N8N_API_KEY`.\n\nCVE-2026-45707 is what happens when the multi-tenant boundary is enforced at the HTTP entry point and the credential factory three call frames below never reads the flag.","post_id":568,"slug":"n8n-mcp-cve-2026-45707-factory-doesnt-read-the-flag","title":"CVE-2026-45707: n8n-MCP's Router Reads ENABLE_MULTI_TENANT. The Credential Factory Below It Does Not.","type":"initial","unreadable_sentence":"`getN8nApiClient(undefined)` does not return `null`. It returns a client built from the operator's `N8N_API_KEY`."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCarA0XgAKCRDeZjl4jgkQ JpO/AQD8IyZWXjRwtTn/BH6hkWDpItsabqmwdM3IsH2bJ/cMRAEApLp/abcfZ/BQ U4xAoG/R7Lf27ZdDBMiiyZ9w6qJSbQU= =9ne9 -----END PGP SIGNATURE-----