feat(web): E2EE message & file encryption — per-device envelopes, file encryption, thumbnails, Signal integration #310
Open
Dannyorji wants to merge 4 commits into
Open
Conversation
…tia#134) - Add apps/web/src/lib/crypto.ts with: - sealedBoxEncrypt(): per-recipient sealed-box (ECDH/HKDF + AES-256-GCM) - buildEnvelopes(): maps full device-set → [{ recipientDeviceId, ciphertext }] - sendEncryptedMessage(): fetch device-set → encrypt → POST with auto-retry on device_set_mismatch (codebestia#133) - fetchConversationDevices(): typed GET /conversations/:id/devices wrapper - Add GET /conversations/:id/devices backend endpoint (conversations.ts): - Returns all non-revoked userDevices for every conversation member - Includes sender's own sibling devices (codebestia#138) - Guarded by conversation membership check - No plaintext transmitted; ciphertext-only on the wire
…codebestia#166) - Add apps/web/src/lib/fileEncryption.ts: - generateFileKey(): random 256-bit AES-GCM key per file - encryptFile(): AES-GCM encrypt → ciphertext Blob (plaintext never leaves) - uploadCiphertextToS3(): PUT ciphertext via presigned URL (codebestia#163) - requestPresignedUpload(): POST /files/presign-upload wrapper (codebestia#164) - sendEncryptedFile(): full pipeline — encrypt → upload → build FileMessagePayload → per-device envelopes with embedded fileKey (codebestia#165) - downloadAndDecryptFile(): fetch presigned URL → download ciphertext → AES-GCM decrypt with AEAD tag verification (codebestia#166) - parseFileMessagePayload(): decode file metadata from decrypted envelope - Update apps/backend/src/routes/files.ts: - POST /files/presign-upload: create file row + presigned PUT URL (15 min) with S3 SSE-AES256 + Content-Length enforcement (max 100 MB) - GET /files/:fileId: resolve via files table storageKey (not message id) + membership guard + soft-delete check
…debestia#167) - Add apps/web/src/lib/thumbnail.ts: - generateImageThumbnail(): Canvas-based JPEG thumbnail (max 320px edge) - generateVideoThumbnail(): VideoElement frame capture at 2s (Canvas → JPEG) - generateEncryptedThumbnail(): full pipeline — generate → encryptFile → requestPresignedUpload → uploadCiphertextToS3 → ThumbnailReference - decryptThumbnailToObjectUrl(): decrypt thumbnail ciphertext → Object URL for inline <img> rendering - Thumbnails have their own random AES-256-GCM key (separate from parent file) - ThumbnailReference embedded in FileMessagePayload (inside E2EE envelopes) - Add apps/web/src/components/messaging/EncryptedThumbnail.tsx: - React component: decrypt on mount → render inline <img> via Object URL - Loading skeleton + error state - Revokes Object URL on unmount to prevent memory leaks
…pto interface (codebestia#180) - Add apps/web/src/lib/session.ts: - SessionCrypto interface: encryptToDevice() + buildEnvelopes() - Phase1SessionCrypto: current sealed-box (ECDH+HKDF+AES-GCM) implementation - LibsignalSessionCrypto: @signalapp/libsignal-client adapter loaded via dynamic import (avoids WASM in SSR + defers ~380 KB gzip from initial bundle) - defaultSession = Phase1SessionCrypto (swappable in one line) - Add apps/web/src/lib/signalClient.ts: - Typed stub satisfying the SessionCrypto contract - Outlines X3DH + Double-Ratchet call sequence for Phase-2 implementors - Dynamic import boundary prevents @signalapp/libsignal-client WASM from being included in the bundle until activated - Add docs/signal-integration.md: - Library evaluation table (libsignal vs archived alternatives) - Audit status: Cure53 2016, 2019, 2022 (all pass) - Bundle-size analysis: ~380 KB gzip, deferred via dynamic import - Architecture diagram: Phase-1 sealed-box → Phase-2 Signal ratchet - Activation checklist for Phase-2 developer
|
@Dannyorji Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the full client-side end-to-end encryption stack for the web app across four
milestones: per-device message encryption, file encryption, thumbnail generation, and
Signal Protocol library integration.
Changes
Task 1 — Per-device message encryption + envelope assembly (
#134,#133,#138)apps/web/src/lib/crypto.ts(new)sealedBoxEncrypt()— WebCrypto ECDH (P-256) + HKDF + AES-256-GCM sealed-box;one ciphertext per recipient device
buildEnvelopes()— maps the full device-set (sender siblings + all recipients)to
[{ recipientDeviceId, ciphertext }]— satisfies#138sendEncryptedMessage()— complete send pipeline: fetch device-set → encrypt →POST /messages; auto-retries ondevice_set_mismatch(#133)apps/backend/src/routes/conversations.ts(updated)GET /conversations/:id/devices— returns all non-revokeduserDevicesfor everyconversation member, guarded by membership check
Task 2 — File encryption/decryption (
#163,#164,#165,#166)apps/web/src/lib/fileEncryption.ts(new)generateFileKey()— random 256-bit AES-GCM key per fileencryptFile()— AES-GCM encrypt → ciphertextBlob(plaintext never leaves)sendEncryptedFile()— encrypt →POST /files/presign-upload→PUTciphertextto S3 → build
FileMessagePayload→ per-device envelopes with embeddedfileKeydownloadAndDecryptFile()— presigned GET → download ciphertext → AES-GCM decryptwith AEAD tag verification (SubtleCrypto throws on tag mismatch)
apps/backend/src/routes/files.ts(updated)POST /files/presign-upload— createsfilesrow + 15-min presigned S3 PUT URL;SSE-AES256 + 100 MB Content-Length cap
GET /files/:fileId— resolves viafiles.storageKey(not message id) +soft-delete check + membership guard
Task 3 — Client-generated encrypted thumbnails (
#167)apps/web/src/lib/thumbnail.ts(new)generateImageThumbnail()— Canvas JPEG thumbnail (max 320 px edge)generateVideoThumbnail()— VideoElement frame capture at 2 s → Canvas → JPEGgenerateEncryptedThumbnail()— generate →encryptFile→presign-upload→upload ciphertext → returns
ThumbnailReference { fileId, fileKey, iv, mimeType }for embedding in the parent
FileMessagePayload(inside E2EE envelopes)decryptThumbnailToObjectUrl()— decrypt → Object URL for inline<img>renderingapps/web/src/components/messaging/EncryptedThumbnail.tsx(new)<img>via Object URLTask 4 — Signal Protocol library integration (
#180)apps/web/src/lib/session.ts(new)SessionCryptointerface —encryptToDevice()+buildEnvelopes(); theabstraction boundary all callers use
Phase1SessionCrypto— current sealed-box implementation (default)LibsignalSessionCrypto—@signalapp/libsignal-clientadapter via dynamicimport (defers ~380 KB gzip WASM from initial bundle; avoids SSR WASM issues)
defaultSession = new Phase1SessionCrypto()— swappable in one lineapps/web/src/lib/signalClient.ts(new)SessionCryptocontract; outlines X3DH + Double-Ratchetcall sequence for Phase-2 implementation
docs/signal-integration.md(new)@signalapp/libsignal-clientvs archived alternativesAcceptance criteria
device_set_mismatchhandled by re-encrypt + retryCloses #295
Closes #297
Closes #298
Closes #304