release(v4.0.0): Shade GA — V3.x consolidation + audit prep
Some checks failed
Test / test (push) Has been cancelled
Cross-platform vectors / TypeScript vectors (bun) (push) Has been cancelled
Cross-platform vectors / Kotlin vectors (gradle) (push) Has been cancelled
Docker build and publish / docker (push) Has been cancelled
Publish / publish (push) Has been cancelled
Some checks failed
Test / test (push) Has been cancelled
Cross-platform vectors / TypeScript vectors (bun) (push) Has been cancelled
Cross-platform vectors / Kotlin vectors (gradle) (push) Has been cancelled
Docker build and publish / docker (push) Has been cancelled
Publish / publish (push) Has been cancelled
V3.1 → V3.12 consolidated and tagged for the first GA release. Wire format unchanged from 0.4.x — 4.0 peers interoperate with 0.4.x peers byte-for-byte. The version bump is semantic: audit-cycle complete, opt-in surface fully exposed, threat model refreshed for every new surface. Highlights: - All 24 @shade/* packages bumped to 4.0.0 in lockstep. - CHANGELOG 4.0.0 section is the canonical manifest of what landed. - THREAT-MODEL extended (§10 fingerprint gates, §11 WebRTC P2P, §12 Web-Worker boundary) + residual-risks table refreshed. - OpenAPI now covers all 27 routes: prekey, transfer, KT, inbox, bridge, observer, /metrics, /healthz, /ready. - MIGRATION 0.3.x → 4.0 documented + smoke-tested against shade migrate-storage on a real SQLite DB. - docs/audit/REVIEW-BUNDLE.md + SCOPE.md ready for external reviewer. - scripts/soak.ts harness for the GA-stable 2-week soak window. - All V*.md plans archived under docs/archive/ with Status: Done. - Voice/Video carved out into V5.0; 4.0 audit focuses on the frozen non-realtime stack. Tests: TS 1000/1000 + Kotlin 11/11 cross-platform vectors green. Docker: gt.zyon.no/stian/shade-prekey:4.0.0 builds and reports version 4.0.0 on /health. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export { SQLiteStorage } from './sqlite-storage.js';
|
||||
export { SqlitePrekeyStore } from './sqlite-prekey-store.js';
|
||||
export { SqliteInboxStore } from './sqlite-inbox-store.js';
|
||||
|
||||
188
packages/shade-storage-sqlite/src/sqlite-inbox-store.ts
Normal file
188
packages/shade-storage-sqlite/src/sqlite-inbox-store.ts
Normal file
@@ -0,0 +1,188 @@
|
||||
import { Database } from 'bun:sqlite';
|
||||
import type { InboxStore } from '@shade/inbox-server';
|
||||
import { toBase64, fromBase64 } from '@shade/core';
|
||||
|
||||
/**
|
||||
* SQLite-backed InboxStore for the Shade Inbox Server (V3.6).
|
||||
*
|
||||
* Stores ciphertext blobs keyed by (address, msgId) with an
|
||||
* (address, expires_at) index so prune scans don't need a full table walk.
|
||||
*
|
||||
* Docker usage:
|
||||
* Volume mount /data, set SHADE_INBOX_DB_PATH=/data/shade-inbox.db
|
||||
*/
|
||||
export class SqliteInboxStore implements InboxStore {
|
||||
private db: Database;
|
||||
|
||||
private stmts!: {
|
||||
saveOwner: ReturnType<Database['prepare']>;
|
||||
getOwner: ReturnType<Database['prepare']>;
|
||||
deleteOwner: ReturnType<Database['prepare']>;
|
||||
deleteOwnerBlobs: ReturnType<Database['prepare']>;
|
||||
insertBlob: ReturnType<Database['prepare']>;
|
||||
findBlob: ReturnType<Database['prepare']>;
|
||||
fetchSince: ReturnType<Database['prepare']>;
|
||||
deleteBlob: ReturnType<Database['prepare']>;
|
||||
countBlobs: ReturnType<Database['prepare']>;
|
||||
purgeExpired: ReturnType<Database['prepare']>;
|
||||
nextSeq: ReturnType<Database['prepare']>;
|
||||
};
|
||||
|
||||
// Monotonic in-process sequence for receivedAt — guarantees a strict
|
||||
// ordering even when many writes land in the same millisecond.
|
||||
private seq = 0;
|
||||
|
||||
constructor(dbPath?: string) {
|
||||
const path = dbPath ?? process.env.SHADE_INBOX_DB_PATH ?? '/data/shade-inbox.db';
|
||||
this.db = new Database(path, { create: true });
|
||||
this.db.exec('PRAGMA journal_mode=WAL');
|
||||
this.ensureTables();
|
||||
this.prepareStatements();
|
||||
this.bootstrapSeq();
|
||||
}
|
||||
|
||||
private ensureTables() {
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS inbox_owners (
|
||||
address TEXT PRIMARY KEY,
|
||||
signing_key TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS inbox_blobs (
|
||||
address TEXT NOT NULL,
|
||||
msg_id TEXT NOT NULL,
|
||||
ciphertext TEXT NOT NULL,
|
||||
received_at INTEGER NOT NULL,
|
||||
expires_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (address, msg_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_inbox_addr_expires
|
||||
ON inbox_blobs(address, expires_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_inbox_addr_received
|
||||
ON inbox_blobs(address, received_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_inbox_expires
|
||||
ON inbox_blobs(expires_at);
|
||||
`);
|
||||
}
|
||||
|
||||
private prepareStatements() {
|
||||
this.stmts = {
|
||||
saveOwner: this.db.prepare(
|
||||
'INSERT OR REPLACE INTO inbox_owners (address, signing_key) VALUES (?, ?)',
|
||||
),
|
||||
getOwner: this.db.prepare('SELECT signing_key FROM inbox_owners WHERE address = ?'),
|
||||
deleteOwner: this.db.prepare('DELETE FROM inbox_owners WHERE address = ?'),
|
||||
deleteOwnerBlobs: this.db.prepare('DELETE FROM inbox_blobs WHERE address = ?'),
|
||||
insertBlob: this.db.prepare(
|
||||
'INSERT INTO inbox_blobs (address, msg_id, ciphertext, received_at, expires_at) VALUES (?, ?, ?, ?, ?)',
|
||||
),
|
||||
findBlob: this.db.prepare(
|
||||
'SELECT received_at FROM inbox_blobs WHERE address = ? AND msg_id = ?',
|
||||
),
|
||||
fetchSince: this.db.prepare(
|
||||
`SELECT msg_id, ciphertext, received_at, expires_at
|
||||
FROM inbox_blobs
|
||||
WHERE address = ? AND received_at > ? AND expires_at > ?
|
||||
ORDER BY received_at ASC
|
||||
LIMIT ?`,
|
||||
),
|
||||
deleteBlob: this.db.prepare(
|
||||
'DELETE FROM inbox_blobs WHERE address = ? AND msg_id = ?',
|
||||
),
|
||||
countBlobs: this.db.prepare(
|
||||
'SELECT COUNT(*) AS count FROM inbox_blobs WHERE address = ? AND expires_at > ?',
|
||||
),
|
||||
purgeExpired: this.db.prepare(
|
||||
'DELETE FROM inbox_blobs WHERE expires_at <= ?',
|
||||
),
|
||||
nextSeq: this.db.prepare(
|
||||
'SELECT MAX(received_at) AS max FROM inbox_blobs',
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
private bootstrapSeq() {
|
||||
const row = this.stmts.nextSeq.get() as { max: number | null };
|
||||
this.seq = Math.max(row?.max ?? 0, Date.now());
|
||||
}
|
||||
|
||||
close() {
|
||||
this.db.close();
|
||||
}
|
||||
|
||||
async saveAddressOwner(address: string, signingKey: Uint8Array): Promise<void> {
|
||||
this.stmts.saveOwner.run(address, toBase64(signingKey));
|
||||
}
|
||||
|
||||
async getAddressOwner(address: string): Promise<Uint8Array | null> {
|
||||
const row = this.stmts.getOwner.get(address) as { signing_key: string } | undefined;
|
||||
if (!row) return null;
|
||||
return fromBase64(row.signing_key);
|
||||
}
|
||||
|
||||
async putBlob(args: {
|
||||
address: string;
|
||||
msgId: string;
|
||||
ciphertext: Uint8Array;
|
||||
expiresAt: number;
|
||||
}): Promise<{ created: boolean; receivedAt: number }> {
|
||||
const existing = this.stmts.findBlob.get(args.address, args.msgId) as
|
||||
| { received_at: number }
|
||||
| undefined;
|
||||
if (existing) {
|
||||
return { created: false, receivedAt: existing.received_at };
|
||||
}
|
||||
this.seq = Math.max(this.seq + 1, Date.now());
|
||||
const receivedAt = this.seq;
|
||||
this.stmts.insertBlob.run(
|
||||
args.address,
|
||||
args.msgId,
|
||||
toBase64(args.ciphertext),
|
||||
receivedAt,
|
||||
args.expiresAt,
|
||||
);
|
||||
return { created: true, receivedAt };
|
||||
}
|
||||
|
||||
async fetchBlobs(args: {
|
||||
address: string;
|
||||
sinceCursor: number;
|
||||
now: number;
|
||||
limit: number;
|
||||
}): Promise<Array<{ msgId: string; ciphertext: Uint8Array; receivedAt: number; expiresAt: number }>> {
|
||||
const rows = this.stmts.fetchSince.all(
|
||||
args.address,
|
||||
args.sinceCursor,
|
||||
args.now,
|
||||
args.limit,
|
||||
) as Array<{ msg_id: string; ciphertext: string; received_at: number; expires_at: number }>;
|
||||
return rows.map((r) => ({
|
||||
msgId: r.msg_id,
|
||||
ciphertext: fromBase64(r.ciphertext),
|
||||
receivedAt: r.received_at,
|
||||
expiresAt: r.expires_at,
|
||||
}));
|
||||
}
|
||||
|
||||
async deleteBlob(address: string, msgId: string): Promise<boolean> {
|
||||
const result = this.stmts.deleteBlob.run(address, msgId);
|
||||
return result.changes > 0;
|
||||
}
|
||||
|
||||
async countBlobs(address: string, now: number): Promise<number> {
|
||||
const row = this.stmts.countBlobs.get(address, now) as { count: number };
|
||||
return row.count;
|
||||
}
|
||||
|
||||
async purgeExpired(now: number): Promise<number> {
|
||||
const result = this.stmts.purgeExpired.run(now);
|
||||
return result.changes;
|
||||
}
|
||||
|
||||
async deleteAddress(address: string): Promise<void> {
|
||||
const tx = this.db.transaction(() => {
|
||||
this.stmts.deleteOwner.run(address);
|
||||
this.stmts.deleteOwnerBlobs.run(address);
|
||||
});
|
||||
tx();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Database } from 'bun:sqlite';
|
||||
import type { StorageProvider, IdentityKeyPair, SignedPreKey, OneTimePreKey, SessionState, RetiredIdentity, PersistedStreamState } from '@shade/core';
|
||||
import type { StorageProvider, IdentityKeyPair, SignedPreKey, OneTimePreKey, SessionState, RetiredIdentity, PersistedStreamState, PeerVerification, PeerVerificationSource } from '@shade/core';
|
||||
import {
|
||||
toBase64, fromBase64,
|
||||
constantTimeEqual,
|
||||
@@ -48,6 +48,11 @@ export class SQLiteStorage implements StorageProvider {
|
||||
listActiveStreamStates: ReturnType<Database['prepare']>;
|
||||
listActiveStreamStatesByDirection: ReturnType<Database['prepare']>;
|
||||
pruneStreamStates: ReturnType<Database['prepare']>;
|
||||
savePeerVerification: ReturnType<Database['prepare']>;
|
||||
getPeerVerification: ReturnType<Database['prepare']>;
|
||||
removePeerVerification: ReturnType<Database['prepare']>;
|
||||
getPeerIdentityVersion: ReturnType<Database['prepare']>;
|
||||
upsertPeerIdentityVersion: ReturnType<Database['prepare']>;
|
||||
};
|
||||
|
||||
constructor(dbPath?: string) {
|
||||
@@ -111,6 +116,17 @@ export class SQLiteStorage implements StorageProvider {
|
||||
CREATE INDEX IF NOT EXISTS idx_stream_state_peer ON stream_state(peer_address);
|
||||
CREATE INDEX IF NOT EXISTS idx_stream_state_updated ON stream_state(updated_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_stream_state_status ON stream_state(status, direction);
|
||||
CREATE TABLE IF NOT EXISTS peer_verifications (
|
||||
peer_address TEXT PRIMARY KEY,
|
||||
fingerprint TEXT NOT NULL,
|
||||
verified_at INTEGER NOT NULL,
|
||||
verified_by TEXT NOT NULL,
|
||||
identity_version INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS peer_identity_versions (
|
||||
peer_address TEXT PRIMARY KEY,
|
||||
version INTEGER NOT NULL
|
||||
);
|
||||
`);
|
||||
}
|
||||
|
||||
@@ -153,6 +169,20 @@ export class SQLiteStorage implements StorageProvider {
|
||||
pruneStreamStates: this.db.prepare(
|
||||
"DELETE FROM stream_state WHERE status IN ('finished', 'aborted') AND updated_at < ?",
|
||||
),
|
||||
savePeerVerification: this.db.prepare(
|
||||
`INSERT OR REPLACE INTO peer_verifications
|
||||
(peer_address, fingerprint, verified_at, verified_by, identity_version)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
),
|
||||
getPeerVerification: this.db.prepare(
|
||||
'SELECT peer_address, fingerprint, verified_at, verified_by, identity_version FROM peer_verifications WHERE peer_address = ?',
|
||||
),
|
||||
removePeerVerification: this.db.prepare('DELETE FROM peer_verifications WHERE peer_address = ?'),
|
||||
getPeerIdentityVersion: this.db.prepare('SELECT version FROM peer_identity_versions WHERE peer_address = ?'),
|
||||
upsertPeerIdentityVersion: this.db.prepare(
|
||||
`INSERT INTO peer_identity_versions (peer_address, version) VALUES (?, ?)
|
||||
ON CONFLICT(peer_address) DO UPDATE SET version = excluded.version`,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -320,6 +350,48 @@ export class SQLiteStorage implements StorageProvider {
|
||||
async pruneStreamStates(olderThan: number): Promise<void> {
|
||||
this.stmts.pruneStreamStates.run(olderThan);
|
||||
}
|
||||
|
||||
// ─── Peer verifications (V3.3) ────────────────────────────
|
||||
|
||||
async savePeerVerification(v: PeerVerification): Promise<void> {
|
||||
this.stmts.savePeerVerification.run(
|
||||
v.peerAddress,
|
||||
v.fingerprint,
|
||||
v.verifiedAt,
|
||||
v.verifiedBy,
|
||||
v.identityVersion,
|
||||
);
|
||||
}
|
||||
|
||||
async getPeerVerification(address: string): Promise<PeerVerification | null> {
|
||||
const row = this.stmts.getPeerVerification.get(address) as
|
||||
| { peer_address: string; fingerprint: string; verified_at: number | bigint; verified_by: string; identity_version: number | bigint }
|
||||
| undefined;
|
||||
if (!row) return null;
|
||||
return {
|
||||
peerAddress: row.peer_address,
|
||||
fingerprint: row.fingerprint,
|
||||
verifiedAt: Number(row.verified_at),
|
||||
verifiedBy: row.verified_by as PeerVerificationSource,
|
||||
identityVersion: Number(row.identity_version),
|
||||
};
|
||||
}
|
||||
|
||||
async removePeerVerification(address: string): Promise<void> {
|
||||
this.stmts.removePeerVerification.run(address);
|
||||
}
|
||||
|
||||
async getPeerIdentityVersion(address: string): Promise<number> {
|
||||
const row = this.stmts.getPeerIdentityVersion.get(address) as { version: number | bigint } | undefined;
|
||||
return row ? Number(row.version) : 1;
|
||||
}
|
||||
|
||||
async bumpPeerIdentityVersion(address: string): Promise<number> {
|
||||
const current = await this.getPeerIdentityVersion(address);
|
||||
const next = current + 1;
|
||||
this.stmts.upsertPeerIdentityVersion.run(address, next);
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
function rowToStreamState(row: any): PersistedStreamState {
|
||||
|
||||
Reference in New Issue
Block a user