@@ -251,6 +251,325 @@ describe("restoreRedactedValues", () => {
251251 ) . toBe ( "user-provided-new-token-value" ) ;
252252 } ) ;
253253
254+ describe . each ( [
255+ { name : "schema hints" , hints : { "accounts[].token" : { sensitive : true } } } ,
256+ { name : "sensitive-path guessing" , hints : undefined } ,
257+ ] ) ( "stable array identities with $name" , ( { hints } ) => {
258+ const original = {
259+ accounts : [
260+ { id : "alpha" , token : "synthetic-alpha-token" } ,
261+ { id : "bravo" , token : "synthetic-bravo-token" } ,
262+ { id : "charlie" , token : "synthetic-charlie-token" } ,
263+ ] ,
264+ } ;
265+
266+ it . each ( [
267+ { change : "deleting an earlier entry" , ids : [ "bravo" , "charlie" ] } ,
268+ { change : "reordering the entries" , ids : [ "charlie" , "alpha" , "bravo" ] } ,
269+ ] ) ( "restores each owner's secret after $change" , ( { ids } ) => {
270+ const incoming = {
271+ accounts : ids . map ( ( id ) => ( { id, token : REDACTED_SENTINEL } ) ) ,
272+ } ;
273+
274+ const restored = restoreRedactedValues ( incoming , original , hints ) ;
275+
276+ expect ( restored . accounts ) . toEqual ( ids . map ( ( id ) => ( { id, token : `synthetic-${ id } -token` } ) ) ) ;
277+ } ) ;
278+
279+ it ( "keeps a unique owner's secret when an unidentified sibling is deleted" , ( ) => {
280+ const previous = {
281+ accounts : [
282+ { token : "synthetic-unidentified-token" } ,
283+ { id : "bravo" , token : "synthetic-bravo-token" } ,
284+ ] ,
285+ } ;
286+ const incoming = {
287+ accounts : [ { id : "bravo" , token : REDACTED_SENTINEL } ] ,
288+ } ;
289+
290+ expect ( restoreRedactedValues ( incoming , previous , hints ) . accounts ) . toEqual ( [
291+ { id : "bravo" , token : "synthetic-bravo-token" } ,
292+ ] ) ;
293+ } ) ;
294+
295+ it ( "keeps a unique owner's secret when ambiguous sibling identities are deleted" , ( ) => {
296+ const previous = {
297+ accounts : [
298+ { id : "duplicate" , token : "synthetic-first-duplicate-token" } ,
299+ { id : "duplicate" , token : "synthetic-second-duplicate-token" } ,
300+ { id : "bravo" , token : "synthetic-bravo-token" } ,
301+ ] ,
302+ } ;
303+ const incoming = {
304+ accounts : [ { id : "bravo" , token : REDACTED_SENTINEL } ] ,
305+ } ;
306+
307+ expect ( restoreRedactedValues ( incoming , previous , hints ) . accounts ) . toEqual ( [
308+ { id : "bravo" , token : "synthetic-bravo-token" } ,
309+ ] ) ;
310+ } ) ;
311+
312+ it ( "rejects a redacted secret for a new identity instead of borrowing its position" , ( ) => {
313+ const result = restoreRedactedValues_orig (
314+ { accounts : [ { id : "new-owner" , token : REDACTED_SENTINEL } ] } ,
315+ original ,
316+ hints ,
317+ ) ;
318+
319+ expect ( result . ok ) . toBe ( false ) ;
320+ expect ( result . humanReadableMessage ) . not . toContain ( "synthetic-alpha-token" ) ;
321+ } ) ;
322+
323+ it ( "accepts a new identity when its secret was explicitly supplied" , ( ) => {
324+ const restored = restoreRedactedValues (
325+ { accounts : [ { id : "new-owner" , token : "synthetic-new-token" } ] } ,
326+ original ,
327+ hints ,
328+ ) ;
329+
330+ expect ( restored . accounts ) . toEqual ( [ { id : "new-owner" , token : "synthetic-new-token" } ] ) ;
331+ } ) ;
332+
333+ it ( "matches prototype-shaped identities without inherited-key collisions" , ( ) => {
334+ const previous = {
335+ accounts : [
336+ { id : "__proto__" , token : "synthetic-prototype-token" } ,
337+ { id : "constructor" , token : "synthetic-constructor-token" } ,
338+ ] ,
339+ } ;
340+ const incoming = {
341+ accounts : [
342+ { id : "constructor" , token : REDACTED_SENTINEL } ,
343+ { id : "__proto__" , token : REDACTED_SENTINEL } ,
344+ ] ,
345+ } ;
346+
347+ expect ( restoreRedactedValues ( incoming , previous , hints ) . accounts ) . toEqual ( [
348+ { id : "constructor" , token : "synthetic-constructor-token" } ,
349+ { id : "__proto__" , token : "synthetic-prototype-token" } ,
350+ ] ) ;
351+ } ) ;
352+
353+ it ( "keeps escaped environment identities positional after runtime substitution" , ( ) => {
354+ const previous = {
355+ accounts : [
356+ { id : "${ACCOUNT_ID}" , token : "synthetic-literal-token" } ,
357+ { id : "bravo" , token : "synthetic-bravo-token" } ,
358+ ] ,
359+ } ;
360+ const incoming = {
361+ accounts : [
362+ { id : "$${ACCOUNT_ID}" , token : REDACTED_SENTINEL } ,
363+ { id : "bravo" , token : REDACTED_SENTINEL } ,
364+ ] ,
365+ } ;
366+
367+ expect ( restoreRedactedValues ( incoming , previous , hints ) . accounts ) . toEqual ( [
368+ { id : "$${ACCOUNT_ID}" , token : "synthetic-literal-token" } ,
369+ { id : "bravo" , token : "synthetic-bravo-token" } ,
370+ ] ) ;
371+ } ) ;
372+
373+ it ( "rejects moving an unidentified redacted entry onto an identified owner's position" , ( ) => {
374+ const previous = {
375+ accounts : [
376+ { token : "synthetic-unidentified-token" } ,
377+ { id : "bravo" , token : "synthetic-bravo-token" } ,
378+ ] ,
379+ } ;
380+ const incoming = {
381+ accounts : [ { id : "bravo" , token : REDACTED_SENTINEL } , { token : REDACTED_SENTINEL } ] ,
382+ } ;
383+
384+ const result = restoreRedactedValues_orig ( incoming , previous , hints ) ;
385+
386+ expect ( result . ok ) . toBe ( false ) ;
387+ expect ( result . humanReadableMessage ) . not . toContain ( "synthetic-bravo-token" ) ;
388+ } ) ;
389+
390+ it . each ( [
391+ {
392+ reason : "original identities are duplicated" ,
393+ previous : [
394+ { id : "duplicate" , token : "synthetic-first-token" } ,
395+ { id : "duplicate" , token : "synthetic-second-token" } ,
396+ ] ,
397+ incoming : [
398+ { id : "duplicate" , token : REDACTED_SENTINEL } ,
399+ { id : "duplicate" , token : REDACTED_SENTINEL } ,
400+ ] ,
401+ } ,
402+ {
403+ reason : "incoming identities are duplicated" ,
404+ previous : [
405+ { id : "alpha" , token : "synthetic-first-token" } ,
406+ { id : "bravo" , token : "synthetic-second-token" } ,
407+ ] ,
408+ incoming : [
409+ { id : "alpha" , token : REDACTED_SENTINEL } ,
410+ { id : "alpha" , token : REDACTED_SENTINEL } ,
411+ ] ,
412+ } ,
413+ {
414+ reason : "an original identity is missing" ,
415+ previous : [
416+ { id : "alpha" , token : "synthetic-first-token" } ,
417+ { token : "synthetic-second-token" } ,
418+ ] ,
419+ incoming : [ { id : "alpha" , token : REDACTED_SENTINEL } , { token : REDACTED_SENTINEL } ] ,
420+ } ,
421+ {
422+ reason : "an incoming identity is missing" ,
423+ previous : [
424+ { id : "alpha" , token : "synthetic-first-token" } ,
425+ { id : "bravo" , token : "synthetic-second-token" } ,
426+ ] ,
427+ incoming : [ { id : "alpha" , token : REDACTED_SENTINEL } , { token : REDACTED_SENTINEL } ] ,
428+ } ,
429+ {
430+ reason : "an identity is empty" ,
431+ previous : [
432+ { id : "" , token : "synthetic-first-token" } ,
433+ { id : "bravo" , token : "synthetic-second-token" } ,
434+ ] ,
435+ incoming : [
436+ { id : "" , token : REDACTED_SENTINEL } ,
437+ { id : "bravo" , token : REDACTED_SENTINEL } ,
438+ ] ,
439+ } ,
440+ {
441+ reason : "an incoming identity is an unresolved environment placeholder" ,
442+ previous : [
443+ { id : "alpha" , token : "synthetic-first-token" } ,
444+ { id : "bravo" , token : "synthetic-second-token" } ,
445+ ] ,
446+ incoming : [
447+ { id : "${ACCOUNT_ID}" , token : REDACTED_SENTINEL } ,
448+ { id : "bravo" , token : REDACTED_SENTINEL } ,
449+ ] ,
450+ } ,
451+ {
452+ reason : "an incoming identity contains an inline environment placeholder" ,
453+ previous : [
454+ { id : "account-alpha" , token : "synthetic-first-token" } ,
455+ { id : "bravo" , token : "synthetic-second-token" } ,
456+ ] ,
457+ incoming : [
458+ { id : "account-${ACCOUNT_ID}" , token : REDACTED_SENTINEL } ,
459+ { id : "bravo" , token : REDACTED_SENTINEL } ,
460+ ] ,
461+ } ,
462+ ] ) ( "keeps positional restoration when $reason" , ( { previous, incoming } ) => {
463+ const restored = restoreRedactedValues ( { accounts : incoming } , { accounts : previous } , hints ) ;
464+
465+ expect ( restored . accounts . map ( ( entry ) => entry . token ) ) . toEqual ( [
466+ "synthetic-first-token" ,
467+ "synthetic-second-token" ,
468+ ] ) ;
469+ } ) ;
470+ } ) ;
471+
472+ it ( "matches stable identities independently at each nested array boundary" , ( ) => {
473+ const hints : ConfigUiHints = {
474+ "providers[].accounts[].token" : { sensitive : true } ,
475+ } ;
476+ const original = {
477+ providers : [
478+ {
479+ id : "provider-alpha" ,
480+ accounts : [ { id : "account-one" , token : "synthetic-alpha-one-token" } ] ,
481+ } ,
482+ {
483+ id : "provider-bravo" ,
484+ accounts : [
485+ { id : "account-one" , token : "synthetic-bravo-one-token" } ,
486+ { id : "account-two" , token : "synthetic-bravo-two-token" } ,
487+ ] ,
488+ } ,
489+ ] ,
490+ } ;
491+ const incoming = {
492+ providers : [
493+ {
494+ id : "provider-bravo" ,
495+ accounts : [
496+ { id : "account-two" , token : REDACTED_SENTINEL } ,
497+ { id : "account-one" , token : REDACTED_SENTINEL } ,
498+ ] ,
499+ } ,
500+ ] ,
501+ } ;
502+
503+ const restored = restoreRedactedValues ( incoming , original , hints ) ;
504+
505+ expect ( restored . providers ) . toEqual ( [
506+ {
507+ id : "provider-bravo" ,
508+ accounts : [
509+ { id : "account-two" , token : "synthetic-bravo-two-token" } ,
510+ { id : "account-one" , token : "synthetic-bravo-one-token" } ,
511+ ] ,
512+ } ,
513+ ] ) ;
514+ } ) ;
515+
516+ it ( "keeps positional restoration for arrays of scalar secrets" , ( ) => {
517+ const hints : ConfigUiHints = { "apiKeys[]" : { sensitive : true } } ;
518+ const original = { apiKeys : [ "synthetic-first-key" , "synthetic-second-key" ] } ;
519+
520+ const restored = restoreRedactedValues (
521+ { apiKeys : [ REDACTED_SENTINEL , REDACTED_SENTINEL ] } ,
522+ original ,
523+ hints ,
524+ ) ;
525+
526+ expect ( restored ) . toEqual ( original ) ;
527+ } ) ;
528+
529+ it ( "does not treat a redacted identifier as a stable array identity" , ( ) => {
530+ const hints : ConfigUiHints = {
531+ "accounts[].id" : { sensitive : true } ,
532+ "accounts[].token" : { sensitive : true } ,
533+ } ;
534+ const original = {
535+ accounts : [
536+ { id : "synthetic-first-id" , token : "synthetic-first-token" } ,
537+ { id : "synthetic-second-id" , token : "synthetic-second-token" } ,
538+ ] ,
539+ } ;
540+ const incoming = {
541+ accounts : [
542+ { id : REDACTED_SENTINEL , token : REDACTED_SENTINEL } ,
543+ { id : REDACTED_SENTINEL , token : REDACTED_SENTINEL } ,
544+ ] ,
545+ } ;
546+
547+ expect ( restoreRedactedValues ( incoming , original , hints ) ) . toEqual ( original ) ;
548+ } ) ;
549+
550+ it ( "keeps reordered hook-mapping session keys redacted until identity-based restoration" , ( ) => {
551+ const hints : ConfigUiHints = { "hooks.mappings[].sessionKey" : { sensitive : true } } ;
552+ const original = {
553+ hooks : {
554+ mappings : [
555+ { id : "alpha" , sessionKey : "synthetic-alpha-session" } ,
556+ { id : "bravo" , sessionKey : "synthetic-bravo-session" } ,
557+ ] ,
558+ } ,
559+ } ;
560+ const redacted = redactConfigSnapshot ( makeSnapshot ( original ) , hints ) ;
561+ const incoming = {
562+ hooks : { mappings : [ ( redacted . config as typeof original ) . hooks . mappings [ 1 ] ] } ,
563+ } ;
564+
565+ expect ( JSON . stringify ( redacted ) ) . not . toContain ( "synthetic-alpha-session" ) ;
566+ expect ( JSON . stringify ( redacted ) ) . not . toContain ( "synthetic-bravo-session" ) ;
567+ expect ( incoming . hooks . mappings [ 0 ] ?. sessionKey ) . toBe ( REDACTED_SENTINEL ) ;
568+ expect ( restoreRedactedValues ( incoming , original , hints ) . hooks . mappings ) . toEqual ( [
569+ { id : "bravo" , sessionKey : "synthetic-bravo-session" } ,
570+ ] ) ;
571+ } ) ;
572+
254573 it ( "restores redacted SecretRef ids for channels token paths" , ( ) => {
255574 const hints : ConfigUiHints = {
256575 "channels.discord.token" : { sensitive : true } ,
0 commit comments