GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): a saved secret is kept only while its address stays the … · patroza/t3code@1536711

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • serverSettings.test.ts
    • serverSettings.ts
    • settingsSecrets.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/serverSettings.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/serverSettings.test.ts
+135-2Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ServerSettingsPatch,
1818
UsageLimitSourceConfig,
1919
UsageLimitSourceId,
20+
type SettingsSecretValue,
2021
type SimlockDeviceHostConfig,
2122
} from "@t3tools/contracts";
2223
import { createModelSelection } from "@t3tools/shared/model";
@@ -1662,11 +1663,11 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
16621663
instanceId: opencode,
16631664
instance: {
16641665
...client.providerInstances[opencode]!,
1665-
config: { ...clientConfig, serverUrl: "http://127.0.0.1:4097" },
1666+
config: { ...clientConfig, binaryPath: "/opt/opencode" },
16661667
},
16671668
});
16681669
assert.deepInclude(edited.providerInstances[opencode]?.config, {
1669-
serverUrl: "http://127.0.0.1:4097",
1670+
binaryPath: "/opt/opencode",
16701671
serverPassword: "legacy-password",
16711672
});
16721673
}).pipe(Effect.provide(layerServerSettings())),
@@ -1929,6 +1930,138 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
19291930
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
19301931
);
19311932

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+
19322065
it.effect("removing a secret provider unsets every field that read from it", () =>
19332066
Effect.gen(function* () {
19342067
const service = yield* ServerSettingsModule.ServerSettingsService;
Collapse file

‎apps/server/src/serverSettings.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/serverSettings.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import {
6767
mapSettingsSecrets,
6868
mapSettingsSecretValues,
6969
withoutRemovedSecretProviderReferences,
70-
withoutRedirectedSimlockTokens,
70+
withoutRedirectedSecrets,
7171
SECRET_REDACTED,
7272
type SettingsSecret,
7373
} from "./settingsSecrets.ts";
@@ -1241,7 +1241,7 @@ const make = Effect.gen(function* () {
12411241
}
12421242
// An older client echoes a reference as the redacted marker; restore it before vetting so
12431243
// a removed provider's reference is cleared below rather than restored by persistence.
1244-
const updated = withoutRedirectedSimlockTokens(
1244+
const updated = withoutRedirectedSecrets(
12451245
current,
12461246
withEchoedSecretReferences(current, yield* update(current)),
12471247
);
Collapse file

‎apps/server/src/settingsSecrets.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/settingsSecrets.ts
+74-19Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import {
55
CodexSettings,
66
EasDeviceHostConfig,
77
type ProviderInstanceConfig,
8+
type ProviderInstanceId,
89
type ServerSettings,
910
type SettingsSecretValue,
1011
SimlockDeviceHostConfig,
1112
TextGeneratorConfig,
1213
UsageLimitSourceConfig,
14+
type UsageLimitSourceId,
1315
isSecretReference,
1416
secretSettingsFields,
1517
} from "@t3tools/contracts";
18+
import * as Predicate from "effect/Predicate";
1619
import { acpRegistryClient } from "@t3tools/provider-acp-registry/client";
1720
import { cursorClient } from "@t3tools/provider-cursor/client";
1821
import { grokClient } from "@t3tools/provider-grok/client";
@@ -293,32 +296,84 @@ export function withoutRemovedSecretProviderReferences(
293296
};
294297
}
295298

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+
]);
297311

298312
/**
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.
301315
*/
302-
export function withoutRedirectedSimlockTokens(
316+
export function withoutRedirectedSecrets(
303317
current: ServerSettings,
304318
next: ServerSettings,
305319
): 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]));
313341
return {
314342
...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+
),
323378
};
324379
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.