Implement keep: self-hosted E2E encrypted secrets sync

Server: Express + better-sqlite3 (WAL), multi-recipient key-wrapping
per IMPLEMENTATION.md's design — vaults/recipients/vault_grants/
access_log. Two auth paths: ADMIN_PASSWORD header for recipient
management and revoke (pure metadata operations), signed-request
auth (Ed25519 signature over method+path+timestamp+body-hash) for
push/pull/grant, mirroring the spirit of this project family's other
signed-handshake patterns without naming them.

CLI: identity init/show/set-id, push/pull/grant/log, admin recipient
add/list/remove and revoke. Grant is a client-side crypto operation
(the granter unwraps the vault's current key locally and reseals it
for the new recipient) rather than a server-side operation, since the
server never holds an unwrapped key to grant with.

Verified end-to-end with two independent local identities against a
live server and separately against the built Docker image: register,
push, pull (granted and ungranted), grant without re-pushing, admin
revoke, a subsequent rotation confirming the revoked recipient stays
excluded, and rejection of missing/malformed signed-request auth.

Two real bugs caught during verification, not just written up:
- libsodium-wrappers' published ESM build does a relative import only
  resolvable under bundler-style resolution — broken under plain Node
  ESM. Fixed via createRequire to force the CJS build.
- Express's req.path inside a sub-router is relative to the mount
  point, which would have silently mismatched a client signing the
  full request path. Fixed by verifying against req.originalUrl.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Fredrik Johansson
2026-07-12 19:38:24 +02:00
parent 2c34562c7f
commit 67000b66ee
27 changed files with 3343 additions and 106 deletions

47
src/server/adminRoutes.ts Normal file
View File

@@ -0,0 +1,47 @@
import { Router } from 'express';
import { randomBytes } from 'node:crypto';
import { listRecipients, createRecipient, deleteRecipient, deleteGrant, getAccessLog, logAccess } from './db.js';
import { requireAdminAuth } from './auth.js';
export const router = Router();
router.use(requireAdminAuth);
function newRecipientId(label: string): string {
const slug = label.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 24);
const suffix = randomBytes(3).toString('hex');
return slug ? `${slug}-${suffix}` : suffix;
}
router.get('/recipients', (_req, res) => {
res.json(listRecipients().map(r => ({ recipientId: r.recipient_id, label: r.label, publicKey: r.public_key, createdAt: r.created_at })));
});
router.post('/recipients', (req, res) => {
const body = req.body as { label?: string; publicKey?: string };
if (!body.label || !body.publicKey) {
res.status(400).json({ error: 'label and publicKey are required' });
return;
}
const recipientId = newRecipientId(body.label);
createRecipient(recipientId, body.label, body.publicKey);
res.json({ recipientId });
});
router.delete('/recipients/:id', (req, res) => {
deleteRecipient(req.params.id);
res.json({ ok: true });
});
// Revocation is pure metadata deletion — no crypto material is touched,
// which is exactly why admin alone (no vault access of their own
// required) can perform it, unlike `grant`.
router.delete('/vaults/:key/grants/:recipientId', (req, res) => {
deleteGrant(req.params.key, req.params.recipientId);
logAccess(req.params.key, req.params.recipientId, 'revoke');
res.json({ ok: true });
});
router.get('/vaults/:key/log', (req, res) => {
res.json(getAccessLog(req.params.key));
});

77
src/server/auth.ts Normal file
View File

