Skip to content

Commit 7d88feb

Browse files
committed
fix(service-settings): a map key source that answered an empty list admits no key
mapShapeVerdict skipped the key check whenever the key's option table was empty, so a source that answered no keys (a host with no lifecycle object) admitted any key at both doors. The table now says whether a source answered; a sourced table judges every key, and only the declared fallback with no keys admits every key. Pins the empty-source refusal at both doors, the two shape misses #30208 left unpinned (an array value, a scalar entry in a several-column map), and corrects the lifecycle-service comment that still called these keys json. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5nBSzc6ktJyjdFcDb3
1 parent f4b1f9e commit 7d88feb

4 files changed

Lines changed: 119 additions & 8 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/service-settings': patch
3+
---
4+
5+
fix(service-settings): a `map` setting refuses every key when its key source answers an empty list
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: none
9+
10+
A `map` specifier's keys are judged against the key's option source (`registerOptionSource`), the list the running system answers, such as the lifecycle-declared objects for `lifecycle.quotas` and `lifecycle.retention_overrides`. When that source answered an empty list, the key check was skipped and any key was saved and put in force from `OS_*`. It now refuses each key with `invalid_option` at the save door and logs the override as not in force at the env door, as for any key the list does not hold. This narrows what is accepted: on a host with no lifecycle-declared object, a non-empty value for those keys, saved or set in `OS_LIFECYCLE_*`, was accepted before and is refused now; no such key could name an object the sweep reads. An empty map still saves. A key whose source could not answer, or which has no source, is still judged against its declared options and admits any key when it declares none.

‎packages/objectql/src/lifecycle/lifecycle-service.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,9 @@ export class LifecycleService {
11591159
* (`retention_overrides`, `quotas`, `quota_defaults`) as the map this service
11601160
* indexes, or `undefined` for "nothing configured".
11611161
*
1162-
* These keys are `json` specifiers, which persist an object (ADR-0007). Until
1163-
* #31241 the console's `json` field sent the textarea's TEXT and the settings
1162+
* These keys are `map` specifiers (#30208; `json` specifiers before it),
1163+
* which persist an object (ADR-0007). Until #31241, when they were still
1164+
* `json`, the console's `json` field sent the textarea's TEXT and the settings
11641165
* save door stored it, so a deployment that saved this page holds a JSON
11651166
* string where the map belongs; indexing that string reads every override and
11661167
* quota as absent, with nothing logged. The save door refuses a string now,

‎packages/services/service-settings/src/settings-map-shape-30208.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,94 @@ describe('[#30208] the env door judges a lifecycle map the same way', () => {
279279
expect(errors[0]).toContain('must be an object of keyed rows');
280280
});
281281
});
282+
283+
describe('[#31422] a key source that answered an empty list admits no key', () => {
284+
/** As `ObjectQLPlugin` registers them, over a registry with no lifecycle-declared object. */
285+
const bootWith = (registry: () => LifecycleKeyRegistryLike | undefined) =>
286+
bootSettingsDoor(lifecycleSettingsManifest, {
287+
before: (svc: SettingsService) => {
288+
svc.registerOptionSource('lifecycle', 'quotas', lifecycleObjectKeySource(registry));
289+
svc.registerOptionSource('lifecycle', 'retention_overrides', lifecycleObjectKeySource(registry));
290+
},
291+
});
292+
const NO_LIFECYCLE_OBJECTS: LifecycleKeyRegistryLike = {
293+
getAllObjects: () => [{ name: 'account', label: 'Account', lifecycle: { class: 'record' } }, { name: 'contact' }],
294+
};
295+
296+
it('the save door refuses every key: invalid_option, naming the entry; nothing stored', async () => {
297+
const { save, stored } = await bootWith(() => NO_LIFECYCLE_OBJECTS);
298+
299+
const st = await save({ quotas: { sys_audit_log: 1000 } });
300+
301+
expect(st.status).toBe(400);
302+
const fields = st.body.error.details.fields as Fields;
303+
expect(fields).toHaveLength(1);
304+
expect(fields[0]).toMatchObject({ field: 'quotas', code: 'invalid_option', constraint: { type: 'map', entry: 'sys_audit_log' } });
305+
expect(fields[0].message).toBe("Row quotas: 'sys_audit_log' is not a key this setting admits.");
306+
expect(stored('quotas')).toBeUndefined();
307+
});
308+
309+
it('the env door does not put such a map in force either', async () => {
310+
const errors: string[] = [];
311+
const svc = new SettingsService({
312+
env: { OS_LIFECYCLE_QUOTAS: '{"sys_audit_log": 1000}' },
313+
logger: { error: (m: string) => void errors.push(m), warn: () => {} } as any,
314+
});
315+
svc.registerOptionSource('lifecycle', 'quotas', lifecycleObjectKeySource(() => NO_LIFECYCLE_OBJECTS));
316+
svc.registerManifest(lifecycleSettingsManifest);
317+
318+
const got = await svc.get('lifecycle', 'quotas');
319+
320+
expect(got.source).not.toBe('env');
321+
expect(errors).toHaveLength(1);
322+
expect(errors[0]).toContain("'sys_audit_log' is not a key this setting admits.");
323+
});
324+
325+
it('CONTROL: an empty map still saves (a map with no entry names no key)', async () => {
326+
const { save, stored } = await bootWith(() => NO_LIFECYCLE_OBJECTS);
327+
328+
const st = await save({ quotas: {} });
329+
330+
expect(st.status).toBe(200);
331+
expect(stored('quotas')?.value).toEqual({});
332+
});
333+
334+
it('CONTROL: a source that could not answer (no registry) leaves the declared table, which declares no key: any key passes', async () => {
335+
const { save, stored } = await bootWith(() => undefined);
336+
337+
const st = await save({ quotas: { sys_audit_log: 1000 } });
338+
339+
expect(st.status).toBe(200);
340+
expect(stored('quotas')?.value).toEqual({ sys_audit_log: 1000 });
341+
});
342+
});
343+
344+
describe('[#31422] the two shape misses #30208 left unpinned', () => {
345+
it('an array is not a map: invalid_type (not_a_map), naming what was sent', async () => {
346+
const { save, stored } = await boot();
347+
348+
const st = await save({ quotas: [1000] });
349+
350+
expect(st.status).toBe(400);
351+
const fields = st.body.error.details.fields as Fields;
352+
expect(fields).toHaveLength(1);
353+
expect(fields[0]).toMatchObject({ field: 'quotas', code: 'invalid_type', constraint: { type: 'map' } });
354+
expect(String(fields[0].message)).toMatch(/^Row quotas must be an object of keyed rows, such as .*, not an array/);
355+
expect(stored('quotas')).toBeUndefined();
356+
});
357+
358+
it('a scalar entry in a several-column map: invalid_shape (not_an_entry), naming the entry and the columns', async () => {
359+
const { save, stored } = await boot();
360+
361+
const st = await save({ retention_overrides: { sys_job_run: '1y' } });
362+
363+
expect(st.status).toBe(400);
364+
const fields = st.body.error.details.fields as Fields;
365+
expect(fields).toHaveLength(1);
366+
expect(fields[0]).toMatchObject({ field: 'retention_overrides', code: 'invalid_shape', constraint: { type: 'map', entry: 'sys_job_run' } });
367+
expect(String(fields[0].message)).toMatch(
368+
/^Retention overrides: 'sys_job_run' must be an object with at least one of maxAge, expireAfter, such as .*, not a string of 2 characters\.$/,
369+
);
370+
expect(stored('retention_overrides')).toBeUndefined();
371+
});
372+
});

