The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * ManagedPolicy - the administrator's machine-wide policy file, read once at startup. A file this |
| 3 | * build cannot fully enforce stops the server instead of leaving it looking managed. |
| 4 | * |
| 5 | * @module ManagedPolicy |
| 6 | */ |
| 7 | import * as NodeModule from "node:module"; |
| 8 | |
| 9 | import { ManagedPolicyDocument, type ManagedPolicySummary } from "@t3tools/contracts"; |
| 10 | import * as Config from "effect/Config"; |
| 11 | import * as Context from "effect/Context"; |
| 12 | import * as Digest from "effect/crypto/Digest"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Hex from "effect/encoding/Hex"; |
| 15 | import * as Layer from "effect/Layer"; |
| 16 | import * as Option from "effect/Option"; |
| 17 | import * as Schema from "effect/Schema"; |
| 18 | |
| 19 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 20 | import { |
| 21 | ManagedPolicyFileError, |
| 22 | managedPolicyPath, |
| 23 | layerWindowsRegistry, |
| 24 | readManagedPolicy, |
| 25 | readManagedPolicyFile, |
| 26 | } from "@t3tools/shared/managedPolicyFile"; |
| 27 | |
| 28 | import { telemetryChannel } from "../config.ts"; |
| 29 | import * as ManagedLimits from "./managedLimits.ts"; |
| 30 | import * as ManagedProviders from "./managedProviders.ts"; |
| 31 | import * as ManagedSettings from "./managedSettings.ts"; |
| 32 | |
| 33 | export class ManagedPolicy extends Context.Service< |
| 34 | ManagedPolicy, |
| 35 | { |
| 36 | /** Where the policy is read from, whether or not a file exists there. */ |
| 37 | readonly path: string; |
| 38 | readonly document: ManagedPolicyDocument | undefined; |
| 39 | readonly settings: ManagedSettings.ManagedSettings; |
| 40 | readonly providers: ManagedProviders.ManagedProviders; |
| 41 | /** What clients are told; absent when no policy applies. */ |
| 42 | readonly summary: ManagedPolicySummary | undefined; |
| 43 | } |
| 44 | >()("t3/policy/ManagedPolicy") {} |
| 45 | |
| 46 | type LimitsSummary = NonNullable<ManagedPolicySummary["limits"]>; |
| 47 | |
| 48 | const encodeDocumentJson = Schema.encodeEffect(Schema.fromJsonString(ManagedPolicyDocument)); |
| 49 | |
| 50 | /** What `t3 policy show` compares to tell whether a running server loaded the installed policy. */ |
| 51 | export const loadedPolicy = Effect.fn("ManagedPolicy.loadedPolicy")(function* ( |
| 52 | policy: Pick<ManagedPolicy["Service"], "path" | "document">, |
| 53 | ) { |
| 54 | if (policy.document === undefined) return null; |
| 55 | const json = yield* encodeDocumentJson(policy.document).pipe(Effect.orDie); |
| 56 | const digest = yield* (yield* Digest.Digest).digest( |
| 57 | Digest.sha256, |
| 58 | new TextEncoder().encode(json), |
| 59 | ); |
| 60 | return { |
| 61 | source: policy.path, |
| 62 | organization: policy.document.organization, |
| 63 | fingerprint: Hex.encode(digest).slice(0, 12), |
| 64 | }; |
| 65 | }); |
| 66 | |
| 67 | /** |
| 68 | * The organization whose policy switches `capability` off, or undefined when it is allowed. |
| 69 | * Only an explicit `false` restricts. |
| 70 | */ |
| 71 | export function deniedBy( |
| 72 | policy: Pick<ManagedPolicy["Service"], "document">, |
| 73 | capability: "webhookTasks" | "telemetry" | "previewTunnel", |
| 74 | ): string | undefined { |
| 75 | return policy.document?.limits?.[capability] === false ? policy.document.organization : undefined; |
| 76 | } |
| 77 | |
| 78 | /** Build the service from a decoded document; fails when the document cannot be enforced. */ |
| 79 | export const fromDocument = Effect.fn("ManagedPolicy.fromDocument")(function* ( |
| 80 | path: string, |
| 81 | document: ManagedPolicyDocument | undefined, |
| 82 | ) { |
| 83 | if (document === undefined) { |
| 84 | return ManagedPolicy.of({ |
| 85 | path, |
| 86 | document: undefined, |
| 87 | settings: ManagedSettings.unmanagedSettings, |
| 88 | providers: ManagedProviders.unmanagedProviders, |
| 89 | summary: undefined, |
| 90 | }); |
| 91 | } |
| 92 | const maxRuntimeMode = document.limits?.maxRuntimeMode; |
| 93 | const settings = ManagedSettings.makeManagedSettings(document.settings ?? {}, maxRuntimeMode); |
| 94 | if (settings._tag === "Invalid") { |
| 95 | return yield* new ManagedPolicyFileError({ path, reason: settings.reason }); |
| 96 | } |
| 97 | // A ceiling alone also shapes providers: their launch configuration becomes the administrator's. |
| 98 | const providerLimits = document.limits?.providers; |
| 99 | const providers = |
| 100 | providerLimits === undefined && maxRuntimeMode === undefined |
| 101 | ? undefined |
| 102 | : ManagedProviders.makeManagedProviders( |
| 103 | providerLimits ?? {}, |
| 104 | maxRuntimeMode, |
| 105 | yield* HostProcess.Platform, |
| 106 | ); |
| 107 | if (providers?._tag === "Invalid") { |
| 108 | return yield* new ManagedPolicyFileError({ path, reason: providers.reason }); |
| 109 | } |
| 110 | const lockedInstanceFields = providers?.providers.lockedInstanceFields ?? {}; |
| 111 | return ManagedPolicy.of({ |
| 112 | path, |
| 113 | document, |
| 114 | settings: settings.settings, |
| 115 | providers: providers?.providers ?? ManagedProviders.unmanagedProviders, |
| 116 | summary: { |
| 117 | organization: document.organization, |
| 118 | ...(document.supportUrl === undefined ? {} : { supportUrl: document.supportUrl }), |
| 119 | lockedSettings: settings.settings.lockedPaths, |
| 120 | ...(document.limits === undefined |
| 121 | ? {} |
| 122 | : { limits: summarizeLimits(document, lockedInstanceFields) }), |
| 123 | }, |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | // Copies only what clients may see; other limits can carry values that stay on the server. |
| 128 | function summarizeLimits( |
| 129 | document: ManagedPolicyDocument, |
| 130 | lockedInstanceFields: Readonly<Record<string, ReadonlyArray<string>>>, |
| 131 | ): LimitsSummary { |
| 132 | const { |
| 133 | maxRuntimeMode, |
| 134 | clients, |
| 135 | webhookTasks, |
| 136 | telemetry, |
| 137 | browserCookieImport, |
| 138 | previewTunnel, |
| 139 | providers, |
| 140 | updates, |
| 141 | } = document.limits ?? {}; |
| 142 | const connect = ManagedLimits.allowsConnect({ document }); |
| 143 | return { |
| 144 | ...(maxRuntimeMode === undefined ? {} : { maxRuntimeMode }), |
| 145 | ...(clients === undefined ? {} : { clients }), |
| 146 | ...(connect ? {} : { connect: false }), |
| 147 | ...(webhookTasks === undefined ? {} : { webhookTasks }), |
| 148 | ...(telemetry === undefined ? {} : { telemetry }), |
| 149 | ...(browserCookieImport === undefined ? {} : { browserCookieImport }), |
| 150 | ...(previewTunnel === undefined ? {} : { previewTunnel }), |
| 151 | ...(providers?.allowedDrivers === undefined && Object.keys(lockedInstanceFields).length === 0 |
| 152 | ? {} |
| 153 | : { |
| 154 | providers: { |
| 155 | ...(providers?.allowedDrivers === undefined |
| 156 | ? {} |
| 157 | : { allowedDrivers: providers.allowedDrivers }), |
| 158 | ...(Object.keys(lockedInstanceFields).length === 0 ? {} : { lockedInstanceFields }), |
| 159 | }, |
| 160 | }), |
| 161 | ...(updates === undefined |
| 162 | ? {} |
| 163 | : { |
| 164 | updates: { |
| 165 | mode: updates.mode, |
| 166 | ...(updates.allowNightly === undefined ? {} : { allowNightly: updates.allowNightly }), |
| 167 | }, |
| 168 | }), |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | /** Read and validate the policy at `path`; a missing file means no policy. */ |
| 173 | export const load = Effect.fn("ManagedPolicy.load")(function* (path: string) { |
| 174 | return yield* fromDocument(path, yield* readManagedPolicyFile(path)); |
| 175 | }); |
| 176 | |
| 177 | const make = Effect.gen(function* () { |
| 178 | // Source builds may read a policy from elsewhere so it can be tried without root. Release |
| 179 | // builds never do: any override a user can set is a way around the policy. |
| 180 | const devPath = |
| 181 | telemetryChannel === "dev" |
| 182 | ? yield* Config.option(Config.String("T3CODE_DEV_MANAGED_POLICY_PATH")).pipe( |
| 183 | Effect.orElseSucceed(() => Option.none<string>()), |
| 184 | ) |
| 185 | : Option.none<string>(); |
| 186 | const platform = yield* HostProcess.Platform; |
| 187 | const policy = yield* Option.match(devPath, { |
| 188 | onSome: load, |
| 189 | onNone: () => |
| 190 | readManagedPolicy( |
| 191 | platform, |
| 192 | ManagedProviders.knownDriverLabels().map(({ driver }) => driver), |
| 193 | ).pipe( |
| 194 | Effect.flatMap((found) => |
| 195 | fromDocument(found?.source ?? managedPolicyPath(platform), found?.document), |
| 196 | ), |
| 197 | ), |
| 198 | }); |
| 199 | const path = policy.path; |
| 200 | if (policy.summary !== undefined) { |
| 201 | yield* Effect.logInfo("Managed policy applies.", { |
| 202 | path, |
| 203 | organization: policy.summary.organization, |
| 204 | lockedSettings: policy.summary.lockedSettings, |
| 205 | }); |
| 206 | } |
| 207 | return policy; |
| 208 | }); |
| 209 | |
| 210 | // A single-executable server resolves packages on disk only through `require`. |
| 211 | const requireForFfi = NodeModule.createRequire(import.meta.url); |
| 212 | |
| 213 | export const layer = Layer.effect(ManagedPolicy, make).pipe( |
| 214 | Layer.provide( |
| 215 | layerWindowsRegistry(async () => requireForFfi("ffi-rs") as typeof import("ffi-rs")), |
| 216 | ), |
| 217 | ); |
| 218 | |
| 219 | /** A policy for tests; `undefined` means unmanaged. */ |
| 220 | export const layerTest = (document?: ManagedPolicyDocument) => |
| 221 | Layer.effect(ManagedPolicy, fromDocument("/test/policy.json", document)); |