Skip to content

Commit 66e4368

Browse files
refactor(settings): secret provider kinds own their config schema
Contracts held 1Password's config schema and a table of known kinds, so a kind loaded later could not add itself. The 1Password package now owns its account and config schema, and the server checks secret provider writes and redacts unknown kinds against the definitions it has loaded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8361ce3 commit 66e4368

11 files changed

Lines changed: 99 additions & 64 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
resolveProviderInstanceEnabled,
1010
SecretProviderId,
1111
ServerSettings,
12+
ServerSettingsPatch,
1213
UsageLimitSourceId,
1314
} from "@t3tools/contracts";
1415
import { createModelSelection } from "@t3tools/shared/model";
@@ -34,6 +35,7 @@ import { resolveProviderInstanceTerminalEnvironment } from "./terminal/Manager.t
3435

3536
const decodeServerSettings = Schema.decodeUnknownEffect(ServerSettings);
3637
const decodeServerSettingsJson = Schema.decodeUnknownEffect(Schema.fromJsonString(ServerSettings));
38+
const decodeServerSettingsPatch = Schema.decodeUnknownEffect(ServerSettingsPatch);
3739

3840
const layerServerSettings = () =>
3941
ServerSettingsModule.layer.pipe(
@@ -279,6 +281,25 @@ it.layer(NodeServices.layer)("server settings", (it) => {
279281
}),
280282
);
281283