‎packages/services/service-settings/src/settings-service.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,17 +1942,20 @@ export class SettingsService {
19421942
* the judgment of a written value: a registered source's answer when it has
19431943
* one, else `declared`, judged by {@link firstRejectedOption}. Both doors ask
19441944
* here, the save (`validatePatch`) and the `OS_*` override
1945-
* (`effectiveEnvOverride`), so a source moves both at once.
1945+
* (`effectiveEnvOverride`), so a source moves both at once. `sourced` says
1946+
* which table it is: `true` when a registered source answered (even with no
1947+
* options), `false` for the declared fallback.
19461948
*/
19471949
private optionTableOf(
19481950
namespace: string,
19491951
key: string,
19501952
declared: string[],
1951-
): { allowed: string[]; judge: (value: unknown) => { value: unknown } | null } {
1953+
): { allowed: string[]; sourced: boolean; judge: (value: unknown) => { value: unknown } | null } {
19521954
const answer = this.optionSourceAnswer(namespace, key);
1953-
if (!answer) return { allowed: declared, judge: (value) => firstRejectedOption(declared, value) };
1955+
if (!answer) return { allowed: declared, sourced: false, judge: (value) => firstRejectedOption(declared, value) };
19541956
return {
19551957
allowed: answer.options.map((opt) => opt.value),
1958+
sourced: true,
19561959
judge: (value) => {
19571960
// Element-wise and in string form, as `firstRejectedOption` judges.
19581961
const picked = Array.isArray(value) ? value : [value];
@@ -1970,7 +1973,13 @@ export class SettingsService {
19701973
* ONE judgement for both doors ({@link firstMapShapeMiss}); the key column's
19711974
* domain is the key's option table as {@link optionTableOf} serves it, a
19721975
* registered source's answer else the declared `options`, so a source moves
1973-
* both doors at once. An empty table admits every key.
1976+
* both doors at once.
1977+
*
1978+
* [#31422] A source that answered is the domain even when it answered no
1979+
* keys: the running system has nothing a key could name (a host with no
1980+
* lifecycle-declared object), so every key is refused. Only the declared
1981+
* fallback with no keys (no source registered, or one that could not answer)
1982+
* admits every key, as a key with no declared `options` always has.
19741983
*/
19751984
private mapShapeVerdict(
19761985
reg: RegisteredManifest,
@@ -1980,8 +1989,8 @@ export class SettingsService {
19801989
): { shape: DeclaredMapShape; miss: MapShapeMiss; allowedKeys: string[] } | null {
19811990
const map = reg.maps.get(key);
19821991
if (!map) return null;
1983-
const { allowed, judge } = this.optionTableOf(namespace, key, map.declaredKeys);
1984-
const admitsKey = allowed.length > 0 ? (entry: string) => judge(entry) === null : undefined;
1992+
const { allowed, sourced, judge } = this.optionTableOf(namespace, key, map.declaredKeys);
1993+
const admitsKey = sourced || allowed.length > 0 ? (entry: string) => judge(entry) === null : undefined;
19851994
const miss = firstMapShapeMiss(map.shape, value, admitsKey);
19861995
return miss ? { shape: map.shape, miss, allowedKeys: allowed } : null;
19871996
}

0 commit comments

Comments
 (0)