Skip to content

Commit 92a9cc5

Browse files
committed
fix: recheck config access after parsing
1 parent c80810e commit 92a9cc5

2 files changed

Lines changed: 98 additions & 14 deletions

File tree

‎ui/src/lib/config/index.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,33 @@ describe("config form auto-save", () => {
638638
runtimeConfig.dispose();
639639
});
640640

641+
it("rechecks operator access after the original-config parser settles", async () => {
642+
vi.useFakeTimers();
643+
const server = createConfigServerMock();
644+
const client = { request: server.request } as unknown as GatewayBrowserClient;
645+
const { gateway, publish } = createGatewayHarness(client);
646+
const runtimeConfig = createRuntimeConfigCapability(gateway);
647+
const originalParse = deferred<void>();
648+
649+
await runtimeConfig.ensureLoaded();
650+
runtimeConfig.state.configRawOriginalParsePending = originalParse.promise;
651+
runtimeConfig.patchForm(["count"], 2);
652+
const timerAdvance = vi.advanceTimersByTimeAsync(CONFIG_FORM_AUTO_SAVE_DEBOUNCE_MS);
653+
await Promise.resolve();
654+
publish(true, client, {
655+
type: "hello-ok",
656+
protocol: 1,
657+
auth: { role: "operator", scopes: ["operator.read"] },
658+
features: { methods: ["config.get", "config.set"] },
659+
} as GatewayHelloOk);
660+
originalParse.resolve();
661+
await timerAdvance;
662+
663+
expect(server.submissions).toHaveLength(0);
664+
expect(runtimeConfig.state.configFormDirty).toBe(true);
665+
runtimeConfig.dispose();
666+
});
667+
641668
it("keeps mid-flight edits dirty and queues exactly one trailing save", async () => {
642669
vi.useFakeTimers();
643670
const { request, submissions, firstSet } = createDeferredSetServerMock();
@@ -2933,6 +2960,33 @@ describe("config form auto-save", () => {
29332960
runtimeConfig.dispose();
29342961
});
29352962

2963+
it.each([
2964+
{ action: "save" as const, method: "config.set" },
2965+
{ action: "apply" as const, method: "config.apply" },
2966+
])("rechecks operator access after parsing before $method dispatch", async ({ action }) => {
2967+
const server = createConfigServerMock();
2968+
const client = { request: server.request } as unknown as GatewayBrowserClient;
2969+
const { gateway, publish } = createGatewayHarness(client);
2970+
const runtimeConfig = createRuntimeConfigCapability(gateway);
2971+
const originalParse = deferred<void>();
2972+
2973+
await runtimeConfig.ensureLoaded();
2974+
runtimeConfig.state.configRawOriginalParsePending = originalParse.promise;
2975+
const result = runtimeConfig[action]();
2976+
await Promise.resolve();
2977+
publish(true, client, {
2978+
type: "hello-ok",
2979+
protocol: 1,
2980+
auth: { role: "operator", scopes: ["operator.read"] },
2981+
features: { methods: ["config.get", "config.apply", "config.set"] },
2982+
} as GatewayHelloOk);
2983+
originalParse.resolve();
2984+
2985+
await expect(result).resolves.toBe(false);
2986+
expect(server.submissions).toHaveLength(0);
2987+
runtimeConfig.dispose();
2988+
});
2989+
29362990
it("defers autosaves behind a manual write and keeps the newer edit", async () => {
29372991
vi.useFakeTimers();
29382992
const { request, submissions, firstSet } = createDeferredSetServerMock();

‎ui/src/lib/config/index.ts‎

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ async function submitConfigChange(
707707
busyKey: ConfigSubmitBusyKey,
708708
extraParams: Record<string, unknown> = {},
709709
onSubmitted?: (info: { raw: string; ackHash: string | null }) => void,
710+
canDispatch: () => boolean = () => true,
710711
): Promise<boolean> {
711712
const client = state.client;
712713
if (!client || !state.connected) {
@@ -737,6 +738,9 @@ async function submitConfigChange(
737738
state.lastError = "Config hash missing; reload and retry.";
738739
return false;
739740
}
741+
if (!isCurrent() || !canDispatch()) {
742+
return false;
743+
}
740744
// Dispatch-phase report (ackHash null): if the connection dies before the
741745
// ack arrives, reconnect reconciliation still needs the submitted bytes
742746
// to recognize its own committed write. The post-ack report below
@@ -812,12 +816,16 @@ function teardownFlushConfigDraft(
812816
state: ConfigState,
813817
client: GatewayBrowserClient,
814818
baseHash: string,
819+
canDispatch: () => boolean,
815820
): void {
816821
// Must stay synchronous: page unload destroys the context before any
817822
// deferred work runs. If a JSON5 original parse is still pending, sanitize
818823
// passes placeholders through; the gateway restores restorable sentinels
819824
// (restoreRedactedValues) and rejects unrestorable ones, so the worst case
820825
// matches not flushing at all while the common case saves the draft.
826+
if (!canDispatch()) {
827+
return;
828+
}
821829
const raw = serializeFormForSubmit(state);
822830
void client.request("config.set", { raw, baseHash }).catch(() => undefined);
823831
}
@@ -832,6 +840,7 @@ function teardownFlushConfigDraft(
832840
async function autoSaveConfig(
833841
state: ConfigState,
834842
onAck?: (ackHash: string | null) => void,
843+
canDispatch: () => boolean = () => true,
835844
): Promise<boolean> {
836845
const client = state.client;
837846
if (!client || !state.connected || !state.configFormDirty || state.configFormMode !== "form") {
@@ -856,6 +865,9 @@ async function autoSaveConfig(
856865
state.lastError = "Config hash missing; reload and retry.";
857866
return false;
858867
}
868+
if (!isCurrent() || !canDispatch()) {
869+
return false;
870+
}
859871
state.configAutoSaveStatus = "saving";
860872
state.lastError = null;
861873
state.chatError = null;
@@ -941,14 +953,22 @@ function resetStaleAutoSaveStatus(state: ConfigState) {
941953
async function saveConfig(
942954
state: ConfigState,
943955
onSubmitted?: (info: { raw: string; ackHash: string | null }) => void,
956+
canDispatch?: () => boolean,
944957
): Promise<boolean> {
945-
return submitConfigChange(state, "config.set", "configSaving", {}, onSubmitted);
958+
return submitConfigChange(state, "config.set", "configSaving", {}, onSubmitted, canDispatch);
946959
}
947960

948-
async function applyConfig(state: ConfigState): Promise<boolean> {
949-
return submitConfigChange(state, "config.apply", "configApplying", {
950-
sessionKey: state.applySessionKey,
951-
});
961+
async function applyConfig(state: ConfigState, canDispatch?: () => boolean): Promise<boolean> {
962+
return submitConfigChange(
963+
state,
964+
"config.apply",
965+
"configApplying",
966+
{
967+
sessionKey: state.applySessionKey,
968+
},
969+
undefined,
970+
canDispatch,
971+
);
952972
}
953973

954974
async function patchConfig(
@@ -1514,9 +1534,13 @@ export function createRuntimeConfigCapability(
15141534
lastFlightSubmittedRaw = serializeFormForSubmit(state);
15151535
lastFlightAckHash = null;
15161536
const flight = run(() =>
1517-
autoSaveConfig(state, (ackHash) => {
1518-
lastFlightAckHash = ackHash;
1519-
}),
1537+
autoSaveConfig(
1538+
state,
1539+
(ackHash) => {
1540+
lastFlightAckHash = ackHash;
1541+
},
1542+
() => canCallConfigMethod("config.set"),
1543+
),
15201544
)
15211545
.catch(() => false)
15221546
.then((saved) => {
@@ -1990,9 +2014,13 @@ export function createRuntimeConfigCapability(
19902014
async () => {
19912015
cancelAppliedRefresh();
19922016
try {
1993-
return await saveConfig(state, (info) => {
1994-
manualFlightInfo = info;
1995-
});
2017+
return await saveConfig(
2018+
state,
2019+
(info) => {
2020+
manualFlightInfo = info;
2021+
},
2022+
() => canCallConfigMethod("config.set"),
2023+
);
19962024
} finally {
19972025
reconcileAppliedRefresh();
19982026
}
@@ -2017,7 +2045,7 @@ export function createRuntimeConfigCapability(
20172045
return false;
20182046
}
20192047
try {
2020-
return await applyConfig(state);
2048+
return await applyConfig(state, () => canCallConfigMethod("config.apply"));
20212049
} finally {
20222050
reconcileAppliedRefresh();
20232051
}
@@ -2223,11 +2251,13 @@ export function createRuntimeConfigCapability(
22232251
// pre-save value reads configFormDirty=false while the persisted
22242252
// bytes are still the unreverted submission.
22252253
if (ackHash && submittedRaw !== null && serializeFormForSubmit(state) !== submittedRaw) {
2226-
teardownFlushConfigDraft(state, client, ackHash);
2254+
teardownFlushConfigDraft(state, client, ackHash, () =>
2255+
canCallConfigMethod("config.set"),
2256+
);
22272257
}
22282258
});
22292259
} else if (canFlush && state.configFormDirty) {
2230-
void autoSaveConfig(state);
2260+
void autoSaveConfig(state, undefined, () => canCallConfigMethod("config.set"));
22312261
}
22322262
invalidateConfigConnection(state);
22332263
state.connected = false;

0 commit comments

Comments
 (0)