Skip to content

Commit 94151d7

Browse files
Strengthen Zero-Knowledge vaults with a per-vault PBKDF2 salt
Each ZK vault now gets its own random salt (generated server-side on creation, stored on the folder, served to the client). The browser derives the vault key from the passphrase + that salt, so passphrase precomputation can no longer be shared across vaults. Backward compatible: vaults without a salt fall back to the previous fixed salt and keep unlocking. Test: vaults get distinct salts, normal folders get none. Suite now 37 API tests. Docs (security, threat model) and checklist updated.
1 parent c1d19ef commit 94151d7

11 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎TEST-CHECKLIST.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,11 @@ were hardened.
131131
- [ ] **Direct API access blocked**: when `API_HOST=127.0.0.1`, the API port is not
132132
reachable from outside the host — only nginx can reach it.
133133

134+
## Stronger Zero-Knowledge vaults (lot 5)
135+
136+
- [ ] **Per-vault salt**: create two vaults → each gets a distinct salt (each derives an
137+
independent key from the same passphrase). Upload/download in a new vault still works.
138+
- [ ] **Backward compatible**: a vault created before this change (no salt) still unlocks
139+
with its passphrase (uses the legacy salt). New vaults use a random per-vault salt.
140+
134141
<!-- New features are appended below as they are built. -->
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "Folder" ADD COLUMN "zkSalt" TEXT;

‎apps/api/prisma/schema.prisma‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ model Folder {
110110
children Folder[] @relation("FolderChildren")
111111
name String
112112
isZeroKnowledge Boolean @default(false)
113+
// Per-vault random salt for the client's PBKDF2 key derivation (ZK folders only).
114+
// Not secret; storing it per vault defeats cross-vault precomputation. null = legacy.
115+
zkSalt String?
113116
createdAt DateTime @default(now())
114117
115118
files FileObject[]

‎apps/api/src/lib/serialize.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function toPublicFolder(f: Folder): PublicFolder {
3838
name: f.name,
3939
parentId: f.parentId,
4040
isZeroKnowledge: f.isZeroKnowledge,
41+
zkSalt: f.zkSalt,
4142
createdAt: f.createdAt.toISOString(),
4243
};
4344
}

‎apps/api/src/routes/folders.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { FastifyPluginAsync } from 'fastify';
2-
import { createFolderSchema, updateFolderSchema } from '@opencoperlock/shared';
2+
import { createFolderSchema, randomToken, updateFolderSchema } from '@opencoperlock/shared';
33
import { prisma } from '../db.js';
44
import { parseOr400 } from '../lib/validate.js';
55
import { toPublicFolder } from '../lib/serialize.js';
@@ -43,13 +43,16 @@ export const folderRoutes: FastifyPluginAsync = async (app) => {
4343
if (!parent) return reply.code(404).send({ error: 'Parent folder not found' });
4444
}
4545

46+
const isZk = body.isZeroKnowledge ?? false;
4647
try {
4748
const folder = await prisma.folder.create({
4849
data: {
4950
ownerId: req.user!.id,
5051
parentId: body.parentId ?? null,
5152
name: body.name,
52-
isZeroKnowledge: body.isZeroKnowledge ?? false,
53+
isZeroKnowledge: isZk,
54+
// Fresh per-vault salt so each vault's passphrase derivation is independent.
55+
zkSalt: isZk ? randomToken(16) : null,
5356
},
5457
});
5558
await audit(req, 'folder.create', { target: folder.id });

‎apps/api/test/integration.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ runIf('API integration', () => {
222222
expect(ok.statusCode).toBe(200);
223223
expect(ok.json().folder.name).toBe('top');
224224
});
225+
226+
it('gives each vault its own ZK salt and normal folders none', async () => {
227+
await createUser({ email: 'vs2@test.local', password: 'correct-horse-battery' });
228+
const auth = await login(app, 'vs2@test.local', 'correct-horse-battery');
229+
const normal = await app.inject({ method: 'POST', url: '/folders', headers: authHeaders(auth), payload: { name: 'plain' } });
230+
expect(normal.json().folder.zkSalt).toBeNull();
231+
const v1 = await app.inject({ method: 'POST', url: '/folders', headers: authHeaders(auth), payload: { name: 'vaultA', isZeroKnowledge: true } });
232+
const v2 = await app.inject({ method: 'POST', url: '/folders', headers: authHeaders(auth), payload: { name: 'vaultB', isZeroKnowledge: true } });
233+
expect(typeof v1.json().folder.zkSalt).toBe('string');
234+
expect(v1.json().folder.zkSalt).not.toBe(v2.json().folder.zkSalt); // unique per vault
235+
});
225236
});
226237

