mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-12 06:10:44 +01:00
## Thinking Path > - Paperclip manages AI agents and their work. > - Dot uses a user OAuth grant to act in one company. > - Cloud must use that original grant when it resolves a selected user's identity. > - A Cloud signature alone must not create user or board authority. > - The tenant's OAuth owner must check the token and its current restrictions. > - This pull request adds a signed private HTTP boundary for that check. > - The result contains only the checked identity fields. It grants no task or board access. ## Linked Issues or Issue Description **Subsystem affected** Server external-agent identity, public MCP OAuth, Cloud control verification, and HTTP logging. **Problem or motivation** Cloud needs a private way to resolve the original Dot token for the selected user, client, company and resource. The existing generic Cloud control route creates an operator actor. That route is not the right owner for this identity check. **Proposed solution** Add `POST /mcp/runner/oauth/cloud-principal`. Require the existing Cloud signature and replay verifier. Bind the whole request to a new, closed `mcp-dot:principal` action with a maximum 60-second lifetime. Authenticate the original token once through the normal Dot OAuth owner. Check current namespace authority before and after that call. **Alternatives considered** A Cloud-created grant or a board actor would change the authority owner. Neither is used. The private route reuses the existing OAuth and Cloud identity owners. **Roadmap alignment** This is a narrow boundary for the existing MCP and Cloud areas in `ROADMAP.md`. It adds no work queue, public hosted profile, or provider enablement. Related public work: Refs #14846, #19427, #19750 and #19776. Searches for the exact private path and action found no duplicate PR. The matching private Cloud caller has merged. The current App head passed its required checks and review. ## What Changed - Add an original-token principal reader. Keep the original client, selected user, company, resource, purpose, scope, membership and feature restrictions. - Add a 16 KiB raw JSON private route. Reject compressed, invalid UTF-8, malformed and unknown-field bodies. Bind an ordered whole-body digest to the signature and existing nonce fence. - Compare the current stack, claim, instance, origin and connector identity before and after OAuth. An absent owner or namespace fails closed. - Mount the route in normal `createApp` after hostname, runtime-claim and standby checks, before session/actor work. Do not add it to generic board-control routing. - Keep private request and response logs content-free, including raw buffers, headers, query data and reflected errors. - Add controlled HTTP/privacy tests and a real normal-App OAuth/Cloud-signer integration case. Document the machine-only contract and correct the existing `assistant-connections/client-use` access entry. No new capability ID or agent API entry is added. No schema, lockfile, workflow, Runner, Muse or useful-task fixture changes belong to this PR. ## Verification - Current `70c03593`: normal module-boundary policy and all seven App compiler programs passed. The public module factory has an explicit portable Express return contract. Whole HTTP/log controls passed again: 30/30. The existing real paired OAuth/Cloud-signer/private HTTP case then passed again on a fresh PostgreSQL fixture: 1/1, zero skipped. All known child processes and five database connections closed; the final independent SQL audit was empty. - The original remote policy failure rejected an App import of a module-internal path; dependent E2E/verify gates stopped on that failure. The fix delegates through the public module API. A first local declaration run reported TS2883 for its inferred return type; the explicit public type fixed it. Original logs remain preserved. No checker, compiler option, test body or authority check was weakened. - The normal full App graph passed on the integrated source. The later fixture-only repairs each passed the complete affected owning program:405 entries, including 403 source roots. The remaining programs and own dependency outputs carried only where their source was exact. - Whole private HTTP/log files passed:30 tests, zero failures or skips. The HTTP controls use a controlled OAuth owner with real signatures. The log controls use the actual HTTP logger. - The paired Cloud source passed its normal strict build and whole 26 signer/client/namespace and credential-error controls. These are controlled caller tests, not provider tests. - One normal-App paired case passed with a dedicated PostgreSQL database. It used real DCR 201, PKCE, consent, token exchange and a persisted unpaired grant; actual signed metadata publication and a committed Cloud receipt; the real Cloud signer/client and actual App HTTP; exactly-once authentication; refresh; current namespace restrictions; and revocation. The Cloud registry/provider were the existing controlled in-memory/Fake fixtures. Recorded inbound Hosts matched, with zero metadata overflow. No grant, readiness or mirror ACK was fabricated. - The original principal helper's 21 controlled tests and two real-OAuth tests passed before the HTTP slice. Those three files remained unchanged through integration. They are separate source-scoped evidence, not an extra combined run. - Preserved failures: a helper import and test typing error were corrected before the passed checks. An earlier suite failure did not serialize its hook error and remains unexplained. Later runs captured a missing normal managed-config initialization and HTTP 404 from the loopback mapper. The fixture now calls the normal initialization owners and uses the existing Host-preserving canonical HTTP adapter. All original authority assertions and deadlines remain. - Reproduce the local files with `pnpm --filter @paperclipai/server exec vitest run src/modules/external-agents/cloud-principal-http.test.ts src/modules/external-agents/cloud-principal-log.test.ts --maxWorkers=1 --no-file-parallelism`. The real integration file is `server/src/__tests__/cloud-principal-normal-app.postgres.test.ts`; it requires its explicit dedicated test DB and an own compiled matching Cloud checkout. - The normal `node scripts/check-maps.mjs` check passed for 35 areas and 192 capability IDs. This validates map structure, not business acceptance. The later edits are documentation/map only; all executed business source and own outputs from the paired case remained exact. - Current-head remote CI passes: 59 successful checks and three intentional skips. Greptile reviewed this exact head at 5/5 with no findings or review threads. The prior Ponytail review was Ship 5/5 at the earlier head; no current-head Ponytail turn is present, which is nonblocking under the authorized review policy. The three full-repository commands were not repeated locally; the focused normal App graph and affected tests above passed, and required remote CI supplies the broad gates. No deployed, provider-capacity, cold-wake or full hosted lifecycle result is claimed. ## Risks - This is a security boundary. Signature, current namespace and original OAuth restrictions must continue to fail closed. The response gives identity only; callers still need normal operation authority. - The request carries the original token. The reserved route uses a closed log projection on both accepted and refused requests. Privacy regressions remain a material risk. - Both repositories must agree on the action and digest contract. Older code rejects the unsupported action; no fallback creates authority. - This is a prerequisite private boundary. The remaining hosted discovery, wake, capability, operation, event, retry/reactivation and revocation lifecycle work, plus Muse, remains open. No profile or capability is enabled here. - No migration is needed. Rollback removes the private boundary and paired caller; normal OAuth and Personal MCP remain with their existing owners. - The engineering documentation describes this private machine boundary and its access restriction. No operator-facing feature is enabled. Documentation for full hosted enablement remains with that later work. ## Model Used OpenAI `gpt-6.1-sol` (Sol6.1), through Codex, produced the source and tests with tool use and local compiler/test execution. A GPT-6-based Codex coordinator reviewed the work. Its exact model ID is unavailable. Context window sizes are unavailable. ## 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 searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge
592 lines
21 KiB
TypeScript
592 lines
21 KiB
TypeScript
import { parseIdleCapabilities, type IdleCapabilities } from "./idle-incoming-wake-capability.js";
|
|
import { createPublicKey, timingSafeEqual, verify, type JsonWebKey } from "node:crypto";
|
|
import { eq } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { instanceSettings } from "@paperclipai/db";
|
|
|
|
export const CLOUD_RUNTIME_IDENTITY_HEADER = "x-paperclip-cloud-runtime-identity";
|
|
export const CLOUD_RUNTIME_IDENTITY_AUDIENCE = "paperclip-runtime-identity/v1";
|
|
export const CLOUD_RUNTIME_IDENTITY_ISSUER = "paperclip-cloud";
|
|
export const CLOUD_RUNTIME_IDENTITY_JWS_TYPE = "paperclip-cloud-runtime-identity+jwt";
|
|
|
|
const SINGLETON_KEY = "cloud-runtime-identity/v1";
|
|
const MAX_ASSERTION_LIFETIME_SECONDS = 10 * 60;
|
|
const MAX_CLOCK_SKEW_SECONDS = 30;
|
|
const STACK_SLUG_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
|
|
type PersistedRuntimeIdentity = {
|
|
stackId: string;
|
|
claimId: string;
|
|
previousOrigin: string;
|
|
canonicalOrigin: string;
|
|
stackSlug: string;
|
|
appliedAt: Date;
|
|
};
|
|
|
|
type RuntimeIdentityDb = Pick<Db, "select" | "insert">;
|
|
|
|
export type CloudRuntimeIdentitySnapshot = {
|
|
stackId: string;
|
|
claimId: string;
|
|
previousOrigin: string;
|
|
canonicalOrigin: string;
|
|
stackSlug: string;
|
|
appliedAt: Date;
|
|
};
|
|
|
|
export type RuntimeIdentityClaims = {
|
|
v: 1;
|
|
iss: typeof CLOUD_RUNTIME_IDENTITY_ISSUER;
|
|
aud: typeof CLOUD_RUNTIME_IDENTITY_AUDIENCE;
|
|
sub: string;
|
|
claimId: string;
|
|
previousOrigin: string;
|
|
canonicalOrigin: string;
|
|
stackSlug: string;
|
|
iat: number;
|
|
exp: number;
|
|
};
|
|
|
|
let initialized = false;
|
|
let startupOrigin: string | null = null;
|
|
let currentIdentity: CloudRuntimeIdentitySnapshot | null = null;
|
|
|
|
function nonEmpty(value: string | undefined): string | null {
|
|
const normalized = value?.trim();
|
|
return normalized ? normalized : null;
|
|
}
|
|
|
|
function exactHttpsOrigin(value: unknown): string | null {
|
|
if (typeof value !== "string" || value.length === 0 || value.length > 2048) return null;
|
|
try {
|
|
const parsed = new URL(value);
|
|
if (
|
|
parsed.protocol !== "https:"
|
|
|| parsed.username
|
|
|| parsed.password
|
|
|| parsed.pathname !== "/"
|
|
|| parsed.search
|
|
|| parsed.hash
|
|
|| parsed.origin !== value
|
|
) {
|
|
return null;
|
|
}
|
|
return parsed.origin;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function configuredStartupOrigin(env: NodeJS.ProcessEnv): string | null {
|
|
const candidate = nonEmpty(env.PAPERCLIP_PUBLIC_URL)
|
|
?? nonEmpty(env.PAPERCLIP_AUTH_PUBLIC_BASE_URL)
|
|
?? nonEmpty(env.PAPERCLIP_API_URL);
|
|
return candidate ? exactHttpsOrigin(candidate) : null;
|
|
}
|
|
|
|
function snapshot(row: PersistedRuntimeIdentity): CloudRuntimeIdentitySnapshot {
|
|
return {
|
|
stackId: row.stackId,
|
|
claimId: row.claimId,
|
|
previousOrigin: row.previousOrigin,
|
|
canonicalOrigin: row.canonicalOrigin,
|
|
stackSlug: row.stackSlug,
|
|
appliedAt: row.appliedAt,
|
|
};
|
|
}
|
|
|
|
function parsePersistedIdentity(row: {
|
|
general: Record<string, unknown>;
|
|
createdAt: Date;
|
|
}): PersistedRuntimeIdentity {
|
|
const value = row.general;
|
|
if (
|
|
value.v !== 1
|
|
|| typeof value.stackId !== "string"
|
|
|| typeof value.claimId !== "string"
|
|
|| typeof value.previousOrigin !== "string"
|
|
|| typeof value.canonicalOrigin !== "string"
|
|
|| typeof value.stackSlug !== "string"
|
|
) {
|
|
throw new Error("Persisted Cloud runtime identity is malformed");
|
|
}
|
|
return {
|
|
stackId: value.stackId,
|
|
claimId: value.claimId,
|
|
previousOrigin: value.previousOrigin,
|
|
canonicalOrigin: value.canonicalOrigin,
|
|
stackSlug: value.stackSlug,
|
|
appliedAt: row.createdAt,
|
|
};
|
|
}
|
|
|
|
async function readPersistedIdentity(db: RuntimeIdentityDb): Promise<PersistedRuntimeIdentity | null> {
|
|
const row = await db
|
|
.select({
|
|
general: instanceSettings.general,
|
|
createdAt: instanceSettings.createdAt,
|
|
})
|
|
.from(instanceSettings)
|
|
.where(eq(instanceSettings.singletonKey, SINGLETON_KEY))
|
|
.limit(1)
|
|
.then((rows) => rows[0] ?? null);
|
|
return row ? parsePersistedIdentity(row) : null;
|
|
}
|
|
|
|
function applyCompatibilityEnvironment(identity: CloudRuntimeIdentitySnapshot, env: NodeJS.ProcessEnv) {
|
|
const hostname = new URL(identity.canonicalOrigin).hostname;
|
|
env.PAPERCLIP_PUBLIC_URL = identity.canonicalOrigin;
|
|
env.PAPERCLIP_AUTH_PUBLIC_BASE_URL = identity.canonicalOrigin;
|
|
env.PAPERCLIP_API_URL = identity.canonicalOrigin;
|
|
env.PAPERCLIP_PRIMARY_HOST = hostname;
|
|
env.PAPERCLIP_STACK_SLUG = identity.stackSlug;
|
|
|
|
const existingCandidates = (() => {
|
|
try {
|
|
const parsed = JSON.parse(env.PAPERCLIP_RUNTIME_API_CANDIDATES_JSON ?? "[]");
|
|
return Array.isArray(parsed) ? parsed.filter((value): value is string => typeof value === "string") : [];
|
|
} catch {
|
|
return [];
|
|
}
|
|
})();
|
|
env.PAPERCLIP_RUNTIME_API_CANDIDATES_JSON = JSON.stringify([
|
|
identity.canonicalOrigin,
|
|
...existingCandidates.filter((candidate) => candidate !== identity.canonicalOrigin),
|
|
]);
|
|
}
|
|
|
|
function assertPersistedIdentityMatchesStack(row: PersistedRuntimeIdentity, env: NodeJS.ProcessEnv) {
|
|
const configuredStackId = nonEmpty(env.PAPERCLIP_CLOUD_STACK_ID);
|
|
if (!configuredStackId || configuredStackId !== row.stackId) {
|
|
throw new Error("Persisted Cloud runtime identity does not match PAPERCLIP_CLOUD_STACK_ID");
|
|
}
|
|
if (!exactHttpsOrigin(row.previousOrigin) || !exactHttpsOrigin(row.canonicalOrigin)) {
|
|
throw new Error("Persisted Cloud runtime identity contains an invalid origin");
|
|
}
|
|
if (!STACK_SLUG_PATTERN.test(row.stackSlug) || new URL(row.canonicalOrigin).hostname.split(".")[0] !== row.stackSlug) {
|
|
throw new Error("Persisted Cloud runtime identity contains an invalid stack slug");
|
|
}
|
|
}
|
|
|
|
/** Load the durable claim before auth, routes, and child-runtime configuration. */
|
|
export async function initializeCloudRuntimeIdentity(
|
|
db: Db,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
): Promise<CloudRuntimeIdentitySnapshot | null> {
|
|
startupOrigin = configuredStartupOrigin(env);
|
|
// Self-hosted servers have no Cloud stack identity to restore. Avoid touching
|
|
// the singleton table on that path; besides keeping the feature inert, this
|
|
// preserves lightweight startup/test database seams that intentionally do
|
|
// not construct a database client.
|
|
if (!nonEmpty(env.PAPERCLIP_CLOUD_STACK_ID)) {
|
|
initialized = true;
|
|
currentIdentity = null;
|
|
return null;
|
|
}
|
|
const row = await readPersistedIdentity(db);
|
|
initialized = true;
|
|
if (!row) {
|
|
currentIdentity = null;
|
|
return null;
|
|
}
|
|
assertPersistedIdentityMatchesStack(row, env);
|
|
currentIdentity = snapshot(row);
|
|
applyCompatibilityEnvironment(currentIdentity, env);
|
|
return currentIdentity;
|
|
}
|
|
|
|
export function getCloudRuntimeIdentity(): CloudRuntimeIdentitySnapshot | null {
|
|
return currentIdentity ? { ...currentIdentity } : null;
|
|
}
|
|
|
|
/** The live canonical origin, falling back to startup configuration off Cloud. */
|
|
export function runtimePublicOrigin(env: NodeJS.ProcessEnv = process.env): string | null {
|
|
if (env === process.env && currentIdentity) return currentIdentity.canonicalOrigin;
|
|
const candidate = nonEmpty(env.PAPERCLIP_PUBLIC_URL)
|
|
?? nonEmpty(env.PAPERCLIP_AUTH_PUBLIC_BASE_URL)
|
|
?? nonEmpty(env.PAPERCLIP_API_URL);
|
|
if (!candidate) return null;
|
|
try {
|
|
return new URL(candidate).origin;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** The asserted Cloud origin only. Callers retain their non-Cloud precedence. */
|
|
export function runtimeCanonicalOrigin(): string | null {
|
|
return currentIdentity?.canonicalOrigin ?? null;
|
|
}
|
|
|
|
function decodeJsonPart(part: string, label: string): Record<string, unknown> {
|
|
if (!/^[A-Za-z0-9_-]+$/.test(part)) throw new Error(`Cloud runtime identity has an invalid ${label}`);
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(Buffer.from(part, "base64url").toString("utf8"));
|
|
} catch {
|
|
throw new Error(`Cloud runtime identity has an invalid ${label}`);
|
|
}
|
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
throw new Error(`Cloud runtime identity has an invalid ${label}`);
|
|
}
|
|
return parsed as Record<string, unknown>;
|
|
}
|
|
|
|
export function cloudRuntimePublicKeyForKid(env: NodeJS.ProcessEnv, kid: string) {
|
|
const raw = nonEmpty(env.PAPERCLIP_CLOUD_RUNTIME_IDENTITY_JWKS);
|
|
if (!raw) throw new Error("PAPERCLIP_CLOUD_RUNTIME_IDENTITY_JWKS is not configured");
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(raw);
|
|
} catch {
|
|
throw new Error("PAPERCLIP_CLOUD_RUNTIME_IDENTITY_JWKS is invalid");
|
|
}
|
|
const keys = parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
? (parsed as { keys?: unknown }).keys
|
|
: undefined;
|
|
if (!Array.isArray(keys)) throw new Error("PAPERCLIP_CLOUD_RUNTIME_IDENTITY_JWKS is invalid");
|
|
const matches = keys.filter((candidate): candidate is JsonWebKey & { kid: string } => {
|
|
if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) return false;
|
|
const key = candidate as JsonWebKey & { kid?: unknown };
|
|
return key.kid === kid;
|
|
});
|
|
if (matches.length !== 1) throw new Error("Cloud runtime identity uses an unknown signing key");
|
|
const jwk = matches[0];
|
|
if (jwk.kty !== "OKP" || jwk.crv !== "Ed25519" || jwk.use !== "sig" || jwk.alg !== "EdDSA" || !jwk.x || jwk.d) {
|
|
throw new Error("Cloud runtime identity signing key is invalid");
|
|
}
|
|
return createPublicKey({ key: jwk, format: "jwk" });
|
|
}
|
|
|
|
function verifyClaims(input: {
|
|
compactJws: string;
|
|
env: NodeJS.ProcessEnv;
|
|
now: Date;
|
|
}): RuntimeIdentityClaims {
|
|
const parts = input.compactJws.split(".");
|
|
if (parts.length !== 3 || parts.some((part) => part.length === 0)) {
|
|
throw new Error("Cloud runtime identity assertion is not a compact JWS");
|
|
}
|
|
const [encodedHeader, encodedPayload, encodedSignature] = parts;
|
|
const header = decodeJsonPart(encodedHeader, "protected header");
|
|
if (
|
|
header.alg !== "EdDSA"
|
|
|| header.typ !== CLOUD_RUNTIME_IDENTITY_JWS_TYPE
|
|
|| typeof header.kid !== "string"
|
|
|| !header.kid
|
|
) {
|
|
throw new Error("Cloud runtime identity protected header is invalid");
|
|
}
|
|
const key = cloudRuntimePublicKeyForKid(input.env, header.kid);
|
|
const signature = Buffer.from(encodedSignature, "base64url");
|
|
const signingInput = Buffer.from(`${encodedHeader}.${encodedPayload}`, "ascii");
|
|
if (!verify(null, signingInput, key, signature)) {
|
|
throw new Error("Cloud runtime identity signature is invalid");
|
|
}
|
|
|
|
const payload = decodeJsonPart(encodedPayload, "payload");
|
|
const nowSeconds = Math.floor(input.now.getTime() / 1000);
|
|
if (
|
|
payload.v !== 1
|
|
|| payload.iss !== CLOUD_RUNTIME_IDENTITY_ISSUER
|
|
|| payload.aud !== CLOUD_RUNTIME_IDENTITY_AUDIENCE
|
|
|| typeof payload.sub !== "string"
|
|
|| typeof payload.claimId !== "string"
|
|
|| typeof payload.previousOrigin !== "string"
|
|
|| typeof payload.canonicalOrigin !== "string"
|
|
|| typeof payload.stackSlug !== "string"
|
|
|| typeof payload.iat !== "number"
|
|
|| !Number.isInteger(payload.iat)
|
|
|| typeof payload.exp !== "number"
|
|
|| !Number.isInteger(payload.exp)
|
|
) {
|
|
throw new Error("Cloud runtime identity claims are incomplete");
|
|
}
|
|
if (
|
|
payload.exp <= nowSeconds
|
|
|| payload.iat > nowSeconds + MAX_CLOCK_SKEW_SECONDS
|
|
|| payload.exp <= payload.iat
|
|
|| payload.exp - payload.iat > MAX_ASSERTION_LIFETIME_SECONDS
|
|
) {
|
|
throw new Error("Cloud runtime identity assertion is expired or has an invalid lifetime");
|
|
}
|
|
return payload as RuntimeIdentityClaims;
|
|
}
|
|
|
|
/** Verify that an assertion is signed for this exact, still-unclaimed instance. */
|
|
export function verifyCloudRuntimeIdentityAssertion(input: {
|
|
compactJws: string;
|
|
env?: NodeJS.ProcessEnv;
|
|
now?: Date;
|
|
expectedPreviousOrigin: string | null;
|
|
}): RuntimeIdentityClaims {
|
|
const env = input.env ?? process.env;
|
|
const claims = verifyClaims({ compactJws: input.compactJws, env, now: input.now ?? new Date() });
|
|
const configuredStackId = nonEmpty(env.PAPERCLIP_CLOUD_STACK_ID);
|
|
if (!configuredStackId || claims.sub !== configuredStackId) {
|
|
throw new Error("Cloud runtime identity stack does not match this instance");
|
|
}
|
|
const previousOrigin = exactHttpsOrigin(claims.previousOrigin);
|
|
const canonicalOrigin = exactHttpsOrigin(claims.canonicalOrigin);
|
|
if (!previousOrigin || !canonicalOrigin || previousOrigin !== input.expectedPreviousOrigin) {
|
|
throw new Error("Cloud runtime identity previous or canonical origin is invalid");
|
|
}
|
|
if (
|
|
!STACK_SLUG_PATTERN.test(claims.stackSlug)
|
|
|| new URL(canonicalOrigin).hostname.split(".")[0] !== claims.stackSlug
|
|
|| claims.claimId.length > 256
|
|
|| claims.claimId.trim() !== claims.claimId
|
|
|| !claims.claimId
|
|
) {
|
|
throw new Error("Cloud runtime identity destination is invalid");
|
|
}
|
|
return claims;
|
|
}
|
|
|
|
function assertionsEqual(row: PersistedRuntimeIdentity, claims: RuntimeIdentityClaims): boolean {
|
|
const left = Buffer.from(JSON.stringify([
|
|
row.stackId,
|
|
row.claimId,
|
|
row.previousOrigin,
|
|
row.canonicalOrigin,
|
|
row.stackSlug,
|
|
]));
|
|
const right = Buffer.from(JSON.stringify([
|
|
claims.sub,
|
|
claims.claimId,
|
|
claims.previousOrigin,
|
|
claims.canonicalOrigin,
|
|
claims.stackSlug,
|
|
]));
|
|
return left.length === right.length && timingSafeEqual(left, right);
|
|
}
|
|
|
|
/** Verify and durably apply the one-time Cloud claim assertion. */
|
|
export async function applyCloudRuntimeIdentityAssertion(input: {
|
|
db: Db;
|
|
compactJws: string;
|
|
env?: NodeJS.ProcessEnv;
|
|
now?: Date;
|
|
}): Promise<CloudRuntimeIdentitySnapshot> {
|
|
const env = input.env ?? process.env;
|
|
if (!initialized) throw new Error("Cloud runtime identity provider is not initialized");
|
|
const claims = verifyCloudRuntimeIdentityAssertion({
|
|
compactJws: input.compactJws,
|
|
env,
|
|
now: input.now,
|
|
// After a natural restart the provider env may already be canonical, but
|
|
// an identical retry of the original claim is still safe and idempotent.
|
|
// The durable row preserves the pool origin that assertion had to match
|
|
// on first application.
|
|
expectedPreviousOrigin: currentIdentity?.previousOrigin ?? startupOrigin,
|
|
});
|
|
const previousOrigin = claims.previousOrigin;
|
|
const canonicalOrigin = claims.canonicalOrigin;
|
|
|
|
const row = await input.db.transaction(async (tx) => {
|
|
const existing = await readPersistedIdentity(tx);
|
|
if (existing) {
|
|
if (!assertionsEqual(existing, claims)) {
|
|
throw new Error("Cloud runtime identity is already claimed by another assertion");
|
|
}
|
|
return existing;
|
|
}
|
|
|
|
const now = input.now ?? new Date();
|
|
await tx
|
|
.insert(instanceSettings)
|
|
.values({
|
|
singletonKey: SINGLETON_KEY,
|
|
general: {
|
|
v: 1,
|
|
stackId: claims.sub,
|
|
claimId: claims.claimId,
|
|
previousOrigin,
|
|
canonicalOrigin,
|
|
stackSlug: claims.stackSlug,
|
|
},
|
|
experimental: {},
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.onConflictDoNothing({ target: instanceSettings.singletonKey });
|
|
const durable = await readPersistedIdentity(tx);
|
|
if (!durable || !assertionsEqual(durable, claims)) {
|
|
throw new Error("Cloud runtime identity is already claimed by another assertion");
|
|
}
|
|
return durable;
|
|
});
|
|
|
|
currentIdentity = snapshot(row);
|
|
applyCompatibilityEnvironment(currentIdentity, env);
|
|
return { ...currentIdentity };
|
|
}
|
|
|
|
/** Test seam for modules that intentionally share a process. */
|
|
export function resetCloudRuntimeIdentityForTests() {
|
|
initialized = false;
|
|
startupOrigin = null;
|
|
currentIdentity = null;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Cloud control assertions
|
|
//
|
|
// A second, deliberately separate use of the same Cloud signing key: where the
|
|
// runtime identity assertion above is a ONE-TIME origin claim applied at
|
|
// bootstrap, a control assertion authorizes a single management call from the
|
|
// Cloud control plane — today, only the task-drain admission hold, so Cloud
|
|
// can stop new agent work and wait for quiescence before it restarts the
|
|
// container for a deploy. The audience, JWS type, and claim shape are
|
|
// disjoint from the runtime identity assertion, so neither token can ever be
|
|
// replayed as the other, and the verifier binds each assertion to one exact
|
|
// action so a "read status" token cannot start or stop a drain.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export const CLOUD_CONTROL_HEADER = "x-paperclip-cloud-control";
|
|
export const CLOUD_CONTROL_AUDIENCE = "paperclip-cloud-control/v1";
|
|
export const CLOUD_CONTROL_JWS_TYPE = "paperclip-cloud-control+jwt";
|
|
export const CLOUD_CONTROL_ACTIONS = [
|
|
"ephemeral-enrollment:prepare",
|
|
"task-drain:read",
|
|
"task-drain:start",
|
|
"task-drain:stop",
|
|
"lifecycle:read",
|
|
"lifecycle:unarchive-primary",
|
|
"connector-events:wake",
|
|
"mcp-device:mint",
|
|
"mcp-dot:principal",
|
|
] as const;
|
|
export type CloudControlAction = (typeof CLOUD_CONTROL_ACTIONS)[number];
|
|
|
|
/** Control calls are immediate; a stolen assertion should age out fast. */
|
|
const CLOUD_CONTROL_MAX_LIFETIME_SECONDS = 5 * 60;
|
|
|
|
/**
|
|
* Single-use fence: a verified assertion's requestId is consumed atomically
|
|
* (module state; JS execution is single-threaded per process) and a replay
|
|
* of the same id is rejected for as long as the original could still be
|
|
* alive. Process-local on purpose — the drain state this protects is
|
|
* itself process-local, so a restart clears both together. Entries prune
|
|
* lazily at their expiry.
|
|
*/
|
|
const consumedControlRequestIds = new Map<string, number>();
|
|
|
|
function consumeControlRequestId(requestId: string, expSeconds: number, nowMs: number): boolean {
|
|
for (const [id, expiresAtMs] of consumedControlRequestIds) {
|
|
if (expiresAtMs <= nowMs) consumedControlRequestIds.delete(id);
|
|
}
|
|
if (consumedControlRequestIds.has(requestId)) {
|
|
return false;
|
|
}
|
|
consumedControlRequestIds.set(requestId, expSeconds * 1000);
|
|
return true;
|
|
}
|
|
|
|
/** Test seam: modules sharing a process must be able to reset the fence. */
|
|
export function resetCloudControlReplayFenceForTests() {
|
|
consumedControlRequestIds.clear();
|
|
}
|
|
|
|
export type CloudControlClaims = {
|
|
v: 1;
|
|
iss: typeof CLOUD_RUNTIME_IDENTITY_ISSUER;
|
|
aud: typeof CLOUD_CONTROL_AUDIENCE;
|
|
sub: string;
|
|
action: CloudControlAction;
|
|
requestId: string;
|
|
idleCapabilities?: IdleCapabilities;
|
|
iat: number;
|
|
exp: number;
|
|
};
|
|
|
|
/**
|
|
* Verify a Cloud control assertion for one exact action on this instance.
|
|
* Signed with the same key set as the runtime identity assertion
|
|
* (PAPERCLIP_CLOUD_RUNTIME_IDENTITY_JWKS) but under its own JWS type and
|
|
* audience. Instances without a Cloud stack identity reject every assertion —
|
|
* the feature is inert when self-hosted.
|
|
*/
|
|
export function verifyCloudControlAssertion(input: {
|
|
compactJws: string;
|
|
expectedAction: CloudControlAction;
|
|
env?: NodeJS.ProcessEnv;
|
|
now?: Date;
|
|
}): CloudControlClaims {
|
|
const env = input.env ?? process.env;
|
|
const now = input.now ?? new Date();
|
|
const parts = input.compactJws.split(".");
|
|
if (parts.length !== 3 || parts.some((part) => part.length === 0)) {
|
|
throw new Error("Cloud control assertion is not a compact JWS");
|
|
}
|
|
const [encodedHeader, encodedPayload, encodedSignature] = parts;
|
|
const header = decodeJsonPart(encodedHeader, "protected header");
|
|
if (
|
|
header.alg !== "EdDSA"
|
|
|| header.typ !== CLOUD_CONTROL_JWS_TYPE
|
|
|| typeof header.kid !== "string"
|
|
|| !header.kid
|
|
) {
|
|
throw new Error("Cloud control protected header is invalid");
|
|
}
|
|
const key = cloudRuntimePublicKeyForKid(env, header.kid);
|
|
const signature = Buffer.from(encodedSignature, "base64url");
|
|
const signingInput = Buffer.from(`${encodedHeader}.${encodedPayload}`, "ascii");
|
|
if (!verify(null, signingInput, key, signature)) {
|
|
throw new Error("Cloud control signature is invalid");
|
|
}
|
|
|
|
const payload = decodeJsonPart(encodedPayload, "payload");
|
|
const configuredStackId = nonEmpty(env.PAPERCLIP_CLOUD_STACK_ID);
|
|
const nowSeconds = Math.floor(now.getTime() / 1000);
|
|
if (
|
|
payload.v !== 1
|
|
|| payload.iss !== CLOUD_RUNTIME_IDENTITY_ISSUER
|
|
|| payload.aud !== CLOUD_CONTROL_AUDIENCE
|
|
|| typeof payload.sub !== "string"
|
|
|| typeof payload.action !== "string"
|
|
|| typeof payload.requestId !== "string"
|
|
|| typeof payload.iat !== "number"
|
|
|| !Number.isInteger(payload.iat)
|
|
|| typeof payload.exp !== "number"
|
|
|| !Number.isInteger(payload.exp)
|
|
) {
|
|
throw new Error("Cloud control claims are incomplete");
|
|
}
|
|
if (!configuredStackId || payload.sub !== configuredStackId) {
|
|
throw new Error("Cloud control assertion stack does not match this instance");
|
|
}
|
|
if (
|
|
!(CLOUD_CONTROL_ACTIONS as readonly string[]).includes(payload.action)
|
|
|| payload.action !== input.expectedAction
|
|
) {
|
|
throw new Error("Cloud control assertion does not authorize this action");
|
|
}
|
|
if (
|
|
!payload.requestId
|
|
|| payload.requestId.trim() !== payload.requestId
|
|
|| payload.requestId.length > 256
|
|
) {
|
|
throw new Error("Cloud control assertion request id is invalid");
|
|
}
|
|
if (
|
|
payload.exp <= nowSeconds
|
|
|| payload.iat > nowSeconds + MAX_CLOCK_SKEW_SECONDS
|
|
|| payload.exp <= payload.iat
|
|
|| payload.exp - payload.iat > CLOUD_CONTROL_MAX_LIFETIME_SECONDS
|
|
) {
|
|
throw new Error("Cloud control assertion is expired or has an invalid lifetime");
|
|
}
|
|
if (payload.idleCapabilities !== undefined) {
|
|
if (payload.action !== "task-drain:read") throw new Error("Idle capabilities require task-drain:read");
|
|
payload.idleCapabilities = parseIdleCapabilities(payload.idleCapabilities);
|
|
}
|
|
// Consumed LAST, only after every other check passed: a rejected
|
|
// assertion must not burn its request id, or an attacker could deny a
|
|
// legitimate call by replaying a mangled copy of it first.
|
|
if (!consumeControlRequestId(payload.requestId, payload.exp + MAX_CLOCK_SKEW_SECONDS, now.getTime())) {
|
|
throw new Error("Cloud control assertion has already been used");
|
|
}
|
|
return payload as CloudControlClaims;
|
|
}
|