GeezerVison is showing github.com — fix(server): a saved secret is kept only while its address stays the … · patroza/t3code@1536711
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/serverSettings.test.ts +135-2Lines changed: 135 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,7 @@ import { | 17 | 17 | ServerSettingsPatch, | 18 | 18 | UsageLimitSourceConfig, | 19 | 19 | UsageLimitSourceId, | | 20 | + type SettingsSecretValue, | 20 | 21 | type SimlockDeviceHostConfig, | 21 | 22 | } from "@t3tools/contracts"; | 22 | 23 | import { createModelSelection } from "@t3tools/shared/model"; | @@ -1662,11 +1663,11 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" | 1662 | 1663 | instanceId: opencode, | 1663 | 1664 | instance: { | 1664 | 1665 | ...client.providerInstances[opencode]!, | 1665 | | - config: { ...clientConfig, serverUrl: "http://127.0.0.1:4097" }, | | 1666 | + config: { ...clientConfig, binaryPath: "/opt/opencode" }, | 1666 | 1667 | }, | 1667 | 1668 | }); | 1668 | 1669 | assert.deepInclude(edited.providerInstances[opencode]?.config, { | 1669 | | - serverUrl: "http://127.0.0.1:4097", | | 1670 | + binaryPath: "/opt/opencode", | 1670 | 1671 | serverPassword: "legacy-password", | 1671 | 1672 | }); | 1672 | 1673 | }).pipe(Effect.provide(layerServerSettings())), | @@ -1929,6 +1930,138 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" | 1929 | 1930 | }).pipe(Effect.provide(layerServerSettingsWithSecrets())), | 1930 | 1931 | ); | 1931 | 1932 |
| | 1933 | + it.effect("a kept secret is dropped when the address it is sent to changes", () => | | 1934 | + Effect.gen(function* () { | | 1935 | + const service = yield* ServerSettingsModule.ServerSettingsService; | | 1936 | + const openCode = ProviderInstanceId.make("opencode"); | | 1937 | + const hub = UsageLimitSourceId.make("hub"); | | 1938 | + const router = ProviderInstanceId.make("openrouter"); | | 1939 | + const reference = { | | 1940 | + provider: SecretProviderId.make("personal"), | | 1941 | + reference: "op://Private/Hub/key", | | 1942 | + }; | | 1943 | + const patch = (input: { | | 1944 | + readonly serverUrl: string; | | 1945 | + readonly hubUrl: string; | | 1946 | + readonly baseUrl: string; | | 1947 | + readonly secret: SettingsSecretValue; | | 1948 | + }) => ({ | | 1949 | + providerInstances: { | | 1950 | + [openCode]: { | | 1951 | + driver: ProviderDriverKind.make("opencode"), | | 1952 | + config: { serverUrl: input.serverUrl, serverPassword: input.secret }, | | 1953 | + }, | | 1954 | + }, | | 1955 | + usageLimitSources: { | | 1956 | + [hub]: { | | 1957 | + kind: "cliproxy" as const, | | 1958 | + url: input.hubUrl, | | 1959 | + managementKey: input.secret, | | 1960 | + enabled: true, | | 1961 | + }, | | 1962 | + }, | | 1963 | + textGenerators: { | | 1964 | + [router]: { | | 1965 | + kind: "openaiCompatible" as const, | | 1966 | + baseUrl: input.baseUrl, | | 1967 | + apiKey: input.secret, | | 1968 | + enabled: true, | | 1969 | + }, | | 1970 | + }, | | 1971 | + }); | | 1972 | + const saved = { | | 1973 | + serverUrl: "http://127.0.0.1:4096", | | 1974 | + hubUrl: "https://hub.test", | | 1975 | + baseUrl: "https://router.test/v1", | | 1976 | + }; | | 1977 | + const elsewhere = { | | 1978 | + serverUrl: "http://elsewhere.test:4096", | | 1979 | + hubUrl: "https://elsewhere.test", | | 1980 | + baseUrl: "https://elsewhere.test/v1", | | 1981 | + }; | | 1982 | + const secrets = (settings: ServerSettings) => [ | | 1983 | + (settings.providerInstances[openCode]?.config as { serverPassword?: unknown } | undefined) | | 1984 | + ?.serverPassword, | | 1985 | + settings.usageLimitSources[hub]?.managementKey, | | 1986 | + settings.textGenerators[router]?.apiKey, | | 1987 | + ]; | | 1988 | + const kept = (secret: SettingsSecretValue) => [secret, secret, secret]; | | 1989 | + yield* decodeServerSettingsPatch({ | | 1990 | + secretProviders: { | | 1991 | + personal: { kind: "one_password", label: "Personal", config: { account: "my" } }, | | 1992 | + }, | | 1993 | + }).pipe(Effect.flatMap(service.updateSettings)); | | 1994 | + | | 1995 | + yield* service.updateSettings(patch({ ...saved, secret: "saved-fixture" })); | | 1996 | + assert.deepEqual( | | 1997 | + secrets(yield* service.updateSettings(patch({ ...saved, secret: SECRET_REDACTED }))), | | 1998 | + kept("saved-fixture"), | | 1999 | + ); | | 2000 | + assert.deepEqual( | | 2001 | + secrets(yield* service.updateSettings(patch({ ...elsewhere, secret: SECRET_REDACTED }))), | | 2002 | + kept(""), | | 2003 | + ); | | 2004 | + assert.deepEqual( | | 2005 | + secrets(yield* service.updateSettings(patch({ ...saved, secret: "typed-fixture" }))), | | 2006 | + kept("typed-fixture"), | | 2007 | + ); | | 2008 | + | | 2009 | + yield* service.updateSettings(patch({ ...saved, secret: reference })); | | 2010 | + assert.deepEqual( | | 2011 | + secrets(yield* service.updateSettings(patch({ ...elsewhere, secret: reference }))), | | 2012 | + kept(""), | | 2013 | + ); | | 2014 | + | | 2015 | + const sameAddress = [ | | 2016 | + { | | 2017 | + serverUrl: "http://127.0.0.1:4096/", | | 2018 | + hubUrl: "https://hub.test//", | | 2019 | + baseUrl: "https://router.test/v1/", | | 2020 | + }, | | 2021 | + { | | 2022 | + serverUrl: "HTTP://127.0.0.1:4096", | | 2023 | + hubUrl: "https://HUB.test:443", | | 2024 | + baseUrl: "https://Router.test/v1//", | | 2025 | + }, | | 2026 | + ]; | | 2027 | + const otherAddress = [ | | 2028 | + { | | 2029 | + serverUrl: "http://127.0.0.1:4097", | | 2030 | + hubUrl: "https://hub.test:8443", | | 2031 | + baseUrl: "https://router.test:8443/v1", | | 2032 | + }, | | 2033 | + { | | 2034 | + serverUrl: "https://127.0.0.1:4096", | | 2035 | + hubUrl: "http://hub.test", | | 2036 | + baseUrl: "http://router.test/v1", | | 2037 | + }, | | 2038 | + { | | 2039 | + serverUrl: "http://127.0.0.1:4096/api", | | 2040 | + hubUrl: "https://hub.test/api/", | | 2041 | + baseUrl: "https://router.test/V1", | | 2042 | + }, | | 2043 | + { | | 2044 | + serverUrl: "http://127.0.0.1:4096/?team=a", | | 2045 | + hubUrl: "https://hub.test?team=a", | | 2046 | + baseUrl: "https://router.test/v1/v1/", | | 2047 | + }, | | 2048 | + ]; | | 2049 | + for (const [addresses, expected] of [ | | 2050 | + [sameAddress, kept("saved-fixture")], | | 2051 | + [otherAddress, kept("")], | | 2052 | + ] as const) { | | 2053 | + for (const address of addresses) { | | 2054 | + yield* service.updateSettings(patch({ ...saved, secret: "saved-fixture" })); | | 2055 | + assert.deepEqual( | | 2056 | + secrets(yield* service.updateSettings(patch({ ...address, secret: SECRET_REDACTED }))), | | 2057 | + expected, | | 2058 | + JSON.stringify(address), | | 2059 | + ); | | 2060 | + } | | 2061 | + } | | 2062 | + }).pipe(Effect.provide(layerServerSettingsWithSecrets())), | | 2063 | + ); | | 2064 | + | 1932 | 2065 | it.effect("removing a secret provider unsets every field that read from it", () => | 1933 | 2066 | Effect.gen(function* () { | 1934 | 2067 | const service = yield* ServerSettingsModule.ServerSettingsService; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/serverSettings.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,7 +67,7 @@ import { | 67 | 67 | mapSettingsSecrets, | 68 | 68 | mapSettingsSecretValues, | 69 | 69 | withoutRemovedSecretProviderReferences, | 70 | | - withoutRedirectedSimlockTokens, | | 70 | + withoutRedirectedSecrets, | 71 | 71 | SECRET_REDACTED, | 72 | 72 | type SettingsSecret, | 73 | 73 | } from "./settingsSecrets.ts"; | @@ -1241,7 +1241,7 @@ const make = Effect.gen(function* () { | 1241 | 1241 | } | 1242 | 1242 | // An older client echoes a reference as the redacted marker; restore it before vetting so | 1243 | 1243 | // a removed provider's reference is cleared below rather than restored by persistence. | 1244 | | - const updated = withoutRedirectedSimlockTokens( | | 1244 | + const updated = withoutRedirectedSecrets( | 1245 | 1245 | current, | 1246 | 1246 | withEchoedSecretReferences(current, yield* update(current)), | 1247 | 1247 | ); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/settingsSecrets.ts +74-19Lines changed: 74 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,14 +5,17 @@ import { | 5 | 5 | CodexSettings, | 6 | 6 | EasDeviceHostConfig, | 7 | 7 | type ProviderInstanceConfig, | | 8 | + type ProviderInstanceId, | 8 | 9 | type ServerSettings, | 9 | 10 | type SettingsSecretValue, | 10 | 11 | SimlockDeviceHostConfig, | 11 | 12 | TextGeneratorConfig, | 12 | 13 | UsageLimitSourceConfig, | | 14 | + type UsageLimitSourceId, | 13 | 15 | isSecretReference, | 14 | 16 | secretSettingsFields, | 15 | 17 | } from "@t3tools/contracts"; | | 18 | +import * as Predicate from "effect/Predicate"; | 16 | 19 | import { acpRegistryClient } from "@t3tools/provider-acp-registry/client"; | 17 | 20 | import { cursorClient } from "@t3tools/provider-cursor/client"; | 18 | 21 | import { grokClient } from "@t3tools/provider-grok/client"; | @@ -293,32 +296,84 @@ export function withoutRemovedSecretProviderReferences( | 293 | 296 | }; | 294 | 297 | } | 295 | 298 |
| 296 | | -const gatewayOrigin = (url: string) => (URL.canParse(url) ? new URL(url).origin : url); | | 299 | +/** A URL in the form two spellings of one address share: `URL` normalization, no trailing slash on the path. */ | | 300 | +const destination = (url: unknown) => { | | 301 | + if (typeof url !== "string" || !URL.canParse(url)) return url; | | 302 | + const parsed = new URL(url); | | 303 | + parsed.pathname = parsed.pathname.replace(/\/+$/, ""); | | 304 | + return parsed.href; | | 305 | +}; | | 306 | + | | 307 | +/** Per provider driver, the config field holding a secret and the one naming where it is sent. */ | | 308 | +const providerSecretDestinations = new Map<string, readonly [secret: string, url: string]>([ | | 309 | + [openCodeClient.driverKind, ["serverPassword", "serverUrl"]], | | 310 | +]); | 297 | 311 |
| 298 | 312 | /** | 299 | | - * `next` with a Simlock host's kept token cleared when its gateway moved to another origin: a | 300 | | - * saved token goes only to the gateway it was saved for. A newly entered token stays. | | 313 | + * `next` with a kept secret cleared when the address it is sent to changed: a saved secret goes | | 314 | + * only to the address it was saved for. A newly entered secret stays. | 301 | 315 | */ | 302 | | -export function withoutRedirectedSimlockTokens( | | 316 | +export function withoutRedirectedSecrets( | 303 | 317 | current: ServerSettings, | 304 | 318 | next: ServerSettings, | 305 | 319 | ): ServerSettings { | 306 | | - const saved = new Map(current.simlockDeviceHosts.map((host) => [host.id, host])); | 307 | | - const kept = (token: SettingsSecretValue, savedToken: SettingsSecretValue) => | 308 | | - token === SECRET_REDACTED || | 309 | | - (isSecretReference(token) && | 310 | | - isSecretReference(savedToken) && | 311 | | - token.provider === savedToken.provider && | 312 | | - token.reference === savedToken.reference); | | 320 | + const kept = (secret: unknown, saved: unknown) => | | 321 | + secret === SECRET_REDACTED || | | 322 | + (isSecretReference(secret) && | | 323 | + isSecretReference(saved) && | | 324 | + secret.provider === saved.provider && | | 325 | + secret.reference === saved.reference); | | 326 | + const rebound = <T extends object>( | | 327 | + entry: T, | | 328 | + previous: object | null | undefined, | | 329 | + secret: string, | | 330 | + url: string, | | 331 | + ): T => { | | 332 | + if (previous === null || previous === undefined) return entry; | | 333 | + const fields = entry as Readonly<Record<string, unknown>>; | | 334 | + const saved = previous as Readonly<Record<string, unknown>>; | | 335 | + return destination(saved[url]) !== destination(fields[url]) && | | 336 | + kept(fields[secret], saved[secret]) | | 337 | + ? { ...entry, [secret]: "" } | | 338 | + : entry; | | 339 | + }; | | 340 | + const simlockHosts = new Map(current.simlockDeviceHosts.map((host) => [host.id, host])); | 313 | 341 | return { | 314 | 342 | ...next, | 315 | | - simlockDeviceHosts: next.simlockDeviceHosts.map((host) => { | 316 | | - const previous = saved.get(host.id); | 317 | | - return previous && | 318 | | - gatewayOrigin(previous.gatewayUrl) !== gatewayOrigin(host.gatewayUrl) && | 319 | | - kept(host.agentToken, previous.agentToken) | 320 | | - ? { ...host, agentToken: "" } | 321 | | - : host; | 322 | | - }), | | 343 | + providerInstances: Object.fromEntries( | | 344 | + Object.entries(next.providerInstances).map(([instanceId, instance]) => { | | 345 | + const fields = providerSecretDestinations.get(instance.driver); | | 346 | + const previous = current.providerInstances[instanceId as ProviderInstanceId]; | | 347 | + return [ | | 348 | + instanceId, | | 349 | + fields !== undefined && | | 350 | + previous?.driver === instance.driver && | | 351 | + Predicate.isObject(instance.config) && | | 352 | + Predicate.isObject(previous.config) | | 353 | + ? { ...instance, config: rebound(instance.config, previous.config, ...fields) } | | 354 | + : instance, | | 355 | + ]; | | 356 | + }), | | 357 | + ) as ServerSettings["providerInstances"], | | 358 | + usageLimitSources: Object.fromEntries( | | 359 | + Object.entries(next.usageLimitSources).map(([id, source]) => [ | | 360 | + id, | | 361 | + rebound( | | 362 | + source, | | 363 | + current.usageLimitSources[id as UsageLimitSourceId], | | 364 | + "managementKey", | | 365 | + "url", | | 366 | + ), | | 367 | + ]), | | 368 | + ) as ServerSettings["usageLimitSources"], | | 369 | + textGenerators: Object.fromEntries( | | 370 | + Object.entries(next.textGenerators).map(([id, generator]) => [ | | 371 | + id, | | 372 | + rebound(generator, current.textGenerators[id as ProviderInstanceId], "apiKey", "baseUrl"), | | 373 | + ]), | | 374 | + ) as ServerSettings["textGenerators"], | | 375 | + simlockDeviceHosts: next.simlockDeviceHosts.map((host) => | | 376 | + rebound(host, simlockHosts.get(host.id), "agentToken", "gatewayUrl"), | | 377 | + ), | 323 | 378 | }; | 324 | 379 | } |
|