@@ -305,10 +305,11 @@ export const FAULT_CAUSE_DEPTH_LIMIT = 5;
305305 * A link whose read throws ends the chain in a marker link too.
306306 *
307307 * A non-Error object cause is serialized with `isRedacted` applied key by
308- * key, the same rule a logger applies to its own fields. The default is the
309- * logger's default redact list (`LoggerConfig.redact`), matched on the
310- * lower-cased name, so the console fallback, which has no logger config,
311- * errs towards withholding.
308+ * key, at every depth. The default is `ObjectLogger`'s own rule over its
309+ * default redact list (`LoggerConfig.redact`'s default words, whole-word
310+ * matched). ⚠️ Only the default words: the console fallback has no logger
311+ * config, so a host's own `redact` list is not applied to the line it writes;
312+ * a door handed a configured logger gets that logger's list instead.
312313 *
313314 * ⛔ Operator-only. It feeds the log line; no door puts it on the wire, and a
314315 * 5xx answer withholds even the wrapper's own sentence.
@@ -360,12 +361,84 @@ function causeOf(value: unknown): unknown {
360361 }
361362}
362363
363- /** `LoggerConfig.redact`'s default words (`@objectstack/spec` `logging.zod.ts`). */
364+ /**
365+ * `LoggerConfig.redact`'s default words (`@objectstack/spec` `logging.zod.ts`),
366+ * which `ObjectLogger` also applies when it is given none.
367+ */
364368const DEFAULT_REDACTED_WORDS = [ 'password' , 'token' , 'secret' , 'key' ] as const ;
365369
370+ /**
371+ * The default field-name rule, matched the way `ObjectLogger` matches its
372+ * redact list: on word boundaries, the maintainer's ruling on #5573, so a bare
373+ * `keys` / `tokens` or a `monkey` / `keyword` / `secretary` is not withheld,
374+ * while `apiKey`, `refresh_tokens` and `accesstoken` are.
375+ *
376+ * A copy of `@objectstack/core`'s matcher (`tokenizeFieldName` and
377+ * `fieldWordsMatchPattern` in `logger.ts`), for the same reason
378+ * {@link faultCauseChain} is one; `logger-error-cause.test.ts` there runs a
379+ * fixture of these spellings through both and fails on any drift.
380+ */
366381function isDefaultRedactedFieldName ( key : string ) : boolean {
367- const lower = key . toLowerCase ( ) ;
368- return DEFAULT_REDACTED_WORDS . some ( ( word ) => lower . includes ( word ) ) ;
382+ const nameWords = tokenizeFieldName ( key ) ;
383+ return DEFAULT_REDACTED_WORDS . some ( ( word ) => fieldWordsMatchPattern ( nameWords , [ word ] ) ) ;
384+ }
385+
386+ function tokenizeFieldName ( name : string ) : string [ ] {
387+ return name
388+ . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, '$1 $2' )
389+ . replace ( / ( [ A - Z ] + ) ( [ A - Z ] [ a - z ] ) / g, '$1 $2' )
390+ . replace ( / ( [ a - z A - Z ] ) ( [ 0 - 9 ] ) / g, '$1 $2' )
391+ . split ( / [ ^ A - Z a - z 0 - 9 ] + / )
392+ . filter ( Boolean )
393+ . map ( ( word ) => word . toLowerCase ( ) ) ;
394+ }
395+
396+ function singularizeWord ( word : string ) : string {
397+ if ( / (?: s s | u s | i s ) $ / . test ( word ) ) return word ;
398+ if ( / (?: c h | s h | s | x | z ) e s $ / . test ( word ) ) return word . slice ( 0 , - 2 ) ;
399+ if ( / [ a - z 0 - 9 ] s $ / . test ( word ) ) return word . slice ( 0 , - 1 ) ;
400+ return word ;
401+ }
402+
403+ /** Glued qualifiers that mark the secret sense: `apikey`, `accesstoken`, `clientsecret`. */
404+ const CONCATENATED_SECRET_QUALIFIERS = new Set ( [
405+ 'access' , 'account' , 'admin' , 'api' , 'app' , 'auth' , 'bearer' , 'client' , 'csrf' , 'db' , 'database' ,
406+ 'encryption' , 'id' , 'jwt' , 'master' , 'oauth' , 'private' , 'public' , 'refresh' , 'root' , 'secret' ,
407+ 'service' , 'session' , 'shared' , 'sign' , 'signing' , 'ssh' , 'token' , 'user' , 'webhook' , 'xsrf' ,
408+ ] ) ;
409+
410+ function isQualifiedConcatenation ( word : string , redactWord : string ) : boolean {
411+ for ( const base of [ word , singularizeWord ( word ) ] ) {
412+ if ( base . length <= redactWord . length || ! base . endsWith ( redactWord ) ) continue ;
413+ if ( CONCATENATED_SECRET_QUALIFIERS . has ( base . slice ( 0 , base . length - redactWord . length ) ) ) return true ;
414+ }
415+ return false ;
416+ }
417+
418+ function containsWordRun ( words : string [ ] , run : string [ ] ) : boolean {
419+ for ( let i = 0 ; i + run . length <= words . length ; i ++ ) {
420+ if ( run . every ( ( word , offset ) => words [ i + offset ] === word ) ) return true ;
421+ }
422+ return false ;
423+ }
424+
425+ function fieldWordsMatchPattern ( nameWords : string [ ] , patternWords : string [ ] ) : boolean {
426+ if ( patternWords . length === 0 || nameWords . length === 0 ) return false ;
427+ if ( patternWords . length > 1 ) {
428+ const glued = patternWords . join ( '' ) ;
429+ return (
430+ containsWordRun ( nameWords , patternWords ) ||
431+ nameWords . some ( ( word ) => word === glued || singularizeWord ( word ) === glued )
432+ ) ;
433+ }
434+ const redactWord = patternWords [ 0 ] ;
435+ const isCompound = nameWords . filter ( ( word ) => / [ a - z ] / . test ( word ) ) . length > 1 ;
436+ return nameWords . some (
437+ ( word ) =>
438+ word === redactWord ||
439+ ( isCompound && singularizeWord ( word ) === redactWord ) ||
440+ isQualifiedConcatenation ( word , redactWord ) ,
441+ ) ;
369442}
370443
371444function causeLine ( cause : unknown , isRedacted : ( key : string ) => boolean ) : FaultCauseLine {
0 commit comments