|
61 | 61 | * an OBJECT depth of `providerConfig`; and the endpoints an instance-auth |
62 | 62 | * kind declares (`auth.tokenUrl`, `auth.authorizationUrl`, `auth.resource`) |
63 | 63 | * and any URL a kind declares later. A string is judged when it starts |
64 | | - * with `scheme://`. What a credential part is lives in ONE place, |
| 64 | + * with `scheme://`. What a credential part is, and the walk that finds |
| 65 | + * every URL holding one, live in ONE place, |
65 | 66 | * `./connector-url-credentials.ts`, which the write door's refinement |
66 | | - * judges by too: an authored URL holding one is refused, so only a row |
67 | | - * stored before that refusal can carry one to this read. |
| 67 | + * runs too: an authored URL holding one is refused, so only a row stored |
| 68 | + * before that refusal can carry one to this read. |
68 | 69 | * 6. **An array under `providerConfig` that holds such a URL, WHOLE**, and |
69 | 70 | * **a key under `providerConfig` whose own name carries a dot that holds |
70 | 71 | * such a URL at any depth, WHOLE.** The carry-forward cannot address an |
@@ -132,10 +133,10 @@ import { redactUrlCredentialQueryParams } from '../data/datasource-credential-re |
132 | 133 | import { |
133 | 134 | CONNECTOR_CREDENTIAL_MAP_PATHS, |
134 | 135 | CONNECTOR_CREDENTIAL_URL_QUERY_PARAM_NAMES, |
135 | | - CONNECTOR_URL_ROOTS, |
136 | 136 | URL_USERINFO_RE, |
137 | 137 | connectorCredentialQueryParamsOf, |
138 | | - connectorUrlCredentialPartsOf, |
| 138 | + connectorUrlCredentialPositionOf, |
| 139 | + findConnectorUrlCredentials, |
139 | 140 | } from './connector-url-credentials'; |
140 | 141 | import { objectShapeOf, wrapperChainOf, zodDefOf } from '../kernel/metadata-credential-fields'; |
141 | 142 | import type { MetadataRedactionResult } from '../kernel/metadata-type-redaction'; |
@@ -363,56 +364,34 @@ function withValueAt(item: Record<string, unknown>, path: readonly string[], val |
363 | 364 | return { ...item, [head]: withValueAt(item[head] as Record<string, unknown>, rest, value) }; |
364 | 365 | } |
365 | 366 |
|
366 | | -/** Whether `value` is a URL holding a credential part — the parts {@link redactConnectorUrl} removes. */ |
367 | | -function isCredentialUrl(value: string, params: readonly string[]): boolean { |
368 | | - return connectorUrlCredentialPartsOf(value, params) !== undefined; |
369 | | -} |
370 | | - |
371 | | -/** Whether `value` holds, at any depth, a URL {@link isCredentialUrl} judges (item 6). */ |
372 | | -function holdsCredentialUrl(value: unknown, params: readonly string[]): boolean { |
373 | | - if (typeof value === 'string') return isCredentialUrl(value, params); |
374 | | - if (Array.isArray(value)) return value.some((element) => holdsCredentialUrl(element, params)); |
375 | | - if (isPlainRecord(value)) return Object.values(value).some((child) => holdsCredentialUrl(child, params)); |
376 | | - return false; |
377 | | -} |
378 | | - |
379 | 367 | /** |
380 | | - * Items 5 and 6: every URL under `providerConfig` and `auth` |
381 | | - * ({@link CONNECTOR_URL_ROOTS}), at object depth, whose credential parts |
382 | | - * {@link redactConnectorUrl} removes — the path and the rewritten value — and |
383 | | - * every array there holding such a URL, withheld whole (`value` absent). |
| 368 | + * Items 5 and 6: every URL the write door's own walk returns |
| 369 | + * ({@link findConnectorUrlCredentials} — the one traversal both doors run), at |
| 370 | + * the position {@link connectorUrlCredentialPositionOf} names. A URL reached |
| 371 | + * through object keys alone is rewritten in place — the path and the value |
| 372 | + * {@link redactConnectorUrl} leaves. An array holding such a URL, and a key |
| 373 | + * whose own name carries a dot holding one at any depth, cannot be addressed |
| 374 | + * part for part, so each is withheld WHOLE (`value` absent), once however many |
| 375 | + * URLs it holds: fail closed, and the carry-forward keeps the stored value at |
| 376 | + * rest by the whole-string fallback. Item 5's "avoid silent deletion" yields to |
| 377 | + * item 7's fail-closed there. |
384 | 378 | */ |
385 | 379 | function credentialUrlRewritesOf( |
386 | 380 | item: Record<string, unknown>, |
387 | 381 | ): Array<{ path: string[]; value?: string }> { |
388 | 382 | const rewrites: Array<{ path: string[]; value?: string }> = []; |
| 383 | + const withheld = new Set<string>(); |
389 | 384 | const params = connectorCredentialQueryParamsOf(item); |
390 | | - const walk = (node: Record<string, unknown>, prefix: readonly string[]): void => { |
391 | | - for (const [key, value] of Object.entries(node)) { |
392 | | - const path = [...prefix, key]; |
393 | | - // A key whose own name carries a dot cannot be addressed segment by |
394 | | - // segment (its dotted name is indistinguishable from a nested path once |
395 | | - // `redactedKeys` joins on dots), so it is never rewritten in place or |
396 | | - // descended into. Fail closed instead: if it holds a credential URL at |
397 | | - // any depth, withhold the key WHOLE — the carry-forward keeps the stored |
398 | | - // value at rest by the whole-string fallback. Item 5's "avoid silent |
399 | | - // deletion" yields to item 7's fail-closed here. |
400 | | - if (key.includes('.')) { |
401 | | - if (holdsCredentialUrl(value, params)) rewrites.push({ path }); |
402 | | - continue; |
403 | | - } |
404 | | - if (typeof value === 'string') { |
405 | | - if (isCredentialUrl(value, params)) rewrites.push({ path, value: redactConnectorUrl(value, params) }); |
406 | | - } else if (Array.isArray(value)) { |
407 | | - if (holdsCredentialUrl(value, params)) rewrites.push({ path }); |
408 | | - } else if (isPlainRecord(value)) { |
409 | | - walk(value, path); |
410 | | - } |
| 385 | + for (const found of findConnectorUrlCredentials(item)) { |
| 386 | + const position = connectorUrlCredentialPositionOf(found.path); |
| 387 | + if (position.rewrite) { |
| 388 | + rewrites.push({ path: position.path, value: redactConnectorUrl(found.url, params) }); |
| 389 | + continue; |
411 | 390 | } |
412 | | - }; |
413 | | - for (const root of CONNECTOR_URL_ROOTS) { |
414 | | - const node = item[root]; |
415 | | - if (isPlainRecord(node)) walk(node, [root]); |
| 391 | + const key = JSON.stringify(position.path); |
| 392 | + if (withheld.has(key)) continue; |
| 393 | + withheld.add(key); |
| 394 | + rewrites.push({ path: position.path }); |
416 | 395 | } |
417 | 396 | return rewrites; |
418 | 397 | } |
|
0 commit comments