@@ -0,0 +1,77 @@
// Signed-request authentication for recipient-facing endpoints
// (push/pull/grant/log) — distinct from the ADMIN_PASSWORD header used by
// admin-only routes (recipient add/remove, revoke). A recipient proves
// their identity by signing a canonical string derived from the request
// itself; no session, no cookie, stateless like every other password/
// signature check in this project family.
import type { Request, Response, NextFunction } from 'express';
import { getRecipient } from './db.js';
import { Identity, signingString } from '../shared/crypto.js';
import { config } from './config.js';
export interface AuthedRequest extends Request {
recipientId?: string;
rawBody?: Buffer;
}
export async function requireRecipientAuth(req: AuthedRequest, res: Response, next: NextFunction): Promise<void> {
const recipientId = req.get('X-Recipient-Id');
const signature = req.get('X-Signature');
const timestampHeader = req.get('X-Timestamp');
if (!recipientId || !signature || !timestampHeader) {
res.status(401).json({ error: 'missing auth headers' });
return;
}
const timestamp = Number(timestampHeader);
if (!Number.isFinite(timestamp) || Math.abs(Date.now() / 1000 - timestamp) > config.requestMaxSkewSeconds) {
res.status(401).json({ error: 'timestamp out of range' });
return;
}
const recipient = getRecipient(recipientId);
if (!recipient) {
res.status(401).json({ error: 'unknown recipient' });
return;
}
// req.path is relative to whichever router mounted this middleware
// (e.g. "/vaults/x/pull" inside a router mounted at "/api"), which would
// silently disagree with a client that signs the full request path.
// req.originalUrl is always the full path as actually requested,
// regardless of router nesting — strip the query string, since the
// client doesn't sign one.
const fullPath = req.originalUrl.split('?')[0];
const body = req.rawBody ?? Buffer.alloc(0);
const expected = signingString(req.method, fullPath, timestamp, body);
let sigBytes: Uint8Array;
try {
sigBytes = Buffer.from(signature, 'hex');
} catch {
res.status(401).json({ error: 'bad signature encoding' });
return;
}
const ok = Identity.verify(recipient.public_key, Buffer.from(expected, 'utf8'), sigBytes);
if (!ok) {
res.status(401).json({ error: 'signature verification failed' });
return;
}
req.recipientId = recipientId;
next();
}
export function requireAdminAuth(req: Request, res: Response, next: NextFunction): void {
if (!config.adminPassword) {
res.status(503).json({ error: 'admin disabled — ADMIN_PASSWORD not set' });
return;
}
if (req.get('X-Admin-Password') !== config.adminPassword) {
res.status(401).json({ error: 'bad password' });
return;
}
next();
}

12
src/server/config.ts Normal file
View File

@@ -0,0 +1,12 @@
import path from 'node:path';
export const config = {
port: parseInt(process.env.PORT ?? '3050', 10),
dataDir: process.env.DATA_DIR ?? path.resolve('data'),
// Gates recipient management + revoke. Unset disables admin routes
// entirely (503) rather than refusing to start — push/pull/grant for
// already-registered recipients keep working regardless.
adminPassword: process.env.ADMIN_PASSWORD || null,
// Replay-protection window for signed requests.
requestMaxSkewSeconds: parseInt(process.env.REQUEST_MAX_SKEW_SECONDS ?? '300', 10),
};

159
src/server/db.ts Normal file
View File

