@@ -18,6 +18,7 @@ import { companySettingsManifest } from './manifests/company.manifest.js';
1818import { brandingSettingsManifest } from './manifests/branding.manifest.js' ;
1919import { builtinSettingsManifests } from './manifests/index.js' ;
2020import { evaluateVisibility , visibilitySource } from './visibility-eval.js' ;
21+ import { SETTINGS_SECRET_MASK } from './settings-secret-redaction.js' ;
2122import { SettingsManifestSchema } from '@objectstack/spec/system' ;
2223
2324describe ( 'reference manifests are spec-valid' , ( ) => {
@@ -2969,3 +2970,161 @@ describe('SettingsService — a reset clears the value stored beneath an env pin
29692970 expect ( r . message ) . toBe ( 'Cleared 1 saved value(s); environment/default configuration is back in effect.' ) ;
29702971 } ) ;
29712972} ) ;
2973+
2974+ describe ( 'SettingsService — the namespace read lists what is stored beneath an env pin' , ( ) => {
2975+ // A pin on each rung a clear can land at, a secret, and a JSON key whose env
2976+ // value can coerce to null.
2977+ const BENEATH = {
2978+ namespace : 'beneath' ,
2979+ version : 1 ,
2980+ label : 'Beneath' ,
2981+ scope : 'tenant' ,
2982+ specifiers : [
2983+ { type : 'text' , key : 'motto' , label : 'Motto' , scope : 'tenant' , default : 'none' } ,
2984+ { type : 'text' , key : 'banner' , label : 'Banner' , scope : 'global' , default : 'none' } ,
2985+ { type : 'text' , key : 'nickname' , label : 'Nickname' , scope : 'user' , default : 'none' } ,
2986+ { type : 'password' , key : 'api_secret' , label : 'API secret' , scope : 'tenant' } ,
2987+ { type : 'password' , key : 'spare_secret' , label : 'Spare secret' , scope : 'tenant' } ,
2988+ { type : 'json' , key : 'rules' , label : 'Rules' , scope : 'tenant' , default : [ ] } ,
2989+ ] ,
2990+ } as any ;
2991+ const envEntry = ( value : unknown , envName : string ) => ( {
2992+ scope : 'env' , value, locked : true , lockedReason : `Set via env: ${ envName } ` , effective : true ,
2993+ } ) ;
2994+
2995+ /** A crypto adapter whose decrypt is watched, so a read can prove it never opened a secret. */
2996+ function watchedCrypto ( ) : CryptoAdapter & { decrypt : ReturnType < typeof vi . fn > } {
2997+ return {
2998+ confidential : true ,
2999+ encrypt : async ( plain : string ) => 'kms:' + Buffer . from ( plain , 'utf8' ) . toString ( 'base64' ) ,
3000+ decrypt : vi . fn ( async ( cipher : string ) => Buffer . from ( cipher . replace ( / ^ k m s : / , '' ) , 'base64' ) . toString ( 'utf8' ) ) ,
3001+ digest : ( ) => 'fnv32:deadbeef' ,
3002+ } ;
3003+ }
3004+
3005+ /** `motto` stored then pinned: the operator's recovery, before the clear. */
3006+ async function pinnedOverStored ( ) {
3007+ const env : Record < string , string | undefined > = { } ;
3008+ const crypto = watchedCrypto ( ) ;
3009+ const svc = new SettingsService ( { env, crypto } ) ;
3010+ svc . registerManifest ( BENEATH ) ;
3011+ await svc . set ( 'beneath' , 'motto' , 'Stored motto' ) ;
3012+ env . OS_BENEATH_MOTTO = 'Pinned motto' ;
3013+ return { env, svc, crypto } ;
3014+ }
3015+
3016+ it ( 'a pinned key over a stored row lists that row beneath its env entry; the answer is otherwise the pin\'s' , async ( ) => {
3017+ const { svc } = await pinnedOverStored ( ) ;
3018+ const { values } = await svc . getNamespace ( 'beneath' ) ;
3019+
3020+ expect ( values . motto ) . toEqual ( {
3021+ value : 'Pinned motto' ,
3022+ source : 'env' ,
3023+ locked : true ,
3024+ lockedReason : 'Set via env: OS_BENEATH_MOTTO' ,
3025+ cascadeChain : [
3026+ envEntry ( 'Pinned motto' , 'OS_BENEATH_MOTTO' ) ,
3027+ { scope : 'tenant' , value : 'Stored motto' , locked : false , lockedReason : undefined } ,
3028+ { scope : 'default' , value : 'none' } ,
3029+ ] ,
3030+ } ) ;
3031+ // The env entry is the only effective one, and the first non-null one.
3032+ expect ( values . motto . cascadeChain ! . filter ( ( e ) => e . effective ) ) . toHaveLength ( 1 ) ;
3033+ } ) ;
3034+
3035+ it ( 'after the clear through the pin the stored entry reads null; a pin with no row lists none' , async ( ) => {
3036+ const { env, svc } = await pinnedOverStored ( ) ;
3037+ await svc . set ( 'beneath' , 'motto' , null ) ;
3038+ env . OS_BENEATH_BANNER = 'Pinned banner' ;
3039+
3040+ const { values } = await svc . getNamespace ( 'beneath' ) ;
3041+ expect ( values . motto . cascadeChain ) . toEqual ( [
3042+ envEntry ( 'Pinned motto' , 'OS_BENEATH_MOTTO' ) ,
3043+ { scope : 'tenant' , value : null , locked : false , lockedReason : undefined } ,
3044+ { scope : 'default' , value : 'none' } ,
3045+ ] ) ;
3046+ expect ( values . banner . cascadeChain ) . toEqual ( [
3047+ envEntry ( 'Pinned banner' , 'OS_BENEATH_BANNER' ) ,
3048+ { scope : 'default' , value : 'none' } ,
3049+ ] ) ;
3050+ } ) ;
3051+
3052+ it ( 'a global key lists its global row, and an upper row\'s lock rides along, so a refused clear is visible' , async ( ) => {
3053+ const { env, svc } = await pinnedOverStored ( ) ;
3054+ await svc . set ( 'beneath' , 'banner' , 'Stored banner' ) ;
3055+ env . OS_BENEATH_BANNER = 'Pinned banner' ;
3056+ await ( svc as any ) . upsertRow ( {
3057+ namespace : 'beneath' , key : 'motto' , scope : 'global' , user_id : null , value : 'Platform motto' ,
3058+ value_enc : null , encrypted : false , locked : true , locked_reason : 'Set by the platform.' ,
3059+ } ) ;
3060+
3061+ const { values } = await svc . getNamespace ( 'beneath' ) ;
3062+ expect ( values . banner . cascadeChain ) . toEqual ( [
3063+ envEntry ( 'Pinned banner' , 'OS_BENEATH_BANNER' ) ,
3064+ { scope : 'global' , value : 'Stored banner' , locked : false , lockedReason : undefined } ,
3065+ { scope : 'default' , value : 'none' } ,
3066+ ] ) ;
3067+ expect ( values . motto . cascadeChain ! . slice ( 1 , 3 ) ) . toEqual ( [
3068+ { scope : 'global' , value : 'Platform motto' , locked : true , lockedReason : 'Set by the platform.' } ,
3069+ { scope : 'tenant' , value : 'Stored motto' , locked : false , lockedReason : undefined } ,
3070+ ] ) ;
3071+ // The pin still answers: an upper row's lock does not take the lock over.
3072+ expect ( values . motto ) . toMatchObject ( { source : 'env' , lockedReason : 'Set via env: OS_BENEATH_MOTTO' } ) ;
3073+ } ) ;
3074+
3075+ it ( 'a user key lists the caller\'s own row only' , async ( ) => {
3076+ const env : Record < string , string | undefined > = { } ;
3077+ const svc = new SettingsService ( { env } ) ;
3078+ svc . registerManifest ( BENEATH ) ;
3079+ await svc . set ( 'beneath' , 'nickname' , 'Ada' , { userId : 'usr_a' } ) ;
3080+ await svc . set ( 'beneath' , 'nickname' , 'Bob' , { userId : 'usr_b' } ) ;
3081+ env . OS_BENEATH_NICKNAME = 'Pinned' ;
3082+
3083+ const forA = ( await svc . getNamespace ( 'beneath' , { userId : 'usr_a' } ) ) . values . nickname . cascadeChain ! ;
3084+ expect ( forA . slice ( 1 ) ) . toEqual ( [ { scope : 'user' , value : 'Ada' } , { scope : 'default' , value : 'none' } ] ) ;
3085+ const forNobody = ( await svc . getNamespace ( 'beneath' ) ) . values . nickname . cascadeChain ! ;
3086+ expect ( forNobody . slice ( 1 ) ) . toEqual ( [ { scope : 'default' , value : 'none' } ] ) ;
3087+ } ) ;
3088+
3089+ it ( 'a secret beneath a pin shows that a value is stored, never the value, and is never decrypted' , async ( ) => {
3090+ const { env, svc, crypto } = await pinnedOverStored ( ) ;
3091+ await svc . set ( 'beneath' , 'api_secret' , 'stored-secret-plaintext' ) ;
3092+ env . OS_BENEATH_API_SECRET = 'env-secret' ;
3093+ env . OS_BENEATH_SPARE_SECRET = 'env-spare' ;
3094+ crypto . decrypt . mockClear ( ) ;
3095+
3096+ const { values } = await svc . getNamespace ( 'beneath' ) ;
3097+ expect ( values . api_secret . cascadeChain ) . toEqual ( [
3098+ envEntry ( 'env-secret' , 'OS_BENEATH_API_SECRET' ) ,
3099+ { scope : 'tenant' , value : SETTINGS_SECRET_MASK , locked : false , lockedReason : undefined } ,
3100+ { scope : 'default' , value : null } ,
3101+ ] ) ;
3102+ expect ( values . spare_secret . cascadeChain ! . slice ( 1 ) ) . toEqual ( [ { scope : 'default' , value : null } ] ) ;
3103+ expect ( JSON . stringify ( values ) ) . not . toContain ( 'stored-secret-plaintext' ) ;
3104+ expect ( crypto . decrypt ) . not . toHaveBeenCalled ( ) ;
3105+
3106+ // Cleared through the pin, the secret's stored entry reads null.
3107+ await svc . set ( 'beneath' , 'api_secret' , null ) ;
3108+ const after = ( await svc . getNamespace ( 'beneath' ) ) . values . api_secret . cascadeChain ! ;
3109+ expect ( after [ 1 ] ) . toEqual ( { scope : 'tenant' , value : null , locked : false , lockedReason : undefined } ) ;
3110+ } ) ;
3111+
3112+ it ( 'an override whose value coerces to null keeps its single env entry' , async ( ) => {
3113+ const env : Record < string , string | undefined > = { } ;
3114+ const svc = new SettingsService ( { env } ) ;
3115+ svc . registerManifest ( BENEATH ) ;
3116+ await svc . set ( 'beneath' , 'rules' , [ 'stored' ] ) ;
3117+ env . OS_BENEATH_RULES = 'null' ;
3118+
3119+ const { values } = await svc . getNamespace ( 'beneath' ) ;
3120+ expect ( values . rules ) . toMatchObject ( { value : null , source : 'env' , locked : true } ) ;
3121+ expect ( values . rules . cascadeChain ) . toEqual ( [ envEntry ( null , 'OS_BENEATH_RULES' ) ] ) ;
3122+ } ) ;
3123+
3124+ it ( 'get() and getMany() still answer a pin from the env alone' , async ( ) => {
3125+ const { svc } = await pinnedOverStored ( ) ;
3126+ const pinOnly = [ envEntry ( 'Pinned motto' , 'OS_BENEATH_MOTTO' ) ] ;
3127+ expect ( ( await svc . get ( 'beneath' , 'motto' ) ) . cascadeChain ) . toEqual ( pinOnly ) ;
3128+ expect ( ( await svc . getMany ( 'beneath' , [ 'motto' ] ) ) . motto . cascadeChain ) . toEqual ( pinOnly ) ;
3129+ } ) ;
3130+ } ) ;
0 commit comments