From 1588b60938de640bb5f4bb086e5ad601cffa0ff8 Mon Sep 17 00:00:00 2001 From: rangarajan19 Date: Mon, 16 Nov 2026 22:44:23 +0530 Subject: [PATCH 1/3] Extend the secret-literal report/repair script to DATABASE base URLs Part 3 of #1245: a DATABASE connector created before #1677 still has its password in plaintext in base_url. report.ts now finds it (database_url_password finding, redacted); repair.ts moves it into encrypted auth_config, dry run by default, with a backup table before --apply. Both reuse moveUrlCredentials/maskUrlPassword from #1677 rather than reimplementing the move, so there is no new driver-specific logic to drift from the live API's behavior. Skips a connector whose auth_config already holds a username or password, same rule the API follows (which one wins then differs by driver). --- docs/connectors/database.md | 1 + llms-full.txt | 1 + .../backend/scripts/secret-literals/repair.ts | 71 ++++++++++++-- .../backend/scripts/secret-literals/report.ts | 14 ++- .../secret-literal-repair.integration.spec.ts | 73 +++++++++++--- .../secret-literal-scan.util.spec.ts | 94 +++++++++++++++++-- .../connectors/secret-literal-scan.util.ts | 54 ++++++++++- 7 files changed, 276 insertions(+), 32 deletions(-) diff --git a/docs/connectors/database.md b/docs/connectors/database.md index 40e129a67..31b280efc 100644 --- a/docs/connectors/database.md +++ b/docs/connectors/database.md @@ -74,6 +74,7 @@ You can type the username and password into the connection string, as above. Whe - An API request that sends `authConfig` replaces it whole, the moved username and password included. - No export contains a password that has moved, an admin export included, so a connector restored from a backup needs it typed in again. - A connection string the parser cannot read, such as a multi-host MongoDB URL typed with its credentials, keeps them in place; the password is shown as `********`. +- A connector created before this existed still has its password in the connection string until it is next saved. `scripts/secret-literals/report.ts` finds these (and the same literal-secret cases other connector types have); `repair.ts` moves them, dry run by default, with a backup table before `--apply`. Connectors saved before v0.55.0 kept `user:password@` in the stored connection string. The backend moves those too: every start checks the stored database connection strings once and moves the credentials it still finds, the same way a save does. This needs no setting and works the same on AnythingMCP Cloud and self-hosted. The backend log shows the result with connector ids and counts, never a connection string: diff --git a/llms-full.txt b/llms-full.txt index 6ac3fc3d2..c3713640c 100644 --- a/llms-full.txt +++ b/llms-full.txt @@ -1030,6 +1030,7 @@ You can type the username and password into the connection string, as above. Whe - An API request that sends `authConfig` replaces it whole, the moved username and password included. - No export contains a password that has moved, an admin export included, so a connector restored from a backup needs it typed in again. - A connection string the parser cannot read, such as a multi-host MongoDB URL typed with its credentials, keeps them in place; the password is shown as `********`. +- A connector created before this existed still has its password in the connection string until it is next saved. `scripts/secret-literals/report.ts` finds these (and the same literal-secret cases other connector types have); `repair.ts` moves them, dry run by default, with a backup table before `--apply`. Connectors saved before v0.55.0 kept `user:password@` in the stored connection string. The backend moves those too: every start checks the stored database connection strings once and moves the credentials it still finds, the same way a save does. This needs no setting and works the same on AnythingMCP Cloud and self-hosted. The backend log shows the result with connector ids and counts, never a connection string: diff --git a/packages/backend/scripts/secret-literals/repair.ts b/packages/backend/scripts/secret-literals/repair.ts index f2fd7ebd7..a0fcf04e3 100644 --- a/packages/backend/scripts/secret-literals/repair.ts +++ b/packages/backend/scripts/secret-literals/repair.ts @@ -11,7 +11,13 @@ * regenerated from the cleaned path; * - config.baseUrlBaseline is cleaned the same way; * - a catalog connector's own headers are left alone (they are rebuilt - * from the adapter's template when its variables change). + * from the adapter's template when its variables change); + * - a DATABASE connector's `user:password@` moves into auth_config + * (re-encrypted with ENCRYPTION_KEY, same column the live API now writes + * on every create/update — see database-url-credentials.util.ts), unless + * auth_config already holds a username or password: which one wins then + * differs by driver, so moving it could change who connects, exactly the + * rule the API itself follows. * * DRY RUN BY DEFAULT: prints what it would change (redacted) and writes * nothing. With --apply it first copies every row it is about to change into @@ -24,7 +30,8 @@ * in-memory tool registry: restart it (or reload the affected connectors). * * Rollback, using the backup table the run prints: - * UPDATE connectors c SET base_url=b.base_url, headers=b.headers, config=b.config, env_vars=b.env_vars + * UPDATE connectors c SET base_url=b.base_url, headers=b.headers, config=b.config, + * env_vars=b.env_vars, auth_config=b.auth_config * FROM b WHERE b.kind='connector' AND b.id=c.id; * UPDATE mcp_tools t SET name=b.name, description=b.description, parameters=b.parameters, * endpoint_mapping=b.endpoint_mapping @@ -38,8 +45,35 @@ import { type ToolRow, } from '../../src/connectors/secret-literal-scan.util'; import { openEnvVarsStrict, sealEnvVars } from '../../src/common/crypto/env-vars-at-rest'; +import { decrypt, encrypt } from '../../src/common/crypto/encryption.util'; -const GUARDED = `('REST','SOAP','GRAPHQL','MCP','ODATA')`; +const GUARDED = `('REST','SOAP','GRAPHQL','MCP','ODATA','DATABASE')`; + +/** + * auth_config's own format (connectors.service.ts): a plain ciphertext + * string, no `$enc` wrapper and no AAD — unlike env_vars. `null` means "not + * decrypted yet" (the two-phase trick `planRepair` relies on); a value that + * fails to decrypt is reported as `null` too, so the caller can tell it + * apart from "genuinely holds nothing" and skip the row instead of writing + * an empty authConfig over a real one. + */ +function openAuthConfigStrict(value: unknown, encryptionKey: string): Record | null { + if (value === null || value === undefined) return {}; + if (typeof value !== 'string' || !value) return {}; + try { + const parsed = JSON.parse(decrypt(value, encryptionKey)); + return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null; + } catch { + return null; + } +} + +/** Read lazily, like env-vars-at-rest.ts's own key(): only a run that actually touches a DATABASE connector needs it set. */ +function encryptionKey(): string { + const value = process.env.ENCRYPTION_KEY; + if (!value) throw new Error("ENCRYPTION_KEY is not set: a DATABASE connector's credentials cannot be moved"); + return value; +} export interface RepairOptions { apply?: boolean; @@ -87,7 +121,7 @@ export async function repair(client: Client, opts: RepairOptions = {}): Promise< try { const connectors = ( await client.query( - `SELECT c.id, c.organization_id, c.name, c.type::text AS type, c.base_url, c.headers, c.config, c.env_vars + `SELECT c.id, c.organization_id, c.name, c.type::text AS type, c.base_url, c.headers, c.config, c.env_vars, c.auth_config FROM connectors c WHERE ${where} ${opts.apply ? 'FOR UPDATE' : ''}`, params, ) @@ -116,7 +150,8 @@ export async function repair(client: Client, opts: RepairOptions = {}): Promise< const result: RepairResult = { plans: [], skipped: [] }; for (const c of connectors) { // Planned without the variables first: most connectors have nothing to - // move, and their env_vars are then never decrypted. + // move, and their env_vars (or a DATABASE connector's auth_config) are + // then never decrypted. const row: ConnectorRow = { id: c.id, organizationId: c.organization_id, @@ -126,9 +161,22 @@ export async function repair(client: Client, opts: RepairOptions = {}): Promise< headers: c.headers, config: c.config, envVars: null, + authConfig: null, }; const ownTools = toolsByConnector.get(c.id) ?? []; if (!planRepair(row, ownTools)) continue; + + if (c.type === 'DATABASE') { + const authConfig = openAuthConfigStrict(c.auth_config, encryptionKey()); + if (authConfig === null) { + result.skipped.push({ connectorId: c.id, reason: 'auth_config could not be decrypted (ENCRYPTION_KEY?)' }); + continue; + } + const plan = planRepair({ ...row, authConfig }, ownTools); + if (plan) result.plans.push(plan); + continue; + } + const env = openEnvVarsStrict(c.env_vars); if (env === null) { result.skipped.push({ connectorId: c.id, reason: 'env_vars could not be decrypted (ENCRYPTION_KEY?)' }); @@ -147,14 +195,16 @@ export async function repair(client: Client, opts: RepairOptions = {}): Promise< await client.query( `CREATE TABLE ${table} ( kind text NOT NULL, id text NOT NULL, base_url text, headers jsonb, config jsonb, - env_vars jsonb, name text, description text, parameters jsonb, endpoint_mapping jsonb, + env_vars jsonb, auth_config text, name text, description text, parameters jsonb, endpoint_mapping jsonb, backed_up_at timestamptz NOT NULL DEFAULT now())`, ); const connectorIds = result.plans.map((p) => p.connectorId); const toolIds = result.plans.flatMap((p) => p.tools.map((t) => t.id)); await client.query( - `INSERT INTO ${table} (kind, id, base_url, headers, config, env_vars) - SELECT 'connector', id, base_url, headers, config, env_vars FROM connectors WHERE id = ANY($1)`, + // auth_config is copied as the ciphertext string it already is — no + // decrypt/re-encrypt round trip for the backup itself. + `INSERT INTO ${table} (kind, id, base_url, headers, config, env_vars, auth_config) + SELECT 'connector', id, base_url, headers, config, env_vars, auth_config FROM connectors WHERE id = ANY($1)`, [connectorIds], ); if (toolIds.length > 0) { @@ -178,6 +228,11 @@ export async function repair(client: Client, opts: RepairOptions = {}): Promise< if (plan.connector.envVars !== undefined) { set('env_vars', sealEnvVars(plan.connector.envVars), true); } + if (plan.connector.authConfig !== undefined) { + // auth_config is a plain text column (the ciphertext itself), not + // jsonb, unlike env_vars — see schema.prisma and connectors.service.ts. + set('auth_config', encrypt(JSON.stringify(plan.connector.authConfig), encryptionKey())); + } if (sets.length > 0) { values.push(plan.connectorId); await client.query(`UPDATE connectors SET ${sets.join(', ')}, updated_at = now() WHERE id = $${values.length}`, values); diff --git a/packages/backend/scripts/secret-literals/report.ts b/packages/backend/scripts/secret-literals/report.ts index cc8498636..132440555 100644 --- a/packages/backend/scripts/secret-literals/report.ts +++ b/packages/backend/scripts/secret-literals/report.ts @@ -1,14 +1,20 @@ /** * READ-ONLY report: connectors and tools whose base URL, headers, paths, * query parameters, parameter defaults or names hold a secret-looking - * literal (see src/common/secret-literals.util.ts). + * literal (see src/common/secret-literals.util.ts), plus a DATABASE + * connector whose connection string still carries its password (see + * src/connectors/database-url-credentials.util.ts) — a `database_url_password` + * finding, for a row from before that move was added. * * Prints one JSON line per finding, then a summary. A finding names the * connector, the workspace, the field and a redacted pattern - * (`bot<10 digits>:***(35)`); the secret itself is never printed. + * (`bot<10 digits>:***(35)`, or the DATABASE connection string with its + * password replaced by `********`); the secret itself is never printed. * * The queries run inside a READ ONLY transaction, so the script cannot - * change anything even by mistake. env_vars are not read. + * change anything even by mistake. env_vars and a DATABASE connector's + * auth_config are not read — the finding only looks at what is still + * visible in the plaintext column, not at whether repair could act on it. * * DATABASE_URL=postgres://… npx ts-node scripts/secret-literals/report.ts * @@ -24,7 +30,7 @@ import { type ToolRow, } from '../../src/connectors/secret-literal-scan.util'; -const GUARDED = `('REST','SOAP','GRAPHQL','MCP','ODATA')`; +const GUARDED = `('REST','SOAP','GRAPHQL','MCP','ODATA','DATABASE')`; export async function report(client: Client, out: (line: string) => void = console.log) { await client.query('BEGIN READ ONLY'); diff --git a/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts b/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts index 62fc509cd..8343ef102 100644 --- a/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts +++ b/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts @@ -1,7 +1,7 @@ import { Client } from 'pg'; import { report } from '../../scripts/secret-literals/report'; import { repair } from '../../scripts/secret-literals/repair'; -import { encrypt } from '../common/crypto/encryption.util'; +import { decrypt, encrypt } from '../common/crypto/encryption.util'; import { openEnvVarsStrict, sealEnvVars } from '../common/crypto/env-vars-at-rest'; /** @@ -33,8 +33,8 @@ d('secret literal report + repair (real Postgres)', () => { async function connector(id: string, values: Record) { await client.query( - `INSERT INTO connectors (id, user_id, organization_id, name, type, base_url, headers, config, env_vars, updated_at) - VALUES ($1, $2, $3, $4, $5::"ConnectorType", $6, $7::jsonb, $8::jsonb, $9::jsonb, now())`, + `INSERT INTO connectors (id, user_id, organization_id, name, type, base_url, headers, config, env_vars, auth_config, updated_at) + VALUES ($1, $2, $3, $4, $5::"ConnectorType", $6, $7::jsonb, $8::jsonb, $9::jsonb, $10, now())`, [ id, USER, @@ -45,6 +45,7 @@ d('secret literal report + repair (real Postgres)', () => { values.headers === undefined ? null : JSON.stringify(values.headers), values.config === undefined ? null : JSON.stringify(values.config), values.envVars === undefined ? null : JSON.stringify(values.envVars), + values.authConfig === undefined ? null : values.authConfig, ], ); } @@ -107,9 +108,22 @@ d('secret literal report + repair (real Postgres)', () => { // Nothing to do here. await connector('c-clean', { name: 'Clean', baseUrl: 'https://api.example.com/v1' }); await tool('t-clean', 'c-clean', 'list_items', { method: 'GET', path: '/items/{id}' }); - await connector('c-db', { name: 'DB', type: 'DATABASE', baseUrl: 'postgres://u:p@db.example.com/x' }); + + // A DATABASE connector from before the fix: password still in the + // connection string, no authConfig. A tool's own fields are never + // inspected for DATABASE, so its secret-looking path stays as it is. + await connector('c-db', { name: 'DB', type: 'DATABASE', baseUrl: 'postgresql://app:s3cret@db.example.com:5432/shop' }); await tool('t-db', 'c-db', 'q', { method: 'query', path: `SELECT '${TG}'` }); + // A DATABASE connector whose authConfig already holds credentials: + // left alone, since which one the engine prefers differs by driver. + await connector('c-db-protected', { + name: 'DB protected', + type: 'DATABASE', + baseUrl: 'mssql://sa:n3w@sql.example.com:1433/shop', + authConfig: encrypt(JSON.stringify({ domain: 'CORP', username: 'sa', password: 'old' }), KEY), + }); + // Variables encrypted with another key: must be skipped, never overwritten. await connector('c-badkey', { name: 'Bad key', @@ -136,43 +150,62 @@ d('secret literal report + repair (real Postgres)', () => { expect(byConnector('c-tg-custom')).toEqual(['tool.description', 'tool.name', 'tool.path']); expect(byConnector('c-maps')).toEqual(['baseUrl', 'headers.X-Api-Key']); expect(byConnector('c-clean')).toEqual([]); - expect(byConnector('c-db')).toEqual([]); + expect(byConnector('c-db')).toEqual(['baseUrl']); + // Still flagged: the URL still shows it, even though repair will leave + // it alone because authConfig already holds credentials. + expect(byConnector('c-db-protected')).toEqual(['baseUrl']); const text = lines.join('\n'); expect(text).not.toContain(TG_SECRET); expect(text).not.toContain(GOOGLE); expect(text).not.toContain('k1k2k3k4k5'); + expect(text).not.toContain('s3cret'); + expect(text).not.toContain('n3w'); const stored = await client.query(`SELECT base_url FROM connectors WHERE id = 'c-tg-catalog'`); expect(stored.rows[0].base_url).toContain(TG_SECRET); }); it('repair dry run: plans every fix, writes nothing', async () => { const result = await repair(client, { workspaceId: ORG, out }); - expect(result.plans.map((p) => p.connectorId).sort()).toEqual(['c-maps', 'c-tg-catalog', 'c-tg-custom']); + // c-db-protected decrypts fine but has nothing to move (authConfig + // already holds credentials), so it is in neither list, like a + // REST connector that turns out to need no changes. + expect(result.plans.map((p) => p.connectorId).sort()).toEqual(['c-db', 'c-maps', 'c-tg-catalog', 'c-tg-custom']); expect(result.skipped).toEqual([{ connectorId: 'c-badkey', reason: expect.stringMatching(/could not be decrypted/) }]); expect(lines.join('\n')).not.toContain(TG_SECRET); + expect(lines.join('\n')).not.toContain('s3cret'); const tables = await client.query(`SELECT 1 FROM pg_tables WHERE tablename LIKE 'secret_literal_repair_backup_%'`); expect(tables.rowCount).toBe(0); const stored = await client.query(`SELECT name FROM mcp_tools WHERE id = 't-tg-custom'`); expect(stored.rows[0].name).toContain('7123456789'); + const dbRow = await client.query(`SELECT base_url FROM connectors WHERE id = 'c-db'`); + expect(dbRow.rows[0].base_url).toContain('s3cret'); }); it('repair --apply: backs up, rewrites, keeps every variable, and is idempotent', async () => { const result = await repair(client, { workspaceId: ORG, apply: true, backupSuffix: 'fixture', out }); expect(result.backupTable).toBe('secret_literal_repair_backup_fixture'); - const backup = await client.query(`SELECT kind, id, base_url FROM secret_literal_repair_backup_fixture ORDER BY kind, id`); + const backup = await client.query(`SELECT kind, id, base_url, auth_config FROM secret_literal_repair_backup_fixture ORDER BY kind, id`); expect(backup.rows.map((r) => `${r.kind}:${r.id}`)).toEqual([ + 'connector:c-db', 'connector:c-maps', 'connector:c-tg-catalog', 'connector:c-tg-custom', 'tool:t-tg-custom', ]); expect(backup.rows.find((r) => r.id === 'c-tg-catalog').base_url).toContain(TG_SECRET); + // The backup holds the exact prior row: plaintext URL, no authConfig yet. + const dbBackup = backup.rows.find((r) => r.id === 'c-db'); + expect(dbBackup.base_url).toContain('s3cret'); + expect(dbBackup.auth_config).toBeNull(); const rows = Object.fromEntries( - (await client.query(`SELECT id, base_url, headers, config, env_vars FROM connectors WHERE organization_id = $1`, [ORG])).rows.map( - (r) => [r.id, r], - ), + ( + await client.query( + `SELECT id, base_url, headers, config, env_vars, auth_config FROM connectors WHERE organization_id = $1`, + [ORG], + ) + ).rows.map((r) => [r.id, r]), ); // Catalog Telegram: same variable reused, nothing added. expect(rows['c-tg-catalog'].base_url).toBe('https://api.telegram.org/bot{{TELEGRAM_BOT_TOKEN}}'); @@ -194,15 +227,29 @@ d('secret literal report + repair (real Postgres)', () => { expect(rows['c-maps'].headers).toEqual({ 'X-Api-Key': '{{MAPS_API_KEY_2}}', Accept: 'application/json' }); expect(openEnvVarsStrict(rows['c-maps'].env_vars)).toEqual({ REGION: 'eu', MAPS_API_KEY: GOOGLE, MAPS_API_KEY_2: 'k1k2k3k4k5' }); + // DATABASE: credentials moved out of the URL into encrypted authConfig. + expect(rows['c-db'].base_url).toBe('postgresql://db.example.com:5432/shop'); + expect(JSON.parse(decrypt(rows['c-db'].auth_config, KEY))).toEqual({ username: 'app', password: 's3cret' }); + + // DATABASE, authConfig already held credentials: left exactly as it was. + expect(rows['c-db-protected'].base_url).toBe('mssql://sa:n3w@sql.example.com:1433/shop'); + expect(JSON.parse(decrypt(rows['c-db-protected'].auth_config, KEY))).toEqual({ + domain: 'CORP', + username: 'sa', + password: 'old', + }); + // Untouched. expect(rows['c-clean'].base_url).toBe('https://api.example.com/v1'); - expect(rows['c-db'].base_url).toBe('postgres://u:p@db.example.com/x'); expect(rows['c-badkey'].base_url).toContain(TG_SECRET); - // A second run finds nothing left to do (the bad-key connector stays skipped). + // A second run finds nothing left to do (the bad-key connector stays + // skipped; c-db-protected still has nothing to move). const again = await repair(client, { workspaceId: ORG, out }); expect(again.plans).toEqual([]); const findings = (await report(client, out)).filter((f) => f.workspaceId === ORG); - expect(findings.map((f) => f.connectorId)).toEqual(['c-badkey']); + // c-db is now clean; c-db-protected's URL still shows its password — + // the one case the fix cannot clean up without risking who connects. + expect(findings.map((f) => f.connectorId).sort()).toEqual(['c-badkey', 'c-db-protected']); }); }); diff --git a/packages/backend/src/connectors/secret-literal-scan.util.spec.ts b/packages/backend/src/connectors/secret-literal-scan.util.spec.ts index 22b067671..0a3ad5197 100644 --- a/packages/backend/src/connectors/secret-literal-scan.util.spec.ts +++ b/packages/backend/src/connectors/secret-literal-scan.util.spec.ts @@ -51,13 +51,41 @@ describe('scanConnector', () => { expect(JSON.stringify(found)).not.toContain(TG_SECRET); }); - it('ignores clean and DATABASE connectors', () => { + it('ignores a clean connector', () => { expect(scanConnector(connector(), [tool()])).toEqual([]); - expect( - scanConnector(connector({ type: 'DATABASE', baseUrl: `postgres://u:${TG}@h/db` }), [ - tool({ endpointMapping: { method: 'query', path: `SELECT '${TG}'` } }), - ]), - ).toEqual([]); + }); + + describe('a DATABASE connector', () => { + const STORED = 'postgresql://app:s3cret@db.example.com:5432/shop'; + + it('finds a password still in the connection string, redacted', () => { + const found = scanConnector(connector({ type: 'DATABASE', baseUrl: STORED }), []); + expect(found).toEqual([ + { + connectorId: 'c1', + workspaceId: 'org1', + field: 'baseUrl', + kind: 'database_url_password', + pattern: 'postgresql://app:********@db.example.com:5432/shop', + adapterSlug: null, + }, + ]); + expect(JSON.stringify(found)).not.toContain('s3cret'); + }); + + it('finds nothing once the password has moved out of the URL', () => { + expect( + scanConnector(connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop' }), []), + ).toEqual([]); + }); + + it('does not inspect tools: the connection string is the only field that can carry a password', () => { + expect( + scanConnector(connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop' }), [ + tool({ endpointMapping: { method: 'query', path: "SELECT * FROM t WHERE p = 's3cret'" } }), + ]), + ).toEqual([]); + }); }); }); @@ -109,4 +137,58 @@ describe('planRepair', () => { it('returns null when there is nothing to do', () => { expect(planRepair(connector(), [tool()])).toBeNull(); }); + + describe('a DATABASE connector', () => { + const STORED = 'postgresql://app:s3cret@db.example.com:5432/shop'; + + it('moves the credentials into authConfig when it holds neither', () => { + const plan = planRepair(connector({ type: 'DATABASE', baseUrl: STORED, authConfig: null }), [])!; + expect(plan.connector.baseUrl).toBe('postgresql://db.example.com:5432/shop'); + expect(plan.connector.authConfig).toEqual({ username: 'app', password: 's3cret' }); + expect(plan.tools).toEqual([]); + }); + + it.each([ + ['mssql', 'mssql://sa:n3w@sql.example.com:1433/shop', 'mssql://sql.example.com:1433/shop', { username: 'sa', password: 'n3w' }], + ['oracle', 'oracle://app:s3cret@ora.example.com:1521/orcl', 'oracle://ora.example.com:1521/orcl', { username: 'app', password: 's3cret' }], + ['hana', 'hana://app:s3cret@hana.example.com:30015/shop', 'hana://hana.example.com:30015/shop', { username: 'app', password: 's3cret' }], + ])('moves credentials out of a %s URL the same way', (_driver, baseUrl, cleaned, authConfig) => { + const plan = planRepair(connector({ type: 'DATABASE', baseUrl, authConfig: null }), [])!; + expect(plan.connector.baseUrl).toBe(cleaned); + expect(plan.connector.authConfig).toEqual(authConfig); + }); + + it('does nothing when authConfig already holds a username or password', () => { + // Which one wins then differs by driver (pg/mysql/mongo prefer the URL's + // username, mssql/oracle/hana prefer authConfig), so moving here could + // change who connects — the same rule the live API follows. + expect( + planRepair(connector({ type: 'DATABASE', baseUrl: STORED, authConfig: { username: 'other' } }), []), + ).toBeNull(); + }); + + it('does nothing once the password has already moved out of the URL', () => { + expect( + planRepair( + connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop', authConfig: null }), + [], + ), + ).toBeNull(); + }); + + it('does nothing for a connection string it cannot parse', () => { + // A multi-host MongoDB URL: DatabaseEngine puts authConfig's credentials + // back into it at call time, so leaving it as it is keeps connecting. + expect( + planRepair( + connector({ + type: 'DATABASE', + baseUrl: 'mongodb://app:s3cret@m1.example.com:27017,m2.example.com:27017/shop', + authConfig: null, + }), + [], + ), + ).toBeNull(); + }); + }); }); diff --git a/packages/backend/src/connectors/secret-literal-scan.util.ts b/packages/backend/src/connectors/secret-literal-scan.util.ts index e759fb674..2d8c4f4cc 100644 --- a/packages/backend/src/connectors/secret-literal-scan.util.ts +++ b/packages/backend/src/connectors/secret-literal-scan.util.ts @@ -5,6 +5,7 @@ import { type SecretKind, } from '../common/secret-literals.util'; import { SECRET_GUARDED_TYPES, SecretExtractor, type MovedSecret } from './secret-extraction.util'; +import { maskUrlPassword, moveUrlCredentials } from './database-url-credentials.util'; /** * Stored connectors and tools checked for secret literals, for the report @@ -22,6 +23,8 @@ export interface ConnectorRow { headers: Record | null; config: Record | null; envVars: Record | null; + /** DATABASE only, decrypted. `null` means "not loaded yet", same as `envVars`. */ + authConfig?: Record | null; } export interface ToolRow { @@ -39,7 +42,7 @@ export interface ScanFinding { toolId?: string; /** `baseUrl`, `config.baseUrlBaseline`, `headers.X`, `tool.path`, `tool.name`… */ field: string; - kind: SecretKind | 'name_contains_secret'; + kind: SecretKind | 'name_contains_secret' | 'database_url_password'; pattern: string; /** The catalog adapter the connector came from, if any. */ adapterSlug: string | null; @@ -54,8 +57,30 @@ function isObject(v: unknown): v is Record { return !!v && typeof v === 'object' && !Array.isArray(v); } +/** + * A DATABASE connector's own check: its connection string can carry a + * password (database-url-credentials.util.ts), a different shape than the + * literal-in-a-header/query/path/name check below. No tool-level check: the + * builtin query and recent-events tools never carry the connection string. + */ +function scanDatabaseConnector(connector: ConnectorRow): ScanFinding[] { + const masked = maskUrlPassword(connector.baseUrl ?? ''); + if (masked === connector.baseUrl) return []; + return [ + { + connectorId: connector.id, + workspaceId: connector.organizationId, + field: 'baseUrl', + kind: 'database_url_password', + pattern: masked, + adapterSlug: slugOf(connector), + }, + ]; +} + /** Everything secret-looking in one connector and its tools. */ export function scanConnector(connector: ConnectorRow, tools: ToolRow[]): ScanFinding[] { + if (connector.type === 'DATABASE') return scanDatabaseConnector(connector); if (!SECRET_GUARDED_TYPES.has(connector.type)) return []; const adapterSlug = slugOf(connector); const out: ScanFinding[] = []; @@ -125,6 +150,8 @@ export interface RepairPlan { /** Fields of the connector to write (only the changed ones). */ connector: Partial> & { envVars?: Record; + /** DATABASE only: the credentials moved out of baseUrl, to encrypt and write. */ + authConfig?: Record; }; tools: Array<{ id: string; changes: Partial> }>; moved: MovedSecret[]; @@ -133,6 +160,30 @@ export interface RepairPlan { renamedTools: string[]; } +/** + * What the repair writes for a DATABASE connector: move `user:password@` out + * of baseUrl into authConfig, the same move the API now makes on every save + * (database-url-credentials.util.ts). `connector.authConfig` must already be + * decrypted; pass `null` first to decide cheaply whether to bother decrypting + * at all — `moveUrlCredentials` treats `null` as "holds neither", so a + * connector whose authConfig turns out to already hold credentials correctly + * plans nothing on the authoritative second call. + */ +function planDatabaseRepair(connector: ConnectorRow): RepairPlan | null { + const moved = moveUrlCredentials(connector.baseUrl, connector.authConfig); + if (!moved) return null; + return { + connectorId: connector.id, + workspaceId: connector.organizationId, + adapterSlug: slugOf(connector), + connector: { baseUrl: moved.baseUrl, authConfig: moved.authConfig }, + tools: [], + moved: [], + newVariables: [], + renamedTools: [], + }; +} + /** * What the repair writes for one connector: the same rewrite the API now * applies on every save. Catalog connectors' headers are left alone (they @@ -140,6 +191,7 @@ export interface RepairPlan { * Returns null when there is nothing to change. */ export function planRepair(connector: ConnectorRow, tools: ToolRow[]): RepairPlan | null { + if (connector.type === 'DATABASE') return planDatabaseRepair(connector); if (!SECRET_GUARDED_TYPES.has(connector.type)) return null; const adapterSlug = slugOf(connector); const env = Object.fromEntries( From 1ab695a1e90bf45a01463a6878daeacb16cd207a Mon Sep 17 00:00:00 2001 From: rangarajan19 Date: Tue, 17 Nov 2026 21:45:57 +0530 Subject: [PATCH 2/3] Address review: drop the query string from the report, update docs for the start-up pass - scanDatabaseConnector no longer prints a connection string's query string in the report's pattern: only the userinfo password was masked, so a value like ?password=... or ?sslpassword=... would have printed in full. Dropped entirely rather than selectively masked, with tests. - docs/connectors/database.md: the line about an old connector's password no longer claims it stays until next saved -- the backend now moves it at every start (v0.55.1). Points at the scripts as the way to check an instance before its next restart, or for a dry run and a backup instead of the silent pass. - Test for a DATABASE connector whose auth_config was encrypted with a different key: skipped by both report and repair, never overwritten, exactly like the existing env_vars case. --- docs/connectors/database.md | 2 +- llms-full.txt | 2 +- .../secret-literal-repair.integration.spec.ts | 40 ++++++++++++++++--- .../secret-literal-scan.util.spec.ts | 33 +++++++++++++++ .../connectors/secret-literal-scan.util.ts | 10 ++++- 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/docs/connectors/database.md b/docs/connectors/database.md index 31b280efc..4cde03228 100644 --- a/docs/connectors/database.md +++ b/docs/connectors/database.md @@ -74,7 +74,7 @@ You can type the username and password into the connection string, as above. Whe - An API request that sends `authConfig` replaces it whole, the moved username and password included. - No export contains a password that has moved, an admin export included, so a connector restored from a backup needs it typed in again. - A connection string the parser cannot read, such as a multi-host MongoDB URL typed with its credentials, keeps them in place; the password is shown as `********`. -- A connector created before this existed still has its password in the connection string until it is next saved. `scripts/secret-literals/report.ts` finds these (and the same literal-secret cases other connector types have); `repair.ts` moves them, dry run by default, with a backup table before `--apply`. +- `scripts/secret-literals/report.ts` finds a DATABASE connector whose connection string still carries its password (and the same literal-secret cases other connector types have); `repair.ts` moves it, dry run by default, with a backup table before `--apply`. The backend also does this at every start (below); the scripts are for checking an instance before its next restart, or for a dry run and a backup instead of the silent start-up pass. Connectors saved before v0.55.0 kept `user:password@` in the stored connection string. The backend moves those too: every start checks the stored database connection strings once and moves the credentials it still finds, the same way a save does. This needs no setting and works the same on AnythingMCP Cloud and self-hosted. The backend log shows the result with connector ids and counts, never a connection string: diff --git a/llms-full.txt b/llms-full.txt index c3713640c..22064d2a3 100644 --- a/llms-full.txt +++ b/llms-full.txt @@ -1030,7 +1030,7 @@ You can type the username and password into the connection string, as above. Whe - An API request that sends `authConfig` replaces it whole, the moved username and password included. - No export contains a password that has moved, an admin export included, so a connector restored from a backup needs it typed in again. - A connection string the parser cannot read, such as a multi-host MongoDB URL typed with its credentials, keeps them in place; the password is shown as `********`. -- A connector created before this existed still has its password in the connection string until it is next saved. `scripts/secret-literals/report.ts` finds these (and the same literal-secret cases other connector types have); `repair.ts` moves them, dry run by default, with a backup table before `--apply`. +- `scripts/secret-literals/report.ts` finds a DATABASE connector whose connection string still carries its password (and the same literal-secret cases other connector types have); `repair.ts` moves it, dry run by default, with a backup table before `--apply`. The backend also does this at every start (below); the scripts are for checking an instance before its next restart, or for a dry run and a backup instead of the silent start-up pass. Connectors saved before v0.55.0 kept `user:password@` in the stored connection string. The backend moves those too: every start checks the stored database connection strings once and moves the credentials it still finds, the same way a save does. This needs no setting and works the same on AnythingMCP Cloud and self-hosted. The backend log shows the result with connector ids and counts, never a connection string: diff --git a/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts b/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts index 8343ef102..5d9c296f1 100644 --- a/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts +++ b/packages/backend/src/connectors/secret-literal-repair.integration.spec.ts @@ -28,6 +28,9 @@ const GOOGLE = ['AI', 'zaSyB1x2y3z4A5B6C7D8E9F0gHiJkLmNoPqRs'].join(''); d('secret literal report + repair (real Postgres)', () => { let client: Client; + // encrypt() uses a random IV each call, so the exact ciphertext inserted + // in beforeAll is captured here rather than re-encrypted for comparison. + let dbBadKeyAuthConfig: string; const lines: string[] = []; const out = (line: string) => lines.push(line); @@ -130,6 +133,16 @@ d('secret literal report + repair (real Postgres)', () => { baseUrl: `https://api.telegram.org/bot${TG}`, envVars: { $enc: encrypt(JSON.stringify({ A: 'b' }), 'z'.repeat(48), 'connectors.env_vars') }, }); + + // DATABASE, authConfig encrypted with another key: must be skipped, its + // base_url and auth_config left exactly as they are, like c-badkey. + dbBadKeyAuthConfig = encrypt(JSON.stringify({ username: 'app', password: 'old' }), 'z'.repeat(48)); + await connector('c-db-badkey', { + name: 'DB bad key', + type: 'DATABASE', + baseUrl: 'postgresql://app:badkeypw@db.example.com:5432/shop', + authConfig: dbBadKeyAuthConfig, + }); }); afterAll(async () => { @@ -154,12 +167,16 @@ d('secret literal report + repair (real Postgres)', () => { // Still flagged: the URL still shows it, even though repair will leave // it alone because authConfig already holds credentials. expect(byConnector('c-db-protected')).toEqual(['baseUrl']); + // The report never reads auth_config, so an unreadable one does not + // stop this connector's URL from being flagged either. + expect(byConnector('c-db-badkey')).toEqual(['baseUrl']); const text = lines.join('\n'); expect(text).not.toContain(TG_SECRET); expect(text).not.toContain(GOOGLE); expect(text).not.toContain('k1k2k3k4k5'); expect(text).not.toContain('s3cret'); expect(text).not.toContain('n3w'); + expect(text).not.toContain('badkeypw'); const stored = await client.query(`SELECT base_url FROM connectors WHERE id = 'c-tg-catalog'`); expect(stored.rows[0].base_url).toContain(TG_SECRET); }); @@ -170,15 +187,21 @@ d('secret literal report + repair (real Postgres)', () => { // already holds credentials), so it is in neither list, like a // REST connector that turns out to need no changes. expect(result.plans.map((p) => p.connectorId).sort()).toEqual(['c-db', 'c-maps', 'c-tg-catalog', 'c-tg-custom']); - expect(result.skipped).toEqual([{ connectorId: 'c-badkey', reason: expect.stringMatching(/could not be decrypted/) }]); + expect(result.skipped.sort((a, b) => a.connectorId.localeCompare(b.connectorId))).toEqual([ + { connectorId: 'c-badkey', reason: expect.stringMatching(/could not be decrypted/) }, + { connectorId: 'c-db-badkey', reason: expect.stringMatching(/could not be decrypted/) }, + ]); expect(lines.join('\n')).not.toContain(TG_SECRET); expect(lines.join('\n')).not.toContain('s3cret'); + expect(lines.join('\n')).not.toContain('badkeypw'); const tables = await client.query(`SELECT 1 FROM pg_tables WHERE tablename LIKE 'secret_literal_repair_backup_%'`); expect(tables.rowCount).toBe(0); const stored = await client.query(`SELECT name FROM mcp_tools WHERE id = 't-tg-custom'`); expect(stored.rows[0].name).toContain('7123456789'); const dbRow = await client.query(`SELECT base_url FROM connectors WHERE id = 'c-db'`); expect(dbRow.rows[0].base_url).toContain('s3cret'); + const dbBadKeyRow = await client.query(`SELECT base_url FROM connectors WHERE id = 'c-db-badkey'`); + expect(dbBadKeyRow.rows[0].base_url).toBe('postgresql://app:badkeypw@db.example.com:5432/shop'); }); it('repair --apply: backs up, rewrites, keeps every variable, and is idempotent', async () => { @@ -243,13 +266,20 @@ d('secret literal report + repair (real Postgres)', () => { expect(rows['c-clean'].base_url).toBe('https://api.example.com/v1'); expect(rows['c-badkey'].base_url).toContain(TG_SECRET); - // A second run finds nothing left to do (the bad-key connector stays + // DATABASE, authConfig encrypted with another key: left exactly as it + // was, byte for byte, never backed up (it was never written). + expect(rows['c-db-badkey'].base_url).toBe('postgresql://app:badkeypw@db.example.com:5432/shop'); + expect(rows['c-db-badkey'].auth_config).toBe(dbBadKeyAuthConfig); + expect(backup.rows.find((r) => r.id === 'c-db-badkey')).toBeUndefined(); + + // A second run finds nothing left to do (the bad-key connectors stay // skipped; c-db-protected still has nothing to move). const again = await repair(client, { workspaceId: ORG, out }); expect(again.plans).toEqual([]); const findings = (await report(client, out)).filter((f) => f.workspaceId === ORG); - // c-db is now clean; c-db-protected's URL still shows its password — - // the one case the fix cannot clean up without risking who connects. - expect(findings.map((f) => f.connectorId).sort()).toEqual(['c-badkey', 'c-db-protected']); + // c-db is now clean; the other three still show a password in their + // URL — the cases the fix cannot clean up without risking who connects + // or without a readable authConfig to compare against. + expect(findings.map((f) => f.connectorId).sort()).toEqual(['c-badkey', 'c-db-badkey', 'c-db-protected']); }); }); diff --git a/packages/backend/src/connectors/secret-literal-scan.util.spec.ts b/packages/backend/src/connectors/secret-literal-scan.util.spec.ts index 0a3ad5197..b66b2ef68 100644 --- a/packages/backend/src/connectors/secret-literal-scan.util.spec.ts +++ b/packages/backend/src/connectors/secret-literal-scan.util.spec.ts @@ -79,6 +79,39 @@ describe('scanConnector', () => { ).toEqual([]); }); + it('drops the query string from the printed pattern, so a secret there is never printed', () => { + const found = scanConnector( + connector({ + type: 'DATABASE', + baseUrl: 'postgresql://app:s3cret@db.example.com:5432/shop?sslmode=require&sslpassword=anothersecret', + }), + [], + ); + expect(found).toEqual([ + { + connectorId: 'c1', + workspaceId: 'org1', + field: 'baseUrl', + kind: 'database_url_password', + pattern: 'postgresql://app:********@db.example.com:5432/shop', + adapterSlug: null, + }, + ]); + expect(JSON.stringify(found)).not.toContain('anothersecret'); + expect(JSON.stringify(found)).not.toContain('sslpassword'); + }); + + it('finds nothing for a userinfo-free URL even when the query string looks secret-ish', () => { + // The query string is dropped before checking, not inspected for secrets + // of its own — only the userinfo password is this check's job. + expect( + scanConnector( + connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop?sslpassword=anothersecret' }), + [], + ), + ).toEqual([]); + }); + it('does not inspect tools: the connection string is the only field that can carry a password', () => { expect( scanConnector(connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop' }), [ diff --git a/packages/backend/src/connectors/secret-literal-scan.util.ts b/packages/backend/src/connectors/secret-literal-scan.util.ts index 2d8c4f4cc..966c56185 100644 --- a/packages/backend/src/connectors/secret-literal-scan.util.ts +++ b/packages/backend/src/connectors/secret-literal-scan.util.ts @@ -62,10 +62,16 @@ function isObject(v: unknown): v is Record { * password (database-url-credentials.util.ts), a different shape than the * literal-in-a-header/query/path/name check below. No tool-level check: the * builtin query and recent-events tools never carry the connection string. + * + * The printed pattern drops the query string and fragment: `maskUrlPassword` + * only masks the userinfo password, so a query parameter such as + * `?password=…` or `?sslpassword=…` would otherwise print in full in a + * report that is supposed to carry no secret. */ function scanDatabaseConnector(connector: ConnectorRow): ScanFinding[] { - const masked = maskUrlPassword(connector.baseUrl ?? ''); - if (masked === connector.baseUrl) return []; + const withoutQuery = (connector.baseUrl ?? '').split(/[?#]/)[0]; + const masked = maskUrlPassword(withoutQuery); + if (masked === withoutQuery) return []; return [ { connectorId: connector.id, From 2bfcd2cc8cdc5fa4fbe95eb072d65bb07259ecad Mon Sep 17 00:00:00 2001 From: rangarajan19 Date: Tue, 17 Nov 2026 22:01:44 +0530 Subject: [PATCH 3/3] Fix a false negative in the query-string drop, refine the doc wording Masking before dropping the query string, not after: maskUrlPassword finds the password by scanning up to the first "/", not the first "?", so a password containing a raw, unencoded ? or # was being cut in two by stripping the query string first, missing the password entirely. Now masks the full string first, then drops the query string from the already-masked result. Added a regression test with such a password. Also reworded the database.md line once more to match the maintainer's exact phrasing: the scripts are for checking what the start-up pass could not move, and for an instance that has not restarted onto it yet. --- docs/connectors/database.md | 2 +- llms-full.txt | 2 +- .../secret-literal-scan.util.spec.ts | 29 +++++++++++++++++-- .../connectors/secret-literal-scan.util.ts | 23 ++++++++++----- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/docs/connectors/database.md b/docs/connectors/database.md index 4cde03228..06388f870 100644 --- a/docs/connectors/database.md +++ b/docs/connectors/database.md @@ -74,7 +74,7 @@ You can type the username and password into the connection string, as above. Whe - An API request that sends `authConfig` replaces it whole, the moved username and password included. - No export contains a password that has moved, an admin export included, so a connector restored from a backup needs it typed in again. - A connection string the parser cannot read, such as a multi-host MongoDB URL typed with its credentials, keeps them in place; the password is shown as `********`. -- `scripts/secret-literals/report.ts` finds a DATABASE connector whose connection string still carries its password (and the same literal-secret cases other connector types have); `repair.ts` moves it, dry run by default, with a backup table before `--apply`. The backend also does this at every start (below); the scripts are for checking an instance before its next restart, or for a dry run and a backup instead of the silent start-up pass. +- `scripts/secret-literals/report.ts` finds a DATABASE connector whose connection string still carries its password (and the same literal-secret cases other connector types have); `repair.ts` moves it, dry run by default, with a backup table before `--apply`. The backend also moves these at every start (below); the scripts are for checking what that pass could not move, and for an instance that has not restarted onto it yet. Connectors saved before v0.55.0 kept `user:password@` in the stored connection string. The backend moves those too: every start checks the stored database connection strings once and moves the credentials it still finds, the same way a save does. This needs no setting and works the same on AnythingMCP Cloud and self-hosted. The backend log shows the result with connector ids and counts, never a connection string: diff --git a/llms-full.txt b/llms-full.txt index 22064d2a3..a29b27d6c 100644 --- a/llms-full.txt +++ b/llms-full.txt @@ -1030,7 +1030,7 @@ You can type the username and password into the connection string, as above. Whe - An API request that sends `authConfig` replaces it whole, the moved username and password included. - No export contains a password that has moved, an admin export included, so a connector restored from a backup needs it typed in again. - A connection string the parser cannot read, such as a multi-host MongoDB URL typed with its credentials, keeps them in place; the password is shown as `********`. -- `scripts/secret-literals/report.ts` finds a DATABASE connector whose connection string still carries its password (and the same literal-secret cases other connector types have); `repair.ts` moves it, dry run by default, with a backup table before `--apply`. The backend also does this at every start (below); the scripts are for checking an instance before its next restart, or for a dry run and a backup instead of the silent start-up pass. +- `scripts/secret-literals/report.ts` finds a DATABASE connector whose connection string still carries its password (and the same literal-secret cases other connector types have); `repair.ts` moves it, dry run by default, with a backup table before `--apply`. The backend also moves these at every start (below); the scripts are for checking what that pass could not move, and for an instance that has not restarted onto it yet. Connectors saved before v0.55.0 kept `user:password@` in the stored connection string. The backend moves those too: every start checks the stored database connection strings once and moves the credentials it still finds, the same way a save does. This needs no setting and works the same on AnythingMCP Cloud and self-hosted. The backend log shows the result with connector ids and counts, never a connection string: diff --git a/packages/backend/src/connectors/secret-literal-scan.util.spec.ts b/packages/backend/src/connectors/secret-literal-scan.util.spec.ts index b66b2ef68..fa30b5f46 100644 --- a/packages/backend/src/connectors/secret-literal-scan.util.spec.ts +++ b/packages/backend/src/connectors/secret-literal-scan.util.spec.ts @@ -102,8 +102,9 @@ describe('scanConnector', () => { }); it('finds nothing for a userinfo-free URL even when the query string looks secret-ish', () => { - // The query string is dropped before checking, not inspected for secrets - // of its own — only the userinfo password is this check's job. + // The query string is never inspected for secrets of its own — only + // the userinfo password is this check's job; it is dropped from the + // printed pattern, not from what is checked for a password. expect( scanConnector( connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop?sslpassword=anothersecret' }), @@ -112,6 +113,30 @@ describe('scanConnector', () => { ).toEqual([]); }); + it('still finds and masks a password that itself contains a raw ? or #', () => { + // maskUrlPassword finds the password by scanning up to the first `/`, + // not the first `?`, so a password with a literal `?` or `#` in it is + // masked whole. Dropping the query string from the raw string before + // masking (instead of from the masked result) would cut the password + // in two here and report nothing at all — the exact mistake this + // locks in against. + const found = scanConnector( + connector({ type: 'DATABASE', baseUrl: 'postgresql://app:weird?pass#1@db.example.com:5432/shop' }), + [], + ); + expect(found).toEqual([ + { + connectorId: 'c1', + workspaceId: 'org1', + field: 'baseUrl', + kind: 'database_url_password', + pattern: 'postgresql://app:********@db.example.com:5432/shop', + adapterSlug: null, + }, + ]); + expect(JSON.stringify(found)).not.toContain('weird?pass#1'); + }); + it('does not inspect tools: the connection string is the only field that can carry a password', () => { expect( scanConnector(connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop' }), [ diff --git a/packages/backend/src/connectors/secret-literal-scan.util.ts b/packages/backend/src/connectors/secret-literal-scan.util.ts index 966c56185..9858a5f50 100644 --- a/packages/backend/src/connectors/secret-literal-scan.util.ts +++ b/packages/backend/src/connectors/secret-literal-scan.util.ts @@ -64,21 +64,30 @@ function isObject(v: unknown): v is Record { * builtin query and recent-events tools never carry the connection string. * * The printed pattern drops the query string and fragment: `maskUrlPassword` - * only masks the userinfo password, so a query parameter such as - * `?password=…` or `?sslpassword=…` would otherwise print in full in a - * report that is supposed to carry no secret. + * only masks the userinfo password, everything from the first `/` onward + * (path, query, fragment) passes through unchanged, so a query parameter + * such as `?password=…` or `?sslpassword=…` would otherwise print in full + * in a report that is supposed to carry no secret. + * + * The drop happens on the masked result, never on the raw `baseUrl`: + * `maskUrlPassword` finds the password by scanning for `@` up to the first + * `/` (not the first `?`), so a password containing a raw, unencoded `?` or + * `#` is still masked correctly first; only then is what remains after the + * first `/` — now free of any password — safe to cut at its own `?`/`#`. + * Splitting the raw string before masking would instead cut the password + * itself in two and miss it entirely. */ function scanDatabaseConnector(connector: ConnectorRow): ScanFinding[] { - const withoutQuery = (connector.baseUrl ?? '').split(/[?#]/)[0]; - const masked = maskUrlPassword(withoutQuery); - if (masked === withoutQuery) return []; + const baseUrl = connector.baseUrl ?? ''; + const masked = maskUrlPassword(baseUrl); + if (masked === baseUrl) return []; return [ { connectorId: connector.id, workspaceId: connector.organizationId, field: 'baseUrl', kind: 'database_url_password', - pattern: masked, + pattern: masked.split(/[?#]/)[0], adapterSlug: slugOf(connector), }, ];