mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-14 10:00:38 +09:00
## Thinking Path > - Paperclip is a control plane, so optional execution providers should sit at the plugin edge instead of hardcoding provider-specific behavior into core shared/server/ui layers. > - Sandbox environments are already first-class, and the fake provider proves the built-in path; the remaining gap was that real providers still leaked provider-specific config and runtime assumptions into core. > - That coupling showed up in config normalization, secret persistence, capabilities reporting, lease reconstruction, and the board UI form fields. > - As long as core knew about those provider-shaped details, shipping a provider as a pure third-party plugin meant every new provider would still require host changes. > - This pull request generalizes the sandbox provider seam around schema-driven plugin metadata and generic secret-ref handling. > - The runtime and UI now consume provider metadata generically, so core only special-cases the built-in fake provider while third-party providers can live entirely in plugins. ## What Changed - Added generic sandbox-provider capability metadata so plugin-backed providers can expose `configSchema` through shared environment support and the environments capabilities API. - Reworked sandbox config normalization/persistence/runtime resolution to handle schema-declared secret-ref fields generically, storing them as Paperclip secrets and resolving them for probe/execute/release flows. - Generalized plugin sandbox runtime handling so provider validation, reusable-lease matching, lease reconstruction, and plugin worker calls all operate on provider-agnostic config instead of provider-shaped branches. - Replaced hardcoded sandbox provider form fields in Company Settings with schema-driven rendering and blocked agent environment selection from the built-in fake provider. - Added regression coverage for the generic seam across shared support helpers plus environment config, probe, routes, runtime, and sandbox-provider runtime tests. ## Verification - `pnpm vitest --run packages/shared/src/environment-support.test.ts server/src/__tests__/environment-config.test.ts server/src/__tests__/environment-probe.test.ts server/src/__tests__/environment-routes.test.ts server/src/__tests__/environment-runtime.test.ts server/src/__tests__/sandbox-provider-runtime.test.ts` - `pnpm -r typecheck` ## Risks - Plugin sandbox providers now depend more heavily on accurate `configSchema` declarations; incorrect schemas can misclassify secret-bearing fields or omit required config. - Reusable lease matching is now metadata-driven for plugin-backed providers, so providers that fail to persist stable metadata may reprovision instead of resuming an existing lease. - The UI form is now fully schema-driven for plugin-backed sandbox providers; provider manifests without good defaults or descriptions may produce a rougher operator experience. ## Model Used - OpenAI Codex via `codex_local` - Model ID: `gpt-5.4` - Reasoning effort: `high` - Context window observed in runtime session metadata: `258400` tokens - Capabilities used: terminal tool execution, git, and local code/test inspection ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge
312 lines
11 KiB
TypeScript
312 lines
11 KiB
TypeScript
/**
|
|
* Plugin secrets host-side handler — resolves secret references through the
|
|
* Paperclip secret provider system.
|
|
*
|
|
* When a plugin worker calls `ctx.secrets.resolve(secretRef)`, the JSON-RPC
|
|
* request arrives at the host with `{ secretRef }`. This module provides the
|
|
* concrete `HostServices.secrets` adapter that:
|
|
*
|
|
* 1. Parses the `secretRef` string to identify the secret.
|
|
* 2. Looks up the secret record and its latest version in the database.
|
|
* 3. Delegates to the configured `SecretProviderModule` to decrypt /
|
|
* resolve the raw value.
|
|
* 4. Returns the resolved plaintext value to the worker.
|
|
*
|
|
* ## Secret Reference Format
|
|
*
|
|
* A `secretRef` is a **secret UUID** — the primary key (`id`) of a row in
|
|
* the `company_secrets` table. Operators place these UUIDs into plugin
|
|
* config values; plugin workers resolve them at execution time via
|
|
* `ctx.secrets.resolve(secretId)`.
|
|
*
|
|
* ## Security Invariants
|
|
*
|
|
* - Resolved values are **never** logged, persisted, or included in error
|
|
* messages (per PLUGIN_SPEC.md §22).
|
|
* - The handler is capability-gated: only plugins with `secrets.read-ref`
|
|
* declared in their manifest may call it (enforced by `host-client-factory`).
|
|
* - The host handler itself does not cache resolved values. Each call goes
|
|
* through the secret provider to honour rotation.
|
|
*
|
|
* @see PLUGIN_SPEC.md §22 — Secrets
|
|
* @see host-client-factory.ts — capability gating
|
|
* @see services/secrets.ts — secretService used by agent env bindings
|
|
*/
|
|
|
|
import { eq, and, desc } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { companySecrets, companySecretVersions, pluginConfig } from "@paperclipai/db";
|
|
import type { SecretProvider } from "@paperclipai/shared";
|
|
import { getSecretProvider } from "../secrets/provider-registry.js";
|
|
import { pluginRegistryService } from "./plugin-registry.js";
|
|
import {
|
|
collectSecretRefPaths,
|
|
isUuidSecretRef,
|
|
readConfigValueAtPath,
|
|
} from "./json-schema-secret-refs.js";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Error helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Create a sanitised error that never leaks secret material.
|
|
* Only the ref identifier is included; never the resolved value.
|
|
*/
|
|
function secretNotFound(secretRef: string): Error {
|
|
const err = new Error(`Secret not found: ${secretRef}`);
|
|
err.name = "SecretNotFoundError";
|
|
return err;
|
|
}
|
|
|
|
function secretVersionNotFound(secretRef: string): Error {
|
|
const err = new Error(`No version found for secret: ${secretRef}`);
|
|
err.name = "SecretVersionNotFoundError";
|
|
return err;
|
|
}
|
|
|
|
function invalidSecretRef(secretRef: string): Error {
|
|
const err = new Error(`Invalid secret reference: ${secretRef}`);
|
|
err.name = "InvalidSecretRefError";
|
|
return err;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Validation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Extract secret reference UUIDs from a plugin's configJson, scoped to only
|
|
* the fields annotated with `format: "secret-ref"` in the schema.
|
|
*
|
|
* When no schema is provided, falls back to collecting all UUID-shaped strings
|
|
* (backwards-compatible for plugins without a declared instanceConfigSchema).
|
|
*/
|
|
export function extractSecretRefsFromConfig(
|
|
configJson: unknown,
|
|
schema?: Record<string, unknown> | null,
|
|
): Set<string> {
|
|
const refs = new Set<string>();
|
|
if (configJson == null || typeof configJson !== "object") return refs;
|
|
|
|
const secretPaths = collectSecretRefPaths(schema);
|
|
|
|
// If schema declares secret-ref paths, extract only those values.
|
|
if (secretPaths.size > 0) {
|
|
for (const dotPath of secretPaths) {
|
|
const current = readConfigValueAtPath(configJson as Record<string, unknown>, dotPath);
|
|
if (typeof current === "string" && isUuidSecretRef(current)) {
|
|
refs.add(current);
|
|
}
|
|
}
|
|
return refs;
|
|
}
|
|
|
|
// Fallback: no schema or no secret-ref annotations — collect all UUIDs.
|
|
// This preserves backwards compatibility for plugins that omit
|
|
// instanceConfigSchema.
|
|
function walkAll(value: unknown): void {
|
|
if (typeof value === "string") {
|
|
if (isUuidSecretRef(value)) refs.add(value);
|
|
} else if (Array.isArray(value)) {
|
|
for (const item of value) walkAll(item);
|
|
} else if (value !== null && typeof value === "object") {
|
|
for (const v of Object.values(value as Record<string, unknown>)) walkAll(v);
|
|
}
|
|
}
|
|
|
|
walkAll(configJson);
|
|
return refs;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Handler factory
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Input shape for the `secrets.resolve` handler.
|
|
*
|
|
* Matches `WorkerToHostMethods["secrets.resolve"][0]` from `protocol.ts`.
|
|
*/
|
|
export interface PluginSecretsResolveParams {
|
|
/** The secret reference string (a secret UUID). */
|
|
secretRef: string;
|
|
}
|
|
|
|
/**
|
|
* Options for creating the plugin secrets handler.
|
|
*/
|
|
export interface PluginSecretsHandlerOptions {
|
|
/** Database connection. */
|
|
db: Db;
|
|
/**
|
|
* The plugin ID using this handler.
|
|
* Used for logging context only; never included in error payloads
|
|
* that reach the plugin worker.
|
|
*/
|
|
pluginId: string;
|
|
}
|
|
|
|
/**
|
|
* The `HostServices.secrets` adapter for the plugin host-client factory.
|
|
*/
|
|
export interface PluginSecretsService {
|
|
/**
|
|
* Resolve a secret reference to its current plaintext value.
|
|
*
|
|
* @param params - Contains the `secretRef` (UUID of the secret)
|
|
* @returns The resolved secret value
|
|
* @throws {Error} If the secret is not found, has no versions, or
|
|
* the provider fails to resolve
|
|
*/
|
|
resolve(params: PluginSecretsResolveParams): Promise<string>;
|
|
}
|
|
|
|
/**
|
|
* Create a `HostServices.secrets` adapter for a specific plugin.
|
|
*
|
|
* The returned service looks up secrets by UUID, fetches the latest version
|
|
* material, and delegates to the appropriate `SecretProviderModule` for
|
|
* decryption.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const secretsHandler = createPluginSecretsHandler({ db, pluginId });
|
|
* const handlers = createHostClientHandlers({
|
|
* pluginId,
|
|
* capabilities: manifest.capabilities,
|
|
* services: {
|
|
* secrets: secretsHandler,
|
|
* // ...
|
|
* },
|
|
* });
|
|
* ```
|
|
*
|
|
* @param options - Database connection and plugin identity
|
|
* @returns A `PluginSecretsService` suitable for `HostServices.secrets`
|
|
*/
|
|
/** Simple sliding-window rate limiter for secret resolution attempts. */
|
|
function createRateLimiter(maxAttempts: number, windowMs: number) {
|
|
const attempts = new Map<string, number[]>();
|
|
|
|
return {
|
|
check(key: string): boolean {
|
|
const now = Date.now();
|
|
const windowStart = now - windowMs;
|
|
const existing = (attempts.get(key) ?? []).filter((ts) => ts > windowStart);
|
|
if (existing.length >= maxAttempts) return false;
|
|
existing.push(now);
|
|
attempts.set(key, existing);
|
|
return true;
|
|
},
|
|
};
|
|
}
|
|
|
|
export function createPluginSecretsHandler(
|
|
options: PluginSecretsHandlerOptions,
|
|
): PluginSecretsService {
|
|
const { db, pluginId } = options;
|
|
const registry = pluginRegistryService(db);
|
|
|
|
// Rate limit: max 30 resolution attempts per plugin per minute
|
|
const rateLimiter = createRateLimiter(30, 60_000);
|
|
|
|
let cachedAllowedRefs: Set<string> | null = null;
|
|
let cachedAllowedRefsExpiry = 0;
|
|
const CONFIG_CACHE_TTL_MS = 30_000; // 30 seconds, matches event bus TTL
|
|
|
|
return {
|
|
async resolve(params: PluginSecretsResolveParams): Promise<string> {
|
|
const { secretRef } = params;
|
|
|
|
// ---------------------------------------------------------------
|
|
// 0. Rate limiting — prevent brute-force UUID enumeration
|
|
// ---------------------------------------------------------------
|
|
if (!rateLimiter.check(pluginId)) {
|
|
const err = new Error("Rate limit exceeded for secret resolution");
|
|
err.name = "RateLimitExceededError";
|
|
throw err;
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// 1. Validate the ref format
|
|
// ---------------------------------------------------------------
|
|
if (!secretRef || typeof secretRef !== "string" || secretRef.trim().length === 0) {
|
|
throw invalidSecretRef(secretRef ?? "<empty>");
|
|
}
|
|
|
|
const trimmedRef = secretRef.trim();
|
|
|
|
if (!isUuidSecretRef(trimmedRef)) {
|
|
throw invalidSecretRef(trimmedRef);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// 1b. Scope check — only allow secrets referenced in this plugin's config
|
|
// ---------------------------------------------------------------
|
|
const now = Date.now();
|
|
if (!cachedAllowedRefs || now > cachedAllowedRefsExpiry) {
|
|
const [configRow, plugin] = await Promise.all([
|
|
db
|
|
.select()
|
|
.from(pluginConfig)
|
|
.where(eq(pluginConfig.pluginId, pluginId))
|
|
.then((rows) => rows[0] ?? null),
|
|
registry.getById(pluginId),
|
|
]);
|
|
|
|
const schema = (plugin?.manifestJson as unknown as Record<string, unknown> | null)
|
|
?.instanceConfigSchema as Record<string, unknown> | undefined;
|
|
cachedAllowedRefs = extractSecretRefsFromConfig(configRow?.configJson, schema);
|
|
cachedAllowedRefsExpiry = now + CONFIG_CACHE_TTL_MS;
|
|
}
|
|
|
|
if (!cachedAllowedRefs.has(trimmedRef)) {
|
|
// Return "not found" to avoid leaking whether the secret exists
|
|
throw secretNotFound(trimmedRef);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// 2. Look up the secret record by UUID
|
|
// ---------------------------------------------------------------
|
|
const secret = await db
|
|
.select()
|
|
.from(companySecrets)
|
|
.where(eq(companySecrets.id, trimmedRef))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!secret) {
|
|
throw secretNotFound(trimmedRef);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// 3. Fetch the latest version's material
|
|
// ---------------------------------------------------------------
|
|
const versionRow = await db
|
|
.select()
|
|
.from(companySecretVersions)
|
|
.where(
|
|
and(
|
|
eq(companySecretVersions.secretId, secret.id),
|
|
eq(companySecretVersions.version, secret.latestVersion),
|
|
),
|
|
)
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!versionRow) {
|
|
throw secretVersionNotFound(trimmedRef);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// 4. Resolve through the appropriate secret provider
|
|
// ---------------------------------------------------------------
|
|
const provider = getSecretProvider(secret.provider as SecretProvider);
|
|
const resolved = await provider.resolveVersion({
|
|
material: versionRow.material as Record<string, unknown>,
|
|
externalRef: secret.externalRef,
|
|
});
|
|
|
|
return resolved;
|
|
},
|
|
};
|
|
}
|