284+
it.effect("stores no secret provider of an unloaded kind or with a config its kind rejects", () =>
285+
Effect.gen(function* () {
286+
const service = yield* ServerSettingsModule.ServerSettingsService;
287+
const write = (entry: unknown) =>
288+
decodeServerSettingsPatch({ secretProviders: { personal: entry } }).pipe(
289+
Effect.flatMap(service.updateSettings),
290+
Effect.flip,
291+
);
292+
for (const entry of [
293+
{ kind: "one_password", label: "Personal", config: { account: "--help" } },
294+
{ kind: "one_password", label: "Personal" },
295+
{ kind: "openbao", label: "Vault", config: { address: "https://x" } },
296+
]) {
297+
assert.deepInclude(yield* write(entry), { operation: "write-secret-provider" });
298+
}
299+
assert.deepStrictEqual((yield* service.getSettings).secretProviders, {});
300+
}).pipe(Effect.provide(layerServerSettings())),
301+
);
302+
282303
it.effect("saves through a symlinked settings file without replacing the link", () =>
283304
Effect.gen(function* () {
284305
const config = yield* ServerConfig.ServerConfig;

‎apps/server/src/serverSettings.ts‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
type ProviderInstanceConfig,
99
type ProviderInstanceMutation,
1010
defaultInstanceIdForDriver,
11-
isKnownSecretProviderKind,
1211
isProviderDriverKind,
1312
ProviderDriverKind,
1413
ProviderInstanceId,
@@ -53,6 +52,7 @@ import {
5352
isModelSelectionProviderEnabled,
5453
} from "@t3tools/shared/serverSettings";
5554
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
55+
import { secretProviderDefinitions } from "./secrets/secretProviderDefinitions.ts";
5656
import {
5757
readSecretFieldValues,
5858
sourceControlHostSecretName,
@@ -144,7 +144,7 @@ function withoutUnknownSecretProviderConfigs(
144144
): ServerSettings["secretProviders"] {
145145
return Object.fromEntries(
146146
Object.entries(settings.secretProviders).map(([id, entry]) => {
147-
if (isKnownSecretProviderKind(entry.kind)) return [id, entry];
147+
if (secretProviderConfigGuards.has(entry.kind)) return [id, entry];
148148
const { config: _config, ...envelope } = entry;
149149
return [id, envelope];
150150
}),
@@ -164,6 +164,40 @@ export function applyProviderInstanceMutation(
164164
return { ...settings, providerInstances };
165165
}
166166

167+
const secretProviderConfigGuards = new Map(
168+
secretProviderDefinitions.definitions.map((definition) => [
169+
definition.kind as string,
170+
Schema.is(definition.configSchema),
171+
]),
172+
);
173+
174+
// Rejects a kind this server has not loaded rather than storing a config it cannot check.
175+
function ensureSecretProviderWritesAllowed(
176+
patch: ServerSettingsPatch,
177+
settingsPath: string,
178+
): Effect.Effect<void, ServerSettingsError> {
179+
for (const [id, entry] of Object.entries(patch.secretProviders ?? {})) {
180+
if (entry === null) continue;
181+
const isConfig = secretProviderConfigGuards.get(entry.kind);
182+
const problem =
183+
isConfig === undefined
184+
? `Unsupported secret provider kind: ${entry.kind}.`
185+
: isConfig(entry.config)
186+
? undefined
187+
: `Invalid ${entry.kind} config for secret provider ${id}.`;
188+
if (problem !== undefined) {
189+
return Effect.fail(
190+
new ServerSettingsError({
191+
settingsPath,
192+
operation: "write-secret-provider",
193+
cause: new Error(problem),
194+
}),
195+
);
196+
}
197+
}
198+
return Effect.void;
199+
}
200+
167201
function ensureProviderInstanceMutationAllowed(
168202
settings: ServerSettings,
169203
mutation: ProviderInstanceMutation,
@@ -1185,11 +1219,14 @@ const make = Effect.gen(function* () {
11851219
getSettings: getMaterializedSettings,
11861220
updateSettings: (patch) =>
11871221
updateAndPersistSettings((current) =>
1188-
Effect.succeed(applyServerSettingsPatch(current, patch)),
1222+
ensureSecretProviderWritesAllowed(patch, settingsPath).pipe(
1223+
Effect.as(applyServerSettingsPatch(current, patch)),
1224+
),
11891225
),
11901226
updateProviderInstance: (mutation, patch = {}) =>
11911227
updateAndPersistSettings((current) =>
11921228
Effect.gen(function* () {
1229+
yield* ensureSecretProviderWritesAllowed(patch, settingsPath);
11931230
yield* ensureProviderInstanceMutationAllowed(current, mutation, settingsPath);
11941231
const patched = applyServerSettingsPatch(current, patch);
11951232
return applyProviderInstanceMutation(patched, mutation);

‎apps/web/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@t3tools/contracts": "workspace:*",
3636
"@t3tools/provider-core": "workspace:*",
3737
"@t3tools/secret-provider-definitions": "workspace:*",
38+
"@t3tools/secret-provider-onepassword": "workspace:*",
3839
"@t3tools/shared": "workspace:*",
3940
"@tanstack/react-pacer": "^0.19.4",
4041
"@tanstack/react-router": "^1.160.2",

‎apps/web/src/components/settings/SecretProviderDialog.logic.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
2-
OnePasswordSecretProviderConfig,
32
SecretProviderId,
43
SecretProviderKind,
54
SecretProviderLabel,
6-
type OnePasswordAccount,
75
type SecretProviderConfig,
86
} from "@t3tools/contracts";
7+
import {
8+
OnePasswordSecretProviderConfig,
9+
type OnePasswordAccount,
10+
} from "@t3tools/secret-provider-onepassword/shared/definition";
911
import * as Option from "effect/Option";
1012
import * as Result from "effect/Result";
1113
import * as Schema from "effect/Schema";

‎packages/contracts/src/settings.test.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,6 @@ describe("ServerSettings secret providers", () => {
166166
).toThrow();
167167
});
168168

169-
it("rejects writes of an unknown kind or an account op could misread", () => {
170-
const write = (entry: unknown) => () =>
171-
decodeServerSettingsPatch({ secretProviders: { personal: entry } });
172-
for (const account of ["", "my account", "--help"]) {
173-
expect(write(onePassword(account))).toThrow();
174-
}
175-
expect(write({ kind: "one_password", label: "Personal" })).toThrow();
176-
expect(write({ kind: "openbao", label: "Vault", config: { address: "https://x" } })).toThrow();
177-
});
178-
179169
it("stores a provider environment secret reference in a layout older builds still decode", () => {
180170
const providerInstances = (secret: unknown) => ({
181171
codex: {

‎packages/contracts/src/settings.ts‎

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -863,29 +863,8 @@ export type UsageLimitSourceConfig = typeof UsageLimitSourceConfig.Type;
863863
export const SecretProviderKind = SecretProviderSlug.pipe(Schema.brand("SecretProviderKind"));
864864
export type SecretProviderKind = typeof SecretProviderKind.Type;
865865

866-
const ONE_PASSWORD_ACCOUNT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
867-
868-
/**
869-
* Any value `op --account` accepts: shorthand, sign-in address, account ID, or user ID.
870-
* It is passed to the CLI as an argument, so it can never start with `-`.
871-
*/
872-
export const OnePasswordAccount = Schema.String.check(
873-
Schema.isMaxLength(253),
874-
Schema.makeFilter((value: string) =>
875-
ONE_PASSWORD_ACCOUNT_PATTERN.test(value)
876-
? true
877-
: "1Password account must be a shorthand, sign-in address, or ID with no spaces.",
878-
),
879-
).pipe(Schema.brand("OnePasswordAccount"));
880-
export type OnePasswordAccount = typeof OnePasswordAccount.Type;
881-
882866
export const SecretProviderLabel = TrimmedNonEmptyString.check(Schema.isMaxLength(64));
883867

884-
export const OnePasswordSecretProviderConfig = Schema.Struct({
885-
account: OnePasswordAccount,
886-
});
887-
export type OnePasswordSecretProviderConfig = typeof OnePasswordSecretProviderConfig.Type;
888-
889868
/** Kinds decode their own config; unknown-kind envelopes round-trip verbatim across versions. */
890869
export const SecretProviderConfig = Schema.Struct({
891870
kind: SecretProviderKind,
@@ -894,31 +873,12 @@ export const SecretProviderConfig = Schema.Struct({
894873
});
895874
export type SecretProviderConfig = typeof SecretProviderConfig.Type;
896875

897-
/** Kinds this build can resolve. Their `config` holds no credentials, so clients may read it. */
898-
const SECRET_PROVIDER_KIND_CONFIG_GUARDS: Readonly<Record<string, (config: unknown) => boolean>> = {
899-
one_password: Schema.is(OnePasswordSecretProviderConfig),
900-
};
901-
902-
export function isKnownSecretProviderKind(kind: string): boolean {
903-
return Object.hasOwn(SECRET_PROVIDER_KIND_CONFIG_GUARDS, kind);
904-
}
905-
906-
// Limits that may change later are enforced on writes only, never on the stored envelope.
907-
// Writes must be a kind this build knows with a config it accepts, so an older server
908-
// rejects a newer kind instead of storing what it cannot check.
876+
// Limits that may change later are enforced on writes only, never on the stored envelope. The
877+
// server also checks the kind and its config against the secret providers it has loaded.
909878
const SecretProviderConfigWrite = Schema.Struct({
910879
...SecretProviderConfig.fields,
911880
label: SecretProviderLabel,
912-
}).check(
913-
Schema.makeFilter((entry) => {
914-
if (!isKnownSecretProviderKind(entry.kind)) {
915-
return `Unsupported secret provider kind: ${entry.kind}.`;
916-
}
917-
return SECRET_PROVIDER_KIND_CONFIG_GUARDS[entry.kind]!(entry.config)
918-
? true
919-
: `Invalid ${entry.kind} secret provider config.`;
920-
}),
921-
);
881+
});
922882

923883
export const SourceControlHostSettings = Schema.Record(Schema.String, Schema.Unknown);
924884
export type SourceControlHostSettings = typeof SourceControlHostSettings.Type;
@@ -1517,6 +1477,7 @@ export const ServerSettingsOperation = Schema.Literals([
15171477
"remove-secret",
15181478
"remove-stale-secret",
15191479
"write-secret",
1480+
"write-secret-provider",
15201481
"write-file",
15211482
"prepare-directory",
15221483
]);

‎packages/secret-provider-onepassword/src/server/driver.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
33
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
44
import { assert, describe, it } from "@effect/vitest";
5-
import { OnePasswordAccount } from "@t3tools/contracts";
65
import * as Deferred from "effect/Deferred";
76
import * as Effect from "effect/Effect";
87
import * as Fiber from "effect/Fiber";
@@ -13,6 +12,7 @@ import * as TestClock from "effect/testing/TestClock";
1312
import { ChildProcessSpawner } from "effect/process";
1413
import * as NodeFS from "node:fs";
1514

15+
import { OnePasswordAccount } from "../shared/definition.ts";
1616
import { driver } from "./driver.ts";
1717

1818
const encoder = new TextEncoder();

‎packages/secret-provider-onepassword/src/server/driver.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { OnePasswordAccount } from "@t3tools/contracts";
21
import { spawnAndCollect } from "@t3tools/provider-core/server/snapshotProbe";
32
import { defineSecretProviderDriver } from "@t3tools/secret-provider-core/server/driver";
43
import { resolveSpawnCommand } from "@t3tools/shared/shell";
@@ -9,7 +8,7 @@ import * as FileSystem from "effect/FileSystem";
98
import * as Option from "effect/Option";
109
import { ChildProcess } from "effect/process";
1110

12-
import { definition } from "../shared/definition.ts";
11+
import { definition, type OnePasswordAccount } from "../shared/definition.ts";
1312

1413
const ONE_PASSWORD_BINARY = "op";
1514
// Bounds a read long enough to reach for a fingerprint reader, short enough not to wedge a rebuild.

‎packages/secret-provider-onepassword/src/shared/definition.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1-
import { OnePasswordSecretProviderConfig, SecretProviderKind } from "@t3tools/contracts";
1+
import { SecretProviderKind } from "@t3tools/contracts";
22
import { defineSecretProvider } from "@t3tools/secret-provider-core/shared/definition";
3+
import * as Schema from "effect/Schema";
4+
5+
const ACCOUNT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
6+
7+
/**
8+
* Any value `op --account` accepts: shorthand, sign-in address, account ID, or user ID.
9+
* It is passed to the CLI as an argument, so it can never start with `-`.
10+
*/
11+
export const OnePasswordAccount = Schema.String.check(
12+
Schema.isMaxLength(253),
13+
Schema.makeFilter((value: string) =>
14+
ACCOUNT_PATTERN.test(value)
15+
? true
16+
: "1Password account must be a shorthand, sign-in address, or ID with no spaces.",
17+
),
18+
).pipe(Schema.brand("OnePasswordAccount"));
19+
export type OnePasswordAccount = typeof OnePasswordAccount.Type;
20+
21+
export const OnePasswordSecretProviderConfig = Schema.Struct({
22+
account: OnePasswordAccount,
23+
});
24+
export type OnePasswordSecretProviderConfig = typeof OnePasswordSecretProviderConfig.Type;
325

426
// 1Password secret reference syntax: names outside this charset must be written as item ids.
527
// It also keeps `{{`, `}}`, and line breaks out of the `op inject` template the server builds.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
DEFAULT_SERVER_SETTINGS,
3-
OnePasswordAccount,
43
ProjectId,
54
ProviderDriverKind,
65
ProviderInstanceId,
@@ -590,7 +589,7 @@ describe("serverSettings helpers", () => {
590589
const onePassword = (label: string, account: string) => ({
591590
kind: SecretProviderKind.make("one_password"),
592591
label,
593-
config: { account: OnePasswordAccount.make(account) },
592+
config: { account },
594593
});
595594
const current = {
596595
...DEFAULT_SERVER_SETTINGS,

0 commit comments

Comments
 (0)