|
12 | 12 | // shape leaks the credentials the DSN carries. |
13 | 13 |
|
14 | 14 | import { describe, it, expect } from 'vitest'; |
| 15 | +import { |
| 16 | + CREDENTIAL_URL_QUERY_PARAM_NAMES, |
| 17 | + urlCredentialQueryParams, |
| 18 | + urlUserinfoPassword, |
| 19 | +} from '@objectstack/spec/data'; |
15 | 20 | import { redactConnectionUrl, describeDriverConnection, describeDriverSqliteFile } from './connection-display.js'; |
16 | 21 |
|
17 | 22 | describe('redactConnectionUrl', () => { |
@@ -56,6 +61,145 @@ describe('redactConnectionUrl', () => { |
56 | 61 | }); |
57 | 62 | }); |
58 | 63 |
|
| 64 | +// The label is a print-time reader of the same question the datasource doors |
| 65 | +// answer: where does a URL carry a password? It asks the platform's ONE |
| 66 | +// userinfo grammar (`@objectstack/spec/data`) rather than keeping its own, so |
| 67 | +// the oracle below is that grammar's own password accessor. |
| 68 | +describe('redactConnectionUrl — the password rule is the shared URL grammar', () => { |
| 69 | + const PASSWORD = 'S3CRET-PW'; |
| 70 | + |
| 71 | + it('removes the password wherever the shared grammar reads one, with or without a literal `://`', () => { |
| 72 | + // A scheme-relative authority. |
| 73 | + expect(redactConnectionUrl(`//admin:${PASSWORD}@db.example.com:5432/app`)) |
| 74 | + .toBe('//admin@db.example.com:5432/app'); |
| 75 | + // A tab inside the `//`, which a URL parser deletes before it reads. |
| 76 | + expect(redactConnectionUrl(`postgres:/\t/admin:${PASSWORD}@db.example.com/app`)) |
| 77 | + .toBe('postgres:/\t/admin@db.example.com/app'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('removes the password where the shared grammar reads a special scheme\'s authority without a `//`', () => { |
| 81 | + expect(redactConnectionUrl(`https:admin:${PASSWORD}@db.example.com/app`)) |
| 82 | + .toBe('https:admin@db.example.com/app'); |
| 83 | + expect(redactConnectionUrl(`http:/admin:${PASSWORD}@db.example.com/app`)) |
| 84 | + .toBe('http:/admin@db.example.com/app'); |
| 85 | + expect(redactConnectionUrl(`wss:\\admin:${PASSWORD}@db.example.com/app`)) |
| 86 | + .toBe('wss:\\admin@db.example.com/app'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('removes the whole password up to the last `@` of the authority, as the shared grammar bounds it', () => { |
| 90 | + expect(redactConnectionUrl('postgres://admin:S3C@RET-PW@h1:5432,h2:5432/app')) |
| 91 | + .toBe('postgres://h1:5432,h2:5432/app'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('never prints a password the shared grammar reads, in any URL shape', () => { |
| 95 | + const shapes = [ |
| 96 | + `postgres://admin:${PASSWORD}@db.example.com:5432/app`, |
| 97 | + `postgres://:${PASSWORD}@db.example.com/app`, |
| 98 | + `mysql://admin:${PASSWORD}@db.example.com:3306/app`, |
| 99 | + `libsql://admin:${PASSWORD}@app-org.turso.io?authToken=jwt`, |
| 100 | + `postgres://admin:${PASSWORD}@h1:5432,h2:5432/app`, |
| 101 | + `//admin:${PASSWORD}@db.example.com:5432/app`, |
| 102 | + `postgres:/\t/admin:${PASSWORD}@db.example.com/app`, |
| 103 | + ` \npostgres://admin:${PASSWORD}@db.example.com/app`, |
| 104 | + // Special schemes without the `//`, read as a URL parser reads them. |
| 105 | + `https:admin:${PASSWORD}@db.example.com/app`, |
| 106 | + `http:/admin:${PASSWORD}@db.example.com/app`, |
| 107 | + `wss:\\admin:${PASSWORD}@db.example.com/app`, |
| 108 | + ]; |
| 109 | + const judged = shapes.filter((value) => urlUserinfoPassword(value) !== undefined); |
| 110 | + // Non-vacuity floor: the shared grammar reads a password in every shape |
| 111 | + // listed, so the property below is never checked over nothing. |
| 112 | + expect(judged.length).toBe(shapes.length); |
| 113 | + for (const value of judged) { |
| 114 | + expect(urlUserinfoPassword(value)).toBe(PASSWORD); |
| 115 | + expect(redactConnectionUrl(value), value).not.toContain(PASSWORD); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it('keeps its output byte for byte for every value the shared grammar reads no password in', () => { |
| 120 | + const controls: ReadonlyArray<readonly [string, string]> = [ |
| 121 | + ['postgres://admin@db.example.com:5432/app', 'postgres://db.example.com:5432/app'], |
| 122 | + ['postgres://admin:@db.example.com/app', 'postgres://db.example.com/app'], |
| 123 | + ['postgres://db.example.com:5432/app?sslmode=require', 'postgres://db.example.com:5432/app'], |
| 124 | + ['postgres://admin@h1:5432,h2:5432/app', 'postgres://h1:5432,h2:5432/app'], |
| 125 | + ['libsql://app-org.turso.io?authToken=secret-jwt', 'libsql://app-org.turso.io'], |
| 126 | + ['https://db.example.com/app', 'https://db.example.com/app'], |
| 127 | + ['file:./data/app.db', 'file:./data/app.db'], |
| 128 | + ['file:///var/lib/app.db', 'file:///var/lib/app.db'], |
| 129 | + ['sqlite:/var/lib/app.db', 'sqlite:/var/lib/app.db'], |
| 130 | + ['//admin@db.example.com/app', '//admin@db.example.com/app'], |
| 131 | + [':memory:', ':memory:'], |
| 132 | + ['(in-memory)', '(in-memory)'], |
| 133 | + ]; |
| 134 | + for (const [value, printed] of controls) { |
| 135 | + expect(urlUserinfoPassword(value), value).toBeUndefined(); |
| 136 | + expect(redactConnectionUrl(value), value).toBe(printed); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + it('keeps its output byte for byte for a password the URL parser already removed', () => { |
| 141 | + expect(redactConnectionUrl(`postgres://admin:${PASSWORD}@db.example.com:5432/app`)) |
| 142 | + .toBe('postgres://db.example.com:5432/app'); |
| 143 | + expect(redactConnectionUrl(`libsql://admin:${PASSWORD}@app-org.turso.io?authToken=jwt`)) |
| 144 | + .toBe('libsql://app-org.turso.io'); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +// The query half of the same rule: a credential query pair is judged by the |
| 149 | +// shared query-pair judge and its one list of names, on every path the label |
| 150 | +// takes — a value with no `://`, the URL parser's, and the hand fallback for a |
| 151 | +// value the parser rejects. |
| 152 | +describe('redactConnectionUrl — the credential query rule is the shared query-pair judge', () => { |
| 153 | + const TOKEN = 'T0KEN-VALUE'; |
| 154 | + |
| 155 | + it('removes a credential query pair from a value with no `://`', () => { |
| 156 | + expect(redactConnectionUrl(`libsql:app-org.turso.io?authToken=${TOKEN}`)) |
| 157 | + .toBe('libsql:app-org.turso.io'); |
| 158 | + expect(redactConnectionUrl(`file:local.db?authToken=${TOKEN}&mode=ro`)) |
| 159 | + .toBe('file:local.db?mode=ro'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('removes a credential query pair on the hand fallback for a value the URL parser rejects', () => { |
| 163 | + expect(redactConnectionUrl(`postgres://admin@h1:5432,h2:5432/app?password=${TOKEN}&sslmode=require`)) |
| 164 | + .toBe('postgres://h1:5432,h2:5432/app?sslmode=require'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('removes a credential query pair on the URL parser path', () => { |
| 168 | + expect(redactConnectionUrl(`postgres://db.example.com/app?PASSWORD=${TOKEN}`)) |
| 169 | + .toBe('postgres://db.example.com/app'); |
| 170 | + expect(redactConnectionUrl(`libsql://app-org.turso.io?authToken=${TOKEN}`)) |
| 171 | + .toBe('libsql://app-org.turso.io'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('never prints a credential query value the shared judge reads, on any path', () => { |
| 175 | + const shapes = [ |
| 176 | + `libsql:app-org.turso.io?authToken=${TOKEN}`, |
| 177 | + `file:local.db?mode=ro&AUTHTOKEN=${TOKEN}`, |
| 178 | + `file:local.db?auth%54oken=${TOKEN}`, |
| 179 | + `postgres://admin@h1:5432,h2:5432/app?password=${TOKEN}`, |
| 180 | + `postgres://db.example.com/app?password=${TOKEN}`, |
| 181 | + `libsql://app-org.turso.io?authToken=${TOKEN}#frag`, |
| 182 | + ]; |
| 183 | + for (const value of shapes) { |
| 184 | + expect(urlCredentialQueryParams(value, CREDENTIAL_URL_QUERY_PARAM_NAMES), value).not.toEqual([]); |
| 185 | + expect(redactConnectionUrl(value), value).not.toContain(TOKEN); |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + it('keeps its output byte for byte for every value with no credential query pair', () => { |
| 190 | + const controls: ReadonlyArray<readonly [string, string]> = [ |
| 191 | + ['postgres://admin@h1:5432,h2:5432/app?sslmode=require', 'postgres://h1:5432,h2:5432/app?sslmode=require'], |
| 192 | + ['file:local.db?mode=ro', 'file:local.db?mode=ro'], |
| 193 | + ['libsql:app-org.turso.io?authToken=', 'libsql:app-org.turso.io?authToken='], |
| 194 | + ['postgres://db.example.com:5432/app?sslmode=require', 'postgres://db.example.com:5432/app'], |
| 195 | + ]; |
| 196 | + for (const [value, printed] of controls) { |
| 197 | + expect(urlCredentialQueryParams(value, CREDENTIAL_URL_QUERY_PARAM_NAMES), value).toEqual([]); |
| 198 | + expect(redactConnectionUrl(value), value).toBe(printed); |
| 199 | + } |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
59 | 203 | describe('describeDriverConnection', () => { |
60 | 204 | it('reads a DSN string', () => { |
61 | 205 | expect(describeDriverConnection({ client: 'pg', connection: 'postgres://u:p@host:5432/app' })) |
|
0 commit comments