Skip to content

Commit 2bfcd2c

Browse files
committed
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.
1 parent 1ab695a commit 2bfcd2c

4 files changed

Lines changed: 45 additions & 11 deletions

File tree

‎docs/connectors/database.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ You can type the username and password into the connection string, as above. Whe
7474
- An API request that sends `authConfig` replaces it whole, the moved username and password included.
7575
- No export contains a password that has moved, an admin export included, so a connector restored from a backup needs it typed in again.
7676
- 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 `********`.
77-
- `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.
77+
- `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.
7878

7979
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:
8080

‎llms-full.txt‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/backend/src/connectors/secret-literal-scan.util.spec.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ describe('scanConnector', () => {
102102
});
103103

104104
it('finds nothing for a userinfo-free URL even when the query string looks secret-ish', () => {
105-
// The query string is dropped before checking, not inspected for secrets
106-
// of its own — only the userinfo password is this check's job.
105+
// The query string is never inspected for secrets of its own — only
106+
// the userinfo password is this check's job; it is dropped from the
107+
// printed pattern, not from what is checked for a password.
107108
expect(
108109
scanConnector(
109110
connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop?sslpassword=anothersecret' }),
@@ -112,6 +113,30 @@ describe('scanConnector', () => {
112113
).toEqual([]);
113114
});
114115

116+
it('still finds and masks a password that itself contains a raw ? or #', () => {
117+
// maskUrlPassword finds the password by scanning up to the first `/`,
118+
// not the first `?`, so a password with a literal `?` or `#` in it is
119+
// masked whole. Dropping the query string from the raw string before
120+
// masking (instead of from the masked result) would cut the password
121+
// in two here and report nothing at all — the exact mistake this
122+
// locks in against.
123+
const found = scanConnector(
124+
connector({ type: 'DATABASE', baseUrl: 'postgresql://app:weird?pass#1@db.example.com:5432/shop' }),
125+
[],
126+
);
127+
expect(found).toEqual([
128+
{
129+
connectorId: 'c1',
130+
workspaceId: 'org1',
131+
field: 'baseUrl',
132+
kind: 'database_url_password',
133+
pattern: 'postgresql://app:********@db.example.com:5432/shop',
134+
adapterSlug: null,
135+
},
136+
]);
137+
expect(JSON.stringify(found)).not.toContain('weird?pass#1');
138+
});
139+
115140
it('does not inspect tools: the connection string is the only field that can carry a password', () => {
116141
expect(
117142
scanConnector(connector({ type: 'DATABASE', baseUrl: 'postgresql://db.example.com:5432/shop' }), [

‎packages/backend/src/connectors/secret-literal-scan.util.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,30 @@ function isObject(v: unknown): v is Record<string, unknown> {
6464
* builtin query and recent-events tools never carry the connection string.
6565
*
6666
* The printed pattern drops the query string and fragment: `maskUrlPassword`
67-
* only masks the userinfo password, so a query parameter such as
68-
* `?password=…` or `?sslpassword=…` would otherwise print in full in a
69-
* report that is supposed to carry no secret.
67+
* only masks the userinfo password, everything from the first `/` onward
68+
* (path, query, fragment) passes through unchanged, so a query parameter
69+
* such as `?password=…` or `?sslpassword=…` would otherwise print in full
70+
* in a report that is supposed to carry no secret.
71+
*
72+
* The drop happens on the masked result, never on the raw `baseUrl`:
73+
* `maskUrlPassword` finds the password by scanning for `@` up to the first
74+
* `/` (not the first `?`), so a password containing a raw, unencoded `?` or
75+
* `#` is still masked correctly first; only then is what remains after the
76+
* first `/` — now free of any password — safe to cut at its own `?`/`#`.
77+
* Splitting the raw string before masking would instead cut the password
78+
* itself in two and miss it entirely.
7079
*/
7180
function scanDatabaseConnector(connector: ConnectorRow): ScanFinding[] {
72-
const withoutQuery = (connector.baseUrl ?? '').split(/[?#]/)[0];
73-
const masked = maskUrlPassword(withoutQuery);
74-
if (masked === withoutQuery) return [];
81+
const baseUrl = connector.baseUrl ?? '';
82+
const masked = maskUrlPassword(baseUrl);
83+
if (masked === baseUrl) return [];
7584
return [
7685
{
7786
connectorId: connector.id,
7887
workspaceId: connector.organizationId,
7988
field: 'baseUrl',
8089
kind: 'database_url_password',
81-
pattern: masked,
90+
pattern: masked.split(/[?#]/)[0],
8291
adapterSlug: slugOf(connector),
8392
},
8493
];

0 commit comments

Comments
 (0)