@@ -0,0 +1,159 @@
import Database from 'better-sqlite3';
import fs from 'node:fs';
import path from 'node:path';
import { config } from './config.js';
fs.mkdirSync(config.dataDir, { recursive: true });
export const db = new Database(path.join(config.dataDir, 'keep.db'));
db.pragma('journal_mode = WAL');
db.exec(`
CREATE TABLE IF NOT EXISTS vaults (
vault_key TEXT PRIMARY KEY,
ciphertext BLOB NOT NULL,
nonce BLOB NOT NULL,
updated_at INTEGER NOT NULL,
updated_by TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS recipients (
recipient_id TEXT PRIMARY KEY,
label TEXT NOT NULL,
public_key TEXT NOT NULL,
created_at INTEGER NOT NULL
);
-- Deleting a row IS the revocation for that (vault, recipient) pair.
CREATE TABLE IF NOT EXISTS vault_grants (
vault_key TEXT NOT NULL,
recipient_id TEXT NOT NULL,
wrapped_key TEXT NOT NULL, -- base64 crypto_box_seal(vault_symmetric_key, recipient_pubkey)
granted_at INTEGER NOT NULL,
PRIMARY KEY (vault_key, recipient_id)
);
CREATE TABLE IF NOT EXISTS access_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
vault_key TEXT NOT NULL,
recipient_id TEXT NOT NULL,
action TEXT NOT NULL,
accessed_at INTEGER NOT NULL
);
`);
export interface VaultRow {
vault_key: string;
ciphertext: Buffer;
nonce: Buffer;
updated_at: number;
updated_by: string;
}
export interface RecipientRow {
recipient_id: string;
label: string;
public_key: string;
created_at: number;
}
export interface GrantRow {
vault_key: string;
recipient_id: string;
wrapped_key: string;
granted_at: number;
}
export function getVault(vaultKey: string): VaultRow | undefined {
return db.prepare(`SELECT * FROM vaults WHERE vault_key = ?`).get(vaultKey) as VaultRow | undefined;
}
export function upsertVault(vaultKey: string, ciphertext: Buffer, nonce: Buffer, updatedBy: string): void {
db.prepare(
`INSERT INTO vaults (vault_key, ciphertext, nonce, updated_at, updated_by)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(vault_key) DO UPDATE SET
ciphertext = excluded.ciphertext, nonce = excluded.nonce,
updated_at = excluded.updated_at, updated_by = excluded.updated_by`,
).run(vaultKey, ciphertext, nonce, Math.floor(Date.now() / 1000), updatedBy);
}
export function getRecipient(recipientId: string): RecipientRow | undefined {
return db.prepare(`SELECT * FROM recipients WHERE recipient_id = ?`).get(recipientId) as RecipientRow | undefined;
}
export function listRecipients(): RecipientRow[] {
return db.prepare(`SELECT * FROM recipients ORDER BY created_at ASC`).all() as RecipientRow[];
}
export function createRecipient(recipientId: string, label: string, publicKey: string): void {
db.prepare(
`INSERT INTO recipients (recipient_id, label, public_key, created_at) VALUES (?, ?, ?, ?)`,
).run(recipientId, label, publicKey, Math.floor(Date.now() / 1000));
}
export function deleteRecipient(recipientId: string): void {
const tx = db.transaction(() => {
db.prepare(`DELETE FROM recipients WHERE recipient_id = ?`).run(recipientId);
db.prepare(`DELETE FROM vault_grants WHERE recipient_id = ?`).run(recipientId);
});
tx();
}
export function hasGrant(vaultKey: string, recipientId: string): boolean {
const row = db.prepare(
`SELECT 1 FROM vault_grants WHERE vault_key = ? AND recipient_id = ?`,
).get(vaultKey, recipientId);
return row != null;
}
export function getGrant(vaultKey: string, recipientId: string): GrantRow | undefined {
return db.prepare(
`SELECT * FROM vault_grants WHERE vault_key = ? AND recipient_id = ?`,
).get(vaultKey, recipientId) as GrantRow | undefined;
}
export function listGrantsForVault(vaultKey: string): GrantRow[] {
return db.prepare(`SELECT * FROM vault_grants WHERE vault_key = ?`).all(vaultKey) as GrantRow[];
}
export function setGrant(vaultKey: string, recipientId: string, wrappedKeyB64: string): void {
db.prepare(
`INSERT INTO vault_grants (vault_key, recipient_id, wrapped_key, granted_at)
VALUES (?, ?, ?, ?)
ON CONFLICT(vault_key, recipient_id) DO UPDATE SET wrapped_key = excluded.wrapped_key, granted_at = excluded.granted_at`,
).run(vaultKey, recipientId, wrappedKeyB64, Math.floor(Date.now() / 1000));
}
// Used by `push` (rotation): replaces every wrapped-key row for a vault
// in one transaction, so a concurrent grant/revoke can't interleave with
// a partial rewrap and leave the vault in a mixed old/new-key state.
export function replaceAllGrantsForVault(vaultKey: string, wrappedByRecipientId: Map<string, string>): void {
const tx = db.transaction(() => {
db.prepare(`DELETE FROM vault_grants WHERE vault_key = ?`).run(vaultKey);
const insert = db.prepare(
`INSERT INTO vault_grants (vault_key, recipient_id, wrapped_key, granted_at) VALUES (?, ?, ?, ?)`,
);
const now = Math.floor(Date.now() / 1000);
for (const [recipientId, wrappedKeyB64] of wrappedByRecipientId) {
insert.run(vaultKey, recipientId, wrappedKeyB64, now);
}
});
tx();
}
export function deleteGrant(vaultKey: string, recipientId: string): void {
db.prepare(`DELETE FROM vault_grants WHERE vault_key = ? AND recipient_id = ?`).run(vaultKey, recipientId);
}
export function logAccess(vaultKey: string, recipientId: string, action: 'pull' | 'push' | 'grant' | 'revoke'): void {
db.prepare(
`INSERT INTO access_log (vault_key, recipient_id, action, accessed_at) VALUES (?, ?, ?, ?)`,
).run(vaultKey, recipientId, action, Math.floor(Date.now() / 1000));
}
export function getAccessLog(vaultKey: string, limit = 50): { recipient_id: string; action: string; accessed_at: number }[] {
return db.prepare(
`SELECT recipient_id, action, accessed_at FROM access_log WHERE vault_key = ? ORDER BY accessed_at DESC LIMIT ?`,
).all(vaultKey, limit) as { recipient_id: string; action: string; accessed_at: number }[];
}