227238
describe('health', () => {

‎apps/web/src/app/(app)/vault/page.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export default function VaultPage() {
5454
if (!passphrase) return;
5555
setError(null);
5656
try {
57-
const key = await deriveVaultKey(passphrase);
57+
const vault = vaults.find((v) => v.id === folderId);
58+
const key = await deriveVaultKey(passphrase, vault?.zkSalt ?? null);
5859
setVaultKey(key);
5960
setActiveVault(folderId);
6061
await openVault(folderId, key);

‎apps/web/src/lib/zk.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
*/
1414

1515
const PBKDF2_ITERATIONS = 210_000; // OWASP-recommended floor for PBKDF2-SHA256
16-
const SALT = 'opencoperlock.zk.v1'; // domain-separation salt (per-vault salt is future work)
16+
// Legacy fixed salt for vaults created before per-vault salts existed (zkSalt === null).
17+
const LEGACY_SALT = 'opencoperlock.zk.v1';
1718

1819
const enc = new TextEncoder();
1920
const dec = new TextDecoder();
@@ -30,8 +31,11 @@ function fromB64(b64: string): Uint8Array<ArrayBuffer> {
3031
return view;
3132
}
3233

33-
/** Derive a non-extractable AES-GCM vault key from the user's passphrase. */
34-
export async function deriveVaultKey(passphrase: string): Promise<CryptoKey> {
34+
/**
35+
* Derive a non-extractable AES-GCM vault key from the user's passphrase and the vault's
36+
* per-vault salt (or the legacy fixed salt for older vaults where `zkSalt` is null).
37+
*/
38+
export async function deriveVaultKey(passphrase: string, zkSalt: string | null): Promise<CryptoKey> {
3539
const baseKey = await crypto.subtle.importKey(
3640
'raw',
3741
enc.encode(passphrase),
@@ -40,7 +44,12 @@ export async function deriveVaultKey(passphrase: string): Promise<CryptoKey> {
4044
['deriveKey'],
4145
);
4246
return crypto.subtle.deriveKey(
43-
{ name: 'PBKDF2', salt: enc.encode(SALT), iterations: PBKDF2_ITERATIONS, hash: 'SHA-256' },
47+
{
48+
name: 'PBKDF2',
49+
salt: enc.encode(zkSalt ?? LEGACY_SALT),
50+
iterations: PBKDF2_ITERATIONS,
51+
hash: 'SHA-256',
52+
},
4453
baseKey,
4554
{ name: 'AES-GCM', length: 256 },
4655
false,

‎docs/SECURITY.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ the strongest separation, store it in a secrets manager and inject it at runtime
3333
Files placed in a vault folder are encrypted **in the browser** with the Web Crypto API
3434
before they ever leave the device:
3535

36-
- a **vault key** is derived from your passphrase with **PBKDF2-SHA256** (≥210 000 iterations),
36+
- a **vault key** is derived from your passphrase with **PBKDF2-SHA256** (≥210 000
37+
iterations) using a **random per-vault salt** (stored server-side; not secret),
3738
- each file gets a random AES-256-GCM data key that encrypts the content **and** the filename,
3839
- that data key is **wrapped with the vault key**.
3940

‎docs/THREAT_MODEL.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ software that overpromises is worse than software that is clear about its bounda
5050
a tampered/truncated blob surfaces as a stream error at the end of a download rather than
5151
before the first byte. Storage integrity is the first line of defence.
5252
- **ZK metadata.** Vault file *sizes* and *counts* are visible to the server (only the
53-
content and filenames are encrypted). Per-vault salts and padded sizes are future work.
53+
content and filenames are encrypted). Each vault now uses its own random PBKDF2 salt, so
54+
passphrase precomputation can't be shared across vaults; padded sizes remain future work.
5455
- **Lost ZK passphrase.** By design, there is no recovery. The operator cannot help.
5556
- **VirusTotal hash submission** reveals a file's SHA-256 to a third party when invoked.
5657
It is opt-in and never uploads file contents.

0 commit comments

Comments
 (0)