Skip to content

Commit 4fec7b1

Browse files
committed
fix(policy): accept mixed-case registry checkbox names
1 parent 297de1f commit 4fec7b1

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

‎apps/server/src/policy/ManagedPolicy.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ const make = Effect.gen(function* () {
185185
const policy = yield* Option.match(devPath, {
186186
onSome: load,
187187
onNone: () =>
188-
readManagedPolicy(platform).pipe(
188+
readManagedPolicy(
189+
platform,
190+
ManagedProviders.knownDriverLabels().map(({ driver }) => driver),
191+
).pipe(
189192
Effect.flatMap((found) =>
190193
fromDocument(found?.source ?? managedPolicyPath(platform), found?.document),
191194
),

‎packages/shared/src/managedPolicyFile.test.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe("readManagedPolicy", () => {
208208
Effect.gen(function* () {
209209
const organization = text("Organization", "Société 日本 ✓");
210210
const document = (registry: RegistryAnswer) =>
211-
readManagedPolicy("win32").pipe(
211+
readManagedPolicy("win32", ["codex", "claudeAgent"]).pipe(
212212
Effect.map((found): unknown => found?.document),
213213
Effect.catchTags({
214214
ManagedPolicyFileError: (error) => Effect.succeed(`error: ${error.reason}`),
@@ -233,7 +233,10 @@ describe("readManagedPolicy", () => {
233233
dword("AllowedDrivers", 1),
234234
],
235235
// Registry names ignore case, so a subkey written in other case still counts.
236-
{ clients: { desktop: 1, remote: 0 }, AllowedDrivers: { codex: 1, claudeAgent: 1 } },
236+
{
237+
clients: { Desktop: 1, "LOCAL-BROWSER": 1, REMOTE: 1 },
238+
AllowedDrivers: { Codex: 1, CLAUDEAGENT: 1, UnknownDriver: 1 },
239+
},
237240
),
238241
),
239242
{
@@ -244,8 +247,8 @@ describe("readManagedPolicy", () => {
244247
maxRuntimeMode: "auto-accept-edits",
245248
telemetry: false,
246249
updates: { mode: "off", allowNightly: true },
247-
clients: ["desktop"],
248-
providers: { allowedDrivers: ["codex", "claudeAgent"] },
250+
clients: ["desktop", "local-browser", "remote"],
251+
providers: { allowedDrivers: ["codex", "claudeAgent", "UnknownDriver"] },
249252
},
250253
},
251254
);
@@ -295,6 +298,10 @@ describe("readManagedPolicy", () => {
295298
],
296299
[policyKey([organization, text("Settings", "{")]), "not valid JSON"],
297300
[policyKey([organization, dword("Clients", 2)]), "DWORD of 0 or 1"],
301+
[
302+
policyKey([organization, dword("Clients", 1)], { Clients: { UnknownClient: 1 } }),
303+
"does not match the policy schema",
304+
],
298305
[
299306
policyKey([organization, dword("Clients", 1)], { Clients: { desktop: 0 } }),
300307
"does not match the policy schema",

‎packages/shared/src/managedPolicyFile.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ManagedPolicyDocument } from "@t3tools/contracts";
1+
import { ManagedPolicyClient, ManagedPolicyDocument } from "@t3tools/contracts";
22
import * as Cause from "effect/Cause";
33
import * as Context from "effect/Context";
44
import * as Duration from "effect/Duration";
@@ -369,6 +369,7 @@ const dwordBoolean = (value: RegistryValue) =>
369369
*/
370370
function windowsPolicyDocument(
371371
values: ReadonlyArray<PolicyRegistryValue>,
372+
knownDrivers: ReadonlyArray<string>,
372373
): { readonly document: unknown } | { readonly error: string } {
373374
// Registry names ignore case; a hand-written `organization` is still Organization.
374375
const canonical = new Map(
@@ -407,7 +408,12 @@ function windowsPolicyDocument(
407408
if (checked === undefined) return { error: `${where} must be a DWORD of 0 or 1` };
408409
const list = lists.get(value.key) ?? [];
409410
lists.set(value.key, list);
410-
if (checked) list.push(value.name);
411+
if (checked) {
412+
const names = value.key === "Clients" ? ManagedPolicyClient.literals : knownDrivers;
413+
list.push(
414+
names.find((name) => name.toLowerCase() === value.name.toLowerCase()) ?? value.name,
415+
);
416+
}
411417
continue;
412418
}
413419
if (Object.hasOwn(WINDOWS_POLICY_GROUPS, value.name)) continue;
@@ -476,7 +482,10 @@ function unwrapForced(json: string): unknown {
476482
}
477483

478484
/** Read the policy a configuration profile or Group Policy set; absent means no such policy. */
479-
const readPlatformPolicy = Effect.fnUntraced(function* (platform: NodeJS.Platform) {
485+
const readPlatformPolicy = Effect.fnUntraced(function* (
486+
platform: NodeJS.Platform,
487+
knownDrivers: ReadonlyArray<string>,
488+
) {
480489
if (platform === "darwin") {
481490
const path = MACOS_MANAGED_PREFERENCES_PATH;
482491
const exists = yield* (yield* FileSystem.FileSystem)
@@ -543,7 +552,7 @@ const readPlatformPolicy = Effect.fnUntraced(function* (platform: NodeJS.Platfor
543552
}
544553
values.push(...checkboxes.values.map((value) => ({ ...value, key: list })));
545554
}
546-
const built = windowsPolicyDocument(values);
555+
const built = windowsPolicyDocument(values, knownDrivers);
547556
if ("error" in built) return yield* refuse(built.error);
548557
// An empty key, or one holding only policies set to Disabled, sets nothing.
549558
if (built.document === undefined) return undefined;
@@ -559,10 +568,11 @@ const readPlatformPolicy = Effect.fnUntraced(function* (platform: NodeJS.Platfor
559568
*/
560569
export const readManagedPolicy = Effect.fn("readManagedPolicy")(function* (
561570
platform: NodeJS.Platform,
571+
knownDrivers: ReadonlyArray<string> = [],
562572
) {
563573
const filePath = managedPolicyPath(platform);
564574
const [platformPolicy, file] = yield* Effect.all([
565-
readPlatformPolicy(platform),
575+
readPlatformPolicy(platform, knownDrivers),
566576
readManagedPolicyFile(filePath),
567577
]);
568578
if (platformPolicy !== undefined && file !== undefined) {

0 commit comments

Comments
 (0)