34
src/server/index.ts Normal file
View File

@@ -0,0 +1,34 @@
import express from 'express';
import { config } from './config.js';
import { router as apiRouter } from './routes.js';
import { router as adminRouter } from './adminRoutes.js';
import type { AuthedRequest } from './auth.js';
const app = express();
// Capture the raw body bytes alongside the parsed JSON — signature
// verification needs to hash exactly what was sent, not a re-serialized
// version of the parsed object (which could differ in key order/spacing
// and produce a different hash than what the client signed).
app.use(express.json({
verify: (req, _res, buf) => {
(req as AuthedRequest).rawBody = Buffer.from(buf);
},
}));
// Must be registered before apiRouter — that router applies
// requireRecipientAuth to every path under /api via router.use(), which
// would otherwise swallow this unauthenticated health check too, since
// Express matches route registration order and app.use('/api', ...)
// matches /api/health as a prefix.
app.get('/api/health', (_req, res) => res.json({ ok: true }));
app.use('/api/admin', adminRouter);
app.use('/api', apiRouter);
app.listen(config.port, () => {
console.log(`[keep] listening on :${config.port}`);
if (!config.adminPassword) {
console.log('[keep] ADMIN_PASSWORD not set — /api/admin is disabled');
}
});

135
src/server/routes.ts Normal file
View File

