Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 259 additions & 0 deletions packages/cloud/shared/src/lib/services/agent-env-crypto.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
/**
* At-rest encryption of `agent_sandboxes.environment_vars` (#11332).
*
* The bug: `updateAgentEnvironment` persisted PATCHed BYO provider keys
* (ANTHROPIC_API_KEY / OPENAI_API_KEY / ...) verbatim into the plaintext jsonb
* column, so user secrets sat unencrypted at rest. The first suite's at-rest
* assertions FAIL against that pre-fix write path (negative control) and pass
* after it: the value handed to the repository is an `enc:v1:` envelope, not
* the secret.
*
* The crypto is the REAL FieldEncryptionService (AES-256-GCM, org DEK wrapped
* by SECRETS_MASTER_KEY) — only its org-key persistence (the db helpers) is
* swapped for an in-memory store, exactly the boundary the sibling service
* tests already mock. The repository row store is `spyOn`-captured so the test
* asserts the exact bytes that would land in Postgres.
*/

import {
afterAll,
afterEach,
beforeEach,
describe,
expect,
mock,
spyOn,
test,
} from "bun:test";
import { randomUUID } from "node:crypto";

import * as realHelpersNs from "../../db/helpers";
import type { AgentSandbox } from "../../db/repositories/agent-sandboxes";
import { agentSandboxesRepository } from "../../db/repositories/agent-sandboxes";

// ---- in-memory organization_encryption_keys store for FieldEncryptionService ----
interface OrgKeyRow {
id: string;
organization_id: string;
encrypted_dek: string;
key_version: number;
created_at: Date;
rotated_at: Date | null;
}
const orgKeyRows: OrgKeyRow[] = [];

const orgKeyDb = {
query: {
organizationEncryptionKeys: {
// Single-org tests: the row set has at most one entry, so the `where`
// clause (org id / key id) always resolves to it.
findFirst: async () => orgKeyRows[0],
},
},
insert: () => ({
values: (v: Record<string, unknown>) => ({
onConflictDoNothing: () => ({
returning: async () => {
const row: OrgKeyRow = {
id: randomUUID(),
key_version: 1,
created_at: new Date(),
rotated_at: null,
organization_id: v.organization_id as string,
encrypted_dek: v.encrypted_dek as string,
};
orgKeyRows.push(row);
return [row];
},
}),
}),
}),
};

const realHelpers = { ...realHelpersNs };
mock.module("../../db/helpers", () => ({
...realHelpers,
dbRead: orgKeyDb,
dbWrite: orgKeyDb,
}));
afterAll(() => {
mock.module("../../db/helpers", () => realHelpers);
});

// ---- captured repository writes (the at-rest boundary) ----
const ORG_ID = "00000000-0000-4000-8000-00000000a001";
const AGENT_ID = "00000000-0000-4000-8000-00000000a002";
const SECRET = "sk-ant-api03-users-real-provider-key";

function sandboxRow(env: Record<string, string>): AgentSandbox {
return {
id: AGENT_ID,
organization_id: ORG_ID,
environment_vars: env,
status: "running",
execution_tier: "dedicated-lazy",
} as unknown as AgentSandbox;
}

let storedRow: AgentSandbox;
let capturedUpdate: Record<string, unknown> | undefined;

const findSpy = spyOn(
agentSandboxesRepository,
"findByIdAndOrg",
).mockImplementation(async () => storedRow);
const updateSpy = spyOn(agentSandboxesRepository, "update").mockImplementation(
async (_id, data) => {
capturedUpdate = data as Record<string, unknown>;
storedRow = { ...storedRow, ...data } as AgentSandbox;
return storedRow;
},
);

const TEST_MASTER_KEY = "a".repeat(64);

beforeEach(() => {
process.env.SECRETS_MASTER_KEY = TEST_MASTER_KEY;
storedRow = sandboxRow({});
capturedUpdate = undefined;
findSpy.mockClear();
updateSpy.mockClear();
});

afterEach(() => {
delete process.env.SECRETS_MASTER_KEY;
});

afterAll(() => {
findSpy.mockRestore();
updateSpy.mockRestore();
});