@@ -0,0 +1,135 @@
import { Router } from 'express';
import {
getVault,
upsertVault,
hasGrant,
getGrant,
listGrantsForVault,
setGrant,
replaceAllGrantsForVault,
logAccess,
getAccessLog,
listRecipients,
} from './db.js';
import { requireRecipientAuth, type AuthedRequest } from './auth.js';
export const router = Router();
router.use(requireRecipientAuth);
// Recipient public keys aren't secret — knowing one doesn't grant access
// to anything — so this is recipient-authenticated (proves you're a
// registered identity) rather than admin-gated. Needed so a recipient can
// look up who else exists before granting them access to a vault.
router.get('/recipients', (_req, res) => {
res.json(listRecipients().map(r => ({ recipientId: r.recipient_id, label: r.label, publicKey: r.public_key })));
});
// Existence isn't secret — lets `keep push` distinguish "vault doesn't
// exist yet, fine to self-bootstrap" from "vault exists but I have no
// grant," which a bare 403 on /recipients can't tell apart.
router.get('/vaults/:key/exists', (req, res) => {
res.json({ exists: getVault(req.params.key) != null });
});
// Only current grantees of a vault can see who else has access to it —
// needed by `keep push` to know who to re-wrap the rotated key for.
router.get('/vaults/:key/recipients', (req: AuthedRequest, res) => {
const vaultKey = req.params.key;
if (!hasGrant(vaultKey, req.recipientId!)) {
res.status(403).json({ error: 'no access to this vault' });
return;
}
const grants = listGrantsForVault(vaultKey);
const byId = new Map(listRecipients().map(r => [r.recipient_id, r]));
res.json(grants.map(g => {
const r = byId.get(g.recipient_id);
return { recipientId: g.recipient_id, label: r?.label ?? null, publicKey: r?.public_key ?? null };
}));
});
router.get('/vaults/:key/pull', (req: AuthedRequest, res) => {
const vaultKey = req.params.key;
const recipientId = req.recipientId!;
const grant = getGrant(vaultKey, recipientId);
const vault = getVault(vaultKey);
if (!grant || !vault) {
logAccess(vaultKey, recipientId, 'pull'); // logged even on failure — a spike of failed pulls from an unexpected identity is exactly the signal this exists to surface
res.status(404).json({ error: 'no vault or no access' });
return;
}
logAccess(vaultKey, recipientId, 'pull');
res.json({
ciphertext: vault.ciphertext.toString('base64'),
nonce: vault.nonce.toString('base64'),
wrappedKey: grant.wrapped_key,
updatedAt: vault.updated_at,
updatedBy: vault.updated_by,
});
});
// Rotation: replaces the vault's ciphertext AND every recipient's wrapped
// key in one call — the client has already generated a fresh symmetric
// key and re-wrapped it for every currently-granted recipient (fetched
// via GET /vaults/:key/recipients first). Existing vaults require the
// pusher to already be a grantee (read implies write, v1 decision from
// IMPLEMENTATION.md); a brand-new vault key may be created by any
// registered recipient, who becomes its first grantee.
router.post('/vaults/:key/push', (req: AuthedRequest, res) => {
const vaultKey = req.params.key;
const recipientId = req.recipientId!;
const existing = getVault(vaultKey);
if (existing && !hasGrant(vaultKey, recipientId)) {
res.status(403).json({ error: 'no access to this vault' });
return;
}
const body = req.body as { ciphertext?: string; nonce?: string; wrappedKeys?: Record<string, string> };
if (!body.ciphertext || !body.nonce || !body.wrappedKeys || Object.keys(body.wrappedKeys).length === 0) {
res.status(400).json({ error: 'ciphertext, nonce, and at least one wrapped key are required' });
return;
}
upsertVault(vaultKey, Buffer.from(body.ciphertext, 'base64'), Buffer.from(body.nonce, 'base64'), recipientId);
replaceAllGrantsForVault(vaultKey, new Map(Object.entries(body.wrappedKeys)));
logAccess(vaultKey, recipientId, 'push');
res.json({ ok: true });
});
// Adds ONE new recipient to an existing vault using the CURRENT
// (unrotated) symmetric key — the granter must already hold a grant
// (meaning they can unwrap the current key locally) and supplies a fresh
// seal of that same key for the new recipient's public key. Does not
// touch the ciphertext or any other recipient's wrapped key.
router.post('/vaults/:key/grant', (req: AuthedRequest, res) => {
const vaultKey = req.params.key;
const granterId = req.recipientId!;
if (!hasGrant(vaultKey, granterId)) {
res.status(403).json({ error: 'no access to this vault' });
return;
}
const body = req.body as { recipientId?: string; wrappedKey?: string };
if (!body.recipientId || !body.wrappedKey) {
res.status(400).json({ error: 'recipientId and wrappedKey are required' });
return;
}
setGrant(vaultKey, body.recipientId, body.wrappedKey);
logAccess(vaultKey, granterId, 'grant');
res.json({ ok: true });
});
router.get('/vaults/:key/log', (req: AuthedRequest, res) => {
const vaultKey = req.params.key;
if (!hasGrant(vaultKey, req.recipientId!)) {
res.status(403).json({ error: 'no access to this vault' });
return;
}
res.json(getAccessLog(vaultKey));
});