async function patchEnv(
env: Record<string, string>,
): Promise<Record<string, string>> {
const { elizaSandboxService } = await import("./eliza-sandbox.ts?actual");
const updated = await elizaSandboxService.updateAgentEnvironment(
AGENT_ID,
ORG_ID,
env,
);
expect(updated).toBeDefined();
expect(capturedUpdate).toBeDefined();
return (capturedUpdate as { environment_vars: Record<string, string> })
.environment_vars;
}

describe("agent environment secrets are encrypted at rest (#11332)", () => {
test("a PATCHed provider key is ciphertext at rest — NOT the plaintext secret", async () => {
const stored = await patchEnv({
ANTHROPIC_API_KEY: SECRET,
ELIZA_PLUGIN_SET: "lean-chat",
});

// The at-rest bug: pre-fix these assertions fail — the row held the secret verbatim.
expect(stored.ANTHROPIC_API_KEY).not.toBe(SECRET);
expect(stored.ANTHROPIC_API_KEY.startsWith("enc:v1:")).toBe(true);
expect(JSON.stringify(stored)).not.toContain(SECRET);

// Non-secret config stays plaintext (value-inspected by the managed-env merge).
expect(stored.ELIZA_PLUGIN_SET).toBe("lean-chat");

// Round-trip: materialization returns the real value to the container.
const { decryptAgentEnvVars } = await import(
"./agent-env-crypto.ts?actual"
);
const materialized = await decryptAgentEnvVars(stored);
expect(materialized.ANTHROPIC_API_KEY).toBe(SECRET);
expect(materialized.ELIZA_PLUGIN_SET).toBe("lean-chat");
});

test("a legacy plaintext row materializes unchanged (no backfill required)", async () => {
const { decryptAgentEnvVars } = await import(
"./agent-env-crypto.ts?actual"
);
const legacy = {
OPENAI_API_KEY: "sk-legacy-plaintext-row",
ELIZA_UI_ENABLE: "true",
};
expect(await decryptAgentEnvVars(legacy)).toEqual(legacy);
});

test("platform bridge tokens are never encrypted — control-plane sync reads stay intact", async () => {
// The create route funnels the full managed env (incl. the platform's own
// tokens) through updateAgentEnvironment; getAgentApiToken / the dedicated
// proxy read these synchronously from the stored row, so they must stay
// plaintext.
const stored = await patchEnv({
ELIZA_API_TOKEN: "agent_platformtoken",
ELIZAOS_API_KEY: "legacy_alias_token",
ELIZAOS_CLOUD_API_KEY: "cloud_platform_key",
ANTHROPIC_API_KEY: SECRET,
});
expect(stored.ELIZA_API_TOKEN).toBe("agent_platformtoken");
expect(stored.ELIZAOS_API_KEY).toBe("legacy_alias_token");
expect(stored.ELIZAOS_CLOUD_API_KEY).toBe("cloud_platform_key");
expect(stored.ANTHROPIC_API_KEY.startsWith("enc:v1:")).toBe(true);
});

test("read-modify-write PATCH echoing stored ciphertext does not double-encrypt", async () => {
const first = await patchEnv({ OPENAI_API_KEY: "sk-first-secret" });
const firstCiphertext = first.OPENAI_API_KEY;
expect(firstCiphertext.startsWith("enc:v1:")).toBe(true);

// The environment route merges { ...existing, ...patch } — stored values
// ride back through the write path verbatim.
const second = await patchEnv({
OPENAI_API_KEY: firstCiphertext,
GITHUB_TOKEN: "ghp_new_secret",
});
expect(second.OPENAI_API_KEY).toBe(firstCiphertext);
expect(second.GITHUB_TOKEN.startsWith("enc:v1:")).toBe(true);

const { decryptAgentEnvVars } = await import(
"./agent-env-crypto.ts?actual"
);
const materialized = await decryptAgentEnvVars(second);
expect(materialized.OPENAI_API_KEY).toBe("sk-first-secret");
expect(materialized.GITHUB_TOKEN).toBe("ghp_new_secret");
});

test("without SECRETS_MASTER_KEY the write degrades to legacy plaintext (no hard dependency)", async () => {
delete process.env.SECRETS_MASTER_KEY;
const stored = await patchEnv({ ANTHROPIC_API_KEY: SECRET });
expect(stored.ANTHROPIC_API_KEY).toBe(SECRET);
});

test("decrypt failure fails closed with the key name — never ships ciphertext as a secret", async () => {
const { decryptAgentEnvVars } = await import(
"./agent-env-crypto.ts?actual"
);
const stored = await patchEnv({ ANTHROPIC_API_KEY: SECRET });
const corrupted = `${stored.ANTHROPIC_API_KEY.slice(0, -6)}AAAAAA`;
expect(
decryptAgentEnvVars({ ANTHROPIC_API_KEY: corrupted }),
).rejects.toThrow(/ANTHROPIC_API_KEY/);
});

test("sensitivity classifier: BYO secrets in, platform tokens and plain config out", async () => {
const { isSensitiveAgentEnvKey } = await import(
"./agent-env-crypto.ts?actual"
);
// user BYO secrets — encrypted
expect(isSensitiveAgentEnvKey("ANTHROPIC_API_KEY")).toBe(true);
expect(isSensitiveAgentEnvKey("OPENAI_API_KEY")).toBe(true);
expect(isSensitiveAgentEnvKey("GITHUB_TOKEN")).toBe(true);
expect(isSensitiveAgentEnvKey("MY_DB_PASSWORD")).toBe(true);
expect(isSensitiveAgentEnvKey("AGENT_SERVER_SHARED_SECRET")).toBe(true);
// platform-managed control-plane tokens — never encrypted
expect(isSensitiveAgentEnvKey("ELIZA_API_TOKEN")).toBe(false);
expect(isSensitiveAgentEnvKey("ELIZAOS_API_KEY")).toBe(false);
expect(isSensitiveAgentEnvKey("ELIZAOS_CLOUD_API_KEY")).toBe(false);
expect(isSensitiveAgentEnvKey("DATABASE_URL")).toBe(false);
expect(isSensitiveAgentEnvKey("STEWARD_AGENT_TOKEN")).toBe(false);
// plain config — untouched
expect(isSensitiveAgentEnvKey("ELIZA_PLUGIN_SET")).toBe(false);
expect(isSensitiveAgentEnvKey("ELIZA_ALLOWED_ORIGINS")).toBe(false);
expect(isSensitiveAgentEnvKey("POSTGRES_POOL_MAX")).toBe(false);
});
});
134 changes: 134 additions & 0 deletions packages/cloud/shared/src/lib/services/agent-env-crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* At-rest encryption for `agent_sandboxes.environment_vars` (#11332).
*
* The column is plain jsonb, and it is the one place users can land BYO
* provider keys today (PATCH /v1/eliza/agents/:id/environment, agent create,
* coding-container create). Without this layer those secrets sit in plaintext
* at rest. Values whose key looks secret-bearing are encrypted on WRITE with
* the EXISTING org-scoped envelope crypto (`FieldEncryptionService`:
* AES-256-GCM, per-org DEK wrapped by `SECRETS_MASTER_KEY`, `enc:v1:` encoded
* strings — the same primitive that already protects tenant DB DSNs) and
* decrypted only at the points the env is materialized for the agent (container
* create, fleet upgrade, runtime bootstrap), so the running agent still sees
* the real values.
*
* Backward compatible by construction:
* - Decrypt passes any non-`enc:v1:` value through untouched, so legacy
* plaintext rows keep working with no forced backfill. Legacy plaintext
* secrets are opportunistically re-encrypted the next time the row's env is
* written through the service.
* - When `SECRETS_MASTER_KEY` is not configured, writes stay plaintext (exact
* legacy behavior) with a structured warning, so environments without the
* key (local dev, self-hosters) do not break. To activate, configure the
* SAME key on the cloud API Worker and the provisioning daemon — the same
* deployment requirement tenant-DB DSN encryption (`user-database.ts`)
* already imposes.
*
* Platform-managed control-plane tokens (`RESERVED_PLATFORM_ENV_KEYS` plus the
* legacy `ELIZAOS_API_KEY` alias) are NEVER encrypted: the control plane reads
* them synchronously outside the materialization path (bridge auth headers,
* the dedicated-agent proxy, pairing routes), and they are minted and owned by
* the platform — they are not user BYO secrets.
*/

import { logger } from "../utils/logger";
import { fieldEncryption } from "./field-encryption";
import { RESERVED_PLATFORM_ENV_KEYS } from "./reserved-env-keys";

/**
* Key-name heuristic for secret-bearing env vars (ANTHROPIC_API_KEY,
* OPENAI_API_KEY, GITHUB_TOKEN, AGENT_SERVER_SHARED_SECRET, ...). Deliberately
* broad: a false positive only costs an encrypt/decrypt round-trip through the
* materialization path; a false negative leaves a secret in plaintext.
*/
const SENSITIVE_ENV_KEY_PATTERN =
/(KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIAL|PRIVATE)/i;

/**
* Platform tokens the control plane must read synchronously from the stored
* row (bridge auth, proxies, pairing) — never encrypted. All of them are
* blocked from the user PATCH surface by the reserved-key gate except
* `ELIZAOS_API_KEY`, the legacy bridge-token alias `getAgentApiToken` falls
* back to.
*/
const NEVER_ENCRYPT_ENV_KEYS: ReadonlySet<string> = new Set(
[...RESERVED_PLATFORM_ENV_KEYS, "ELIZAOS_API_KEY"].map((key) =>
key.toUpperCase(),
),
);

/** Whether a caller-supplied env key should be encrypted at rest. */
export function isSensitiveAgentEnvKey(key: string): boolean {
if (NEVER_ENCRYPT_ENV_KEYS.has(key.toUpperCase())) return false;
return SENSITIVE_ENV_KEY_PATTERN.test(key);
}

/**
* Encrypt the secret-bearing values of an agent env map for storage in
* `agent_sandboxes.environment_vars`. Non-sensitive config values and
* platform tokens pass through unchanged; values that are already `enc:v1:`
* ciphertext (e.g. a read-modify-write PATCH echoing stored values back) are
* never double-encrypted.
*
* Fail-open ONLY for the key-not-configured case (legacy plaintext behavior,
* loudly logged); any real encryption failure propagates so a secret is never
* silently persisted in plaintext when encryption was expected to work.
*/
export async function encryptAgentEnvVarsForStorage(
organizationId: string,
environmentVars: Record<string, string>,
): Promise<Record<string, string>> {
const pending = Object.entries(environmentVars).filter(
([key, value]) =>
isSensitiveAgentEnvKey(key) &&
typeof value === "string" &&
value.length > 0 &&
!fieldEncryption.isEncrypted(value),
);
if (pending.length === 0) return { ...environmentVars };

// Same source FieldEncryptionService reads (the Worker populates process.env
// from bindings under nodejs_compat). No key -> legacy plaintext, warn loud.
if (!process.env.SECRETS_MASTER_KEY) {
logger.warn(
"[agent-env-crypto] SECRETS_MASTER_KEY not configured — storing agent environment secrets as PLAINTEXT (legacy behavior). Configure the key on the cloud API and provisioning daemon to encrypt at rest.",
{ organizationId, keys: pending.map(([key]) => key) },
);
return { ...environmentVars };
}

const out: Record<string, string> = { ...environmentVars };
for (const [key, value] of pending) {
out[key] = await fieldEncryption.encrypt(organizationId, value);
}
return out;
}

/**
* Materialize a stored agent env map back to real values. `enc:v1:` values are
* decrypted; everything else (legacy plaintext rows, non-sensitive config)
* passes through untouched. Decrypt failures fail CLOSED with the key name —
* handing ciphertext to a container as if it were the secret would be a silent
* misconfiguration.
*/
export async function decryptAgentEnvVars(
environmentVars: Record<string, string> | null | undefined,
): Promise<Record<string, string>> {
const out: Record<string, string> = {};
for (const [key, value] of Object.entries(environmentVars ?? {})) {
if (typeof value === "string" && fieldEncryption.isEncrypted(value)) {
try {
out[key] = await fieldEncryption.decrypt(value);
} catch (error) {
throw new Error(
`Failed to decrypt agent environment variable ${key}: ${
error instanceof Error ? error.message : String(error)
}`,
);
}
} else {
out[key] = value;
}
}
return out;
}
Loading
Loading