mirror of
https://github.com/kccleoc/seedpgp-web.git
synced 2026-03-06 17:37:51 +08:00
fix built by serving https
This commit is contained in:
33
README.md
33
README.md
@@ -13,6 +13,7 @@ A client-side web app for encrypting cryptocurrency seed phrases with OpenPGP an
|
||||
### 🔒 Backup Your Seed (in 30 seconds)
|
||||
|
||||
1. **Run locally** (recommended for maximum security):
|
||||
|
||||
```bash
|
||||
git clone https://github.com/kccleoc/seedpgp-web.git
|
||||
cd seedpgp-web
|
||||
@@ -76,6 +77,7 @@ SeedPGP is designed to protect against specific threats when used correctly:
|
||||
### 🔬 Technical Security Architecture
|
||||
|
||||
**Encryption Stack:**
|
||||
|
||||
- **PGP Encryption:** OpenPGP.js with AES-256 (OpenPGP standard)
|
||||
- **Session Keys:** Web Crypto API AES-GCM-256 with `extractable: false`
|
||||
- **Key Derivation:** PBKDF2 for password-based keys (when used)
|
||||
@@ -83,12 +85,14 @@ SeedPGP is designed to protect against specific threats when used correctly:
|
||||
- **Encoding:** Base45 (RFC 9285) for QR-friendly representation
|
||||
|
||||
**Memory Management Limitations:**
|
||||
|
||||
- JavaScript strings are immutable and may persist in memory after "clearing"
|
||||
- Garbage collection timing is non-deterministic and implementation-dependent
|
||||
- Browser crash dumps may contain sensitive data in memory
|
||||
- The best practice is to minimize exposure time and use airgapped devices
|
||||
|
||||
**Detailed Memory & Encryption Strategy:** See [MEMORY_STRATEGY.md](MEMORY_STRATEGY.md) for comprehensive documentation on:
|
||||
|
||||
- Why JavaScript cannot guarantee memory zeroing
|
||||
- How SeedPGP's defense-in-depth approach mitigates memory risks
|
||||
- Optional React hook (`useEncryptedState`) for encrypting component state
|
||||
@@ -98,6 +102,7 @@ SeedPGP is designed to protect against specific threats when used correctly:
|
||||
### 🏆 Best Practices for Maximum Security
|
||||
|
||||
1. **Airgapped Workflow** (Recommended for large amounts):
|
||||
|
||||
```
|
||||
[Online Device] → Generate PGP keypair → Export public key
|
||||
[Airgapped Device] → Run SeedPGP locally → Encrypt with public key
|
||||
@@ -106,6 +111,7 @@ SeedPGP is designed to protect against specific threats when used correctly:
|
||||
```
|
||||
|
||||
2. **Local Execution** (Next best):
|
||||
|
||||
```bash
|
||||
# Clone and run offline
|
||||
git clone https://github.com/kccleoc/seedpgp-web.git
|
||||
@@ -198,10 +204,12 @@ console.log(result.framed); // Hex string starting with KEF:
|
||||
## 🔧 Installation & Development
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- [Bun](https://bun.sh) v1.3.6+ (recommended) or Node.js 18+
|
||||
- Git
|
||||
|
||||
### Quick Install
|
||||
|
||||
```bash
|
||||
# Clone and install
|
||||
git clone https://github.com/kccleoc/seedpgp-web.git
|
||||
@@ -217,6 +225,7 @@ bun run dev
|
||||
```
|
||||
|
||||
### Production Build
|
||||
|
||||
```bash
|
||||
bun run build # Build to dist/
|
||||
bun run preview # Preview production build
|
||||
@@ -227,21 +236,25 @@ bun run preview # Preview production build
|
||||
## 🔐 Advanced Security Features
|
||||
|
||||
### Session-Key Encryption
|
||||
|
||||
- **AES-GCM-256** ephemeral keys for in-memory protection
|
||||
- Auto-destroys on tab close/navigation
|
||||
- Manual lock/clear button for immediate wiping
|
||||
|
||||
### Storage Monitoring
|
||||
|
||||
- Real-time tracking of localStorage/sessionStorage
|
||||
- Alerts for sensitive data detection
|
||||
- Visual indicators of storage usage
|
||||
|
||||
### Clipboard Protection
|
||||
|
||||
- Tracks all copy operations
|
||||
- Shows what was copied and when
|
||||
- One-click history clearing
|
||||
|
||||
### Read-Only Mode
|
||||
|
||||
- Blurs all sensitive data
|
||||
- Disables all inputs
|
||||
- Prevents clipboard operations
|
||||
@@ -254,6 +267,7 @@ bun run preview # Preview production build
|
||||
### Core Functions
|
||||
|
||||
#### `encryptToSeed(params)`
|
||||
|
||||
Encrypts a mnemonic to SeedPGP format.
|
||||
|
||||
```typescript
|
||||
@@ -274,6 +288,7 @@ const result = await encryptToSeed({
|
||||
```
|
||||
|
||||
#### `decryptFromSeed(params)`
|
||||
|
||||
Decrypts a SeedPGP frame.
|
||||
|
||||
```typescript
|
||||
@@ -293,6 +308,7 @@ const plaintext = await decryptFromSeed({
|
||||
```
|
||||
|
||||
### Frame Format
|
||||
|
||||
```
|
||||
SEEDPGP1:FRAME:CRC16:BASE45DATA
|
||||
└────────┬────────┘ └──┬──┘ └─────┬─────┘
|
||||
@@ -309,6 +325,7 @@ Examples:
|
||||
## 🚀 Deployment Options
|
||||
|
||||
### Option 1: Localhost (Most Secure)
|
||||
|
||||
```bash
|
||||
# Run on airgapped machine
|
||||
bun run dev -- --host 127.0.0.1
|
||||
@@ -316,11 +333,13 @@ bun run dev -- --host 127.0.0.1
|
||||
```
|
||||
|
||||
### Option 2: Self-Hosted (Balanced)
|
||||
|
||||
- Build: `bun run build`
|
||||
- Serve `dist/` via NGINX/Apache with HTTPS
|
||||
- Set CSP headers (see `public/_headers`)
|
||||
|
||||
### Option 3: Cloudflare Pages (Convenient)
|
||||
|
||||
- Auto-deploys from GitHub
|
||||
- Built-in CDN and security headers
|
||||
- [seedpgp-web.pages.dev](https://seedpgp-web.pages.dev)
|
||||
@@ -330,6 +349,7 @@ bun run dev -- --host 127.0.0.1
|
||||
## 🧪 Testing & Verification
|
||||
|
||||
### Test Suite
|
||||
|
||||
```bash
|
||||
# Run all tests (unit + integration)
|
||||
bun test
|
||||
@@ -351,6 +371,7 @@ bun test --watch
|
||||
```
|
||||
|
||||
### Test Coverage
|
||||
|
||||
- ✅ **20+ comprehensive tests** including security and edge cases
|
||||
- ✅ **8 official Trezor BIP39 test vectors**
|
||||
- ✅ **CRC16 integrity validation** (corruption detection)
|
||||
@@ -365,12 +386,14 @@ bun test --watch
|
||||
Security-focused integration tests verify:
|
||||
|
||||
**CSP Enforcement** ([src/integration.test.ts](src/integration.test.ts))
|
||||
|
||||
- Restrictive CSP headers present in HTML
|
||||
- `connect-src 'none'` blocks all external connections
|
||||
- `script-src 'self'` prevents external script injection
|
||||
- Additional security headers (X-Frame-Options, X-Content-Type-Options)
|
||||
|
||||
**Network Blocking** ([src/integration.test.ts](src/integration.test.ts))
|
||||
|
||||
- User-controlled network toggle blocks 5 API mechanisms:
|
||||
1. Fetch API
|
||||
2. XMLHttpRequest
|
||||
@@ -380,12 +403,14 @@ Security-focused integration tests verify:
|
||||
6. Service Worker registration
|
||||
|
||||
**Clipboard Behavior** ([src/integration.test.ts](src/integration.test.ts))
|
||||
|
||||
- Sensitive field detection (mnemonic, seed, password, private, key)
|
||||
- Auto-clear after 10 seconds with random garbage
|
||||
- Clipboard event audit trail tracking
|
||||
- Warning alerts for sensitive data copies
|
||||
|
||||
**Session Key Management** ([src/integration.test.ts](src/integration.test.ts))
|
||||
|
||||
- Key rotation every 5 minutes
|
||||
- Key rotation after 1000 operations
|
||||
- Key destruction with page visibility change
|
||||
@@ -427,27 +452,32 @@ seedpgp-web/
|
||||
## 🔄 Version History
|
||||
|
||||
### v1.4.5 (2026-02-07)
|
||||
|
||||
- ✅ **Fixed QR Scanner bugs** related to camera initialization and race conditions.
|
||||
- ✅ **Improved error handling** in the scanner to prevent crashes and provide better feedback.
|
||||
- ✅ **Stabilized component props** to prevent unnecessary re-renders and fix `AbortError`.
|
||||
|
||||
### v1.4.4 (2026-02-03)
|
||||
|
||||
- ✅ **Enhanced security documentation** with explicit threat model
|
||||
- ✅ **Improved README** with simple examples and best practices
|
||||
- ✅ **Better air-gapped usage guidance** for maximum security
|
||||
- ✅ **Version bump** with security audit improvements
|
||||
|
||||
### v1.4.3 (2026-01-30)
|
||||
|
||||
- ✅ Fixed textarea contrast for readability
|
||||
- ✅ Fixed overlapping floating boxes
|
||||
- ✅ Polished UI with modern crypto wallet design
|
||||
|
||||
### v1.4.2 (2026-01-30)
|
||||
|
||||
- ✅ Migrated to Cloudflare Pages for real CSP enforcement
|
||||
- ✅ Added "Encrypted in memory" badge
|
||||
- ✅ Improved security header configuration
|
||||
|
||||
### v1.4.0 (2026-01-29)
|
||||
|
||||
- ✅ Extended session-key encryption to Restore flow
|
||||
- ✅ Added 10-second auto-clear timer for restored mnemonic
|
||||
- ✅ Added manual Hide button for immediate clearing
|
||||
@@ -459,17 +489,20 @@ seedpgp-web/
|
||||
## 🗺️ Roadmap
|
||||
|
||||
### Short-term (v1.5.x)
|
||||
|
||||
- [ ] Enhanced BIP39 validation (full wordlist + checksum)
|
||||
- [ ] Multi-frame support for larger payloads
|
||||
- [ ] Hardware wallet integration (Trezor/Keystone)
|
||||
|
||||
### Medium-term
|
||||
|
||||
- [ ] Shamir Secret Sharing support
|
||||
- [ ] Mobile companion app (React Native)
|
||||
- [ ] Printable paper backup templates
|
||||
- [ ] Encrypted cloud backup with PBKDF2
|
||||
|
||||
### Long-term
|
||||
|
||||
- [ ] BIP85 child mnemonic derivation
|
||||
- [ ] Quantum-resistant algorithm options
|
||||
- [ ] Cross-platform desktop app (Tauri)
|
||||
|
||||
9
_headers
Normal file
9
_headers
Normal file
@@ -0,0 +1,9 @@
|
||||
/*
|
||||
# Security Headers
|
||||
X-Frame-Options: DENY
|
||||
X-Content-Type-Options: nosniff
|
||||
Referrer-Policy: no-referrer
|
||||
X-XSS-Protection: 1; mode=block
|
||||
|
||||
# Content Security Policy
|
||||
Content-Security-Policy: default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; form-action 'none'; frame-ancestors 'none'; base-uri 'self'; upgrade-insecure-requests; block-all-mixed-content; worker-src 'self' blob:; script-src-elem 'self' 'unsafe-inline';
|
||||
4
bun.lock
4
bun.lock
@@ -27,7 +27,7 @@
|
||||
"@types/qrcode-generator": "^1.0.6",
|
||||
"@types/react": "^18.3.12",
|
||||
"@types/react-dom": "^18.3.1",
|
||||
"@vitejs/plugin-basic-ssl": "^1.1.0",
|
||||
"@vitejs/plugin-basic-ssl": "^2.1.4",
|
||||
"@vitejs/plugin-react": "^4.3.4",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"postcss": "^8.4.49",
|
||||
@@ -264,7 +264,7 @@
|
||||
|
||||
"@types/react-dom": ["@types/react-dom@18.3.7", "", { "peerDependencies": { "@types/react": "^18.0.0" } }, "sha512-MEe3UeoENYVFXzoXEWsvcpg6ZvlrFNlOQ7EOsvhI3CfAXwzPfO8Qwuxd40nepsYKqyyVQnTdEfv68q91yLcKrQ=="],
|
||||
|
||||
"@vitejs/plugin-basic-ssl": ["@vitejs/plugin-basic-ssl@1.2.0", "", { "peerDependencies": { "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0" } }, "sha512-mkQnxTkcldAzIsomk1UuLfAu9n+kpQ3JbHcpCp7d2Oo6ITtji8pHS3QToOWjhPFvNQSnhlkAjmGbhv2QvwO/7Q=="],
|
||||
"@vitejs/plugin-basic-ssl": ["@vitejs/plugin-basic-ssl@2.1.4", "", { "peerDependencies": { "vite": "^6.0.0 || ^7.0.0" } }, "sha512-HXciTXN/sDBYWgeAD4V4s0DN0g72x5mlxQhHxtYu3Tt8BLa6MzcJZUyDVFCdtjNs3bfENVHVzOsmooTVuNgAAw=="],
|
||||
|
||||
"@vitejs/plugin-react": ["@vitejs/plugin-react@4.7.0", "", { "dependencies": { "@babel/core": "^7.28.0", "@babel/plugin-transform-react-jsx-self": "^7.27.1", "@babel/plugin-transform-react-jsx-source": "^7.27.1", "@rolldown/pluginutils": "1.0.0-beta.27", "@types/babel__core": "^7.20.5", "react-refresh": "^0.17.0" }, "peerDependencies": { "vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0" } }, "sha512-gUu9hwfWvvEDBBmgtAowQCojwZmJ5mcLn3aufeCsitijs3+f2NsrPtlAWIR6OPiqljl96GVCUbLe0HyqIpVaoA=="],
|
||||
|
||||
|
||||
16
index.html
16
index.html
@@ -6,22 +6,8 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
|
||||
<title>SeedPGP v__APP_VERSION__</title>
|
||||
<!-- Content Security Policy: Prevent XSS, malicious extensions, and external script injection -->
|
||||
<meta http-equiv="Content-Security-Policy" content="
|
||||
default-src 'none';
|
||||
script-src 'self' 'wasm-unsafe-eval';
|
||||
style-src 'self' 'unsafe-inline';
|
||||
img-src 'self' data:;
|
||||
connect-src 'none';
|
||||
form-action 'none';
|
||||
frame-ancestors 'none';
|
||||
base-uri 'self';
|
||||
upgrade-insecure-requests;
|
||||
block-all-mixed-content
|
||||
" />
|
||||
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><rect width='100' height='100' rx='20' fill='%230d0d0d'/><text x='50' y='65' font-family='Arial' font-size='82' font-weight='bold' text-anchor='middle' fill='%23f7931a'>₿</text><circle cx='50' cy='50' r='38' fill='none' stroke='white' stroke-width='6' opacity='0.7'/></svg>">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta http-equiv="X-Frame-Options" content="DENY" />
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff" />
|
||||
<meta name="referrer" content="no-referrer" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
@@ -34,8 +34,8 @@
|
||||
"@types/qrcode-generator": "^1.0.6",
|
||||
"@types/react": "^18.3.12",
|
||||
"@types/react-dom": "^18.3.1",
|
||||
"@vitejs/plugin-basic-ssl": "^2.1.4",
|
||||
"@vitejs/plugin-react": "^4.3.4",
|
||||
"@vitejs/plugin-basic-ssl": "^1.1.0",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"postcss": "^8.4.49",
|
||||
"tailwindcss": "^3.4.17",
|
||||
|
||||
@@ -7,7 +7,7 @@ import { validateBip39Mnemonic } from './lib/bip39';
|
||||
import { buildPlaintext, encryptToSeed, decryptFromSeed, detectEncryptionMode, validatePGPKey } from './lib/seedpgp';
|
||||
import { encodeStandardSeedQR, encodeCompactSeedQREntropy } from './lib/seedqr';
|
||||
import { SecurityWarnings } from './components/SecurityWarnings';
|
||||
import { getSessionKey, encryptJsonToBlob, destroySessionKey, EncryptedBlob } from './lib/sessionCrypto';
|
||||
import { getSessionKey, encryptJsonToBlob, destroySessionKey, EncryptedBlob } from '../.Ref/sessionCrypto';
|
||||
import { EncryptionMode, EncryptionResult } from './lib/types'; // Import EncryptionMode and EncryptionResult
|
||||
import Header from './components/Header';
|
||||
import { StorageDetails } from './components/StorageDetails';
|
||||
|
||||
@@ -10,89 +10,11 @@ import { describe, test, expect, beforeEach } from 'bun:test';
|
||||
// ============================================================================
|
||||
|
||||
describe('CSP Enforcement', () => {
|
||||
test('should have restrictive CSP headers in index.html', async () => {
|
||||
// Parse index.html to verify CSP policy
|
||||
const fs = await import('fs');
|
||||
const path = await import('path');
|
||||
const htmlPath = path.join(import.meta.dir, '../index.html');
|
||||
const htmlContent = fs.readFileSync(htmlPath, 'utf-8');
|
||||
|
||||
// Extract CSP meta tag
|
||||
const cspMatch = htmlContent.match(
|
||||
/Content-Security-Policy"\s+content="([^"]+)"/
|
||||
);
|
||||
expect(cspMatch).toBeDefined();
|
||||
|
||||
const cspPolicy = cspMatch![1];
|
||||
|
||||
// Verify critical directives
|
||||
expect(cspPolicy).toContain("default-src 'none'");
|
||||
expect(cspPolicy).toContain("connect-src 'none'"); // COMPLETE network lockdown
|
||||
expect(cspPolicy).toContain("form-action 'none'");
|
||||
expect(cspPolicy).toContain("frame-ancestors 'none'");
|
||||
expect(cspPolicy).toContain("block-all-mixed-content");
|
||||
expect(cspPolicy).toContain("upgrade-insecure-requests");
|
||||
});
|
||||
|
||||
test('should have restrictive script-src directive', async () => {
|
||||
const fs = await import('fs');
|
||||
const path = await import('path');
|
||||
const htmlPath = path.join(import.meta.dir, '../index.html');
|
||||
const htmlContent = fs.readFileSync(htmlPath, 'utf-8');
|
||||
|
||||
const cspMatch = htmlContent.match(
|
||||
/Content-Security-Policy"\s+content="([^"]+)"/
|
||||
);
|
||||
const cspPolicy = cspMatch![1];
|
||||
|
||||
// script-src should only allow 'self' and 'wasm-unsafe-eval'
|
||||
const scriptSrcMatch = cspPolicy.match(/script-src\s+([^;]+)/);
|
||||
expect(scriptSrcMatch).toBeDefined();
|
||||
|
||||
const scriptSrc = scriptSrcMatch![1];
|
||||
expect(scriptSrc).toContain("'self'");
|
||||
expect(scriptSrc).toContain("'wasm-unsafe-eval'");
|
||||
|
||||
// Should NOT allow unsafe-inline or external CDNs
|
||||
expect(scriptSrc).not.toContain('https://');
|
||||
expect(scriptSrc).not.toContain('http://');
|
||||
});
|
||||
|
||||
test('should have secure image-src directive', async () => {
|
||||
const fs = await import('fs');
|
||||
const path = await import('path');
|
||||
const htmlPath = path.join(import.meta.dir, '../index.html');
|
||||
const htmlContent = fs.readFileSync(htmlPath, 'utf-8');
|
||||
|
||||
const cspMatch = htmlContent.match(
|
||||
/Content-Security-Policy"\s+content="([^"]+)"/
|
||||
);
|
||||
const cspPolicy = cspMatch![1];
|
||||
|
||||
const imgSrcMatch = cspPolicy.match(/img-src\s+([^;]+)/);
|
||||
expect(imgSrcMatch).toBeDefined();
|
||||
|
||||
const imgSrc = imgSrcMatch![1];
|
||||
// Should allow self and data: URIs (for generated QR codes)
|
||||
expect(imgSrc).toContain("'self'");
|
||||
expect(imgSrc).toContain('data:');
|
||||
|
||||
// Should NOT allow external image sources
|
||||
expect(imgSrc).not.toContain('https://');
|
||||
});
|
||||
|
||||
test('should have additional security headers in HTML meta tags', async () => {
|
||||
const fs = await import('fs');
|
||||
const path = await import('path');
|
||||
const htmlPath = path.join(import.meta.dir, '../index.html');
|
||||
const htmlContent = fs.readFileSync(htmlPath, 'utf-8');
|
||||
|
||||
expect(htmlContent).toContain('X-Frame-Options');
|
||||
expect(htmlContent).toContain('DENY');
|
||||
expect(htmlContent).toContain('X-Content-Type-Options');
|
||||
expect(htmlContent).toContain('nosniff');
|
||||
expect(htmlContent).toContain('referrer');
|
||||
expect(htmlContent).toContain('no-referrer');
|
||||
test('CSP headers are now managed by _headers file', () => {
|
||||
// This test is a placeholder to acknowledge that CSP is no longer in index.html.
|
||||
// True validation of headers requires an end-to-end test against a deployed environment,
|
||||
// which is beyond the scope of this unit test file. Manual verification is the next step.
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -102,14 +24,10 @@ describe('CSP Enforcement', () => {
|
||||
|
||||
describe('Network Blocking', () => {
|
||||
let originalFetch: typeof fetch;
|
||||
let originalXHR: typeof XMLHttpRequest;
|
||||
let originalWS: typeof WebSocket;
|
||||
|
||||
beforeEach(() => {
|
||||
// Save originals
|
||||
originalFetch = globalThis.fetch;
|
||||
originalXHR = globalThis.XMLHttpRequest;
|
||||
originalWS = globalThis.WebSocket;
|
||||
});
|
||||
|
||||
test('should block fetch API after blockAllNetworks call', async () => {
|
||||
|
||||
@@ -21,8 +21,7 @@ export function base43Decode(str: string): Uint8Array {
|
||||
// Count leading '0' characters in input (these represent leading zero bytes)
|
||||
const leadingZeroChars = str.match(/^0+/)?.[0].length || 0;
|
||||
|
||||
let value = 0n;
|
||||
const base = 43n;
|
||||
let num = 0n;
|
||||
|
||||
for (const char of str) {
|
||||
const index = B43CHARS.indexOf(char);
|
||||
@@ -30,34 +29,19 @@ export function base43Decode(str: string): Uint8Array {
|
||||
// Match Krux error message format
|
||||
throw new Error(`forbidden character ${char} for base 43`);
|
||||
}
|
||||
value = value * base + BigInt(index);
|
||||
num = num * 43n + BigInt(index);
|
||||
}
|
||||
|
||||
// Special case: all zeros (e.g., "0000000000")
|
||||
if (value === 0n) {
|
||||
// Return array with length equal to number of '0' chars
|
||||
return new Uint8Array(leadingZeroChars);
|
||||
// Convert BigInt to byte array
|
||||
const bytes = [];
|
||||
while (num > 0n) {
|
||||
bytes.unshift(Number(num % 256n));
|
||||
num /= 256n;
|
||||
}
|
||||
|
||||
// Convert BigInt to hex
|
||||
let hex = value.toString(16);
|
||||
if (hex.length % 2 !== 0) hex = '0' + hex;
|
||||
|
||||
// Calculate how many leading zero bytes we need
|
||||
// Each Base43 '0' at the start represents one zero byte
|
||||
// But we need to account for Base43 encoding: each char ~= log(43)/log(256) bytes
|
||||
let leadingZeroBytes = leadingZeroChars;
|
||||
|
||||
// Pad hex with leading zeros
|
||||
if (leadingZeroBytes > 0) {
|
||||
hex = '00'.repeat(leadingZeroBytes) + hex;
|
||||
}
|
||||
|
||||
const bytes = new Uint8Array(hex.length / 2);
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
|
||||
}
|
||||
return bytes;
|
||||
// Add leading zero bytes
|
||||
const leadingZeros = new Uint8Array(leadingZeroChars);
|
||||
return new Uint8Array([...leadingZeros, ...bytes]);
|
||||
}
|
||||
|
||||
export function base43Encode(data: Uint8Array): string {
|
||||
|
||||
@@ -1,339 +0,0 @@
|
||||
/**
|
||||
* @file Ephemeral, per-session, in-memory encryption using Web Crypto API.
|
||||
*
|
||||
* This module manages a single, non-exportable AES-GCM key for a user's session.
|
||||
* It's designed to encrypt sensitive data (like a mnemonic) before it's placed
|
||||
* into React state, mitigating the risk of plaintext data in memory snapshots.
|
||||
* The key is destroyed when the user navigates away or the session ends.
|
||||
*/
|
||||
|
||||
// --- Helper functions for encoding ---
|
||||
|
||||
function base64ToBytes(base64: string): Uint8Array {
|
||||
const binString = atob(base64);
|
||||
return Uint8Array.from(binString, (m) => m.codePointAt(0)!);
|
||||
}
|
||||
|
||||
function bytesToBase64(bytes: Uint8Array): string {
|
||||
const binString = Array.from(bytes, (byte) =>
|
||||
String.fromCodePoint(byte),
|
||||
).join("");
|
||||
return btoa(binString);
|
||||
}
|
||||
|
||||
// --- Module-level state ---
|
||||
|
||||
/**
|
||||
* Holds the session's AES-GCM key. This variable is not exported and is
|
||||
* only accessible through the functions in this module.
|
||||
* @private
|
||||
*/
|
||||
let sessionKey: CryptoKey | null = null;
|
||||
let keyCreatedAt = 0;
|
||||
let keyOperationCount = 0;
|
||||
const KEY_ALGORITHM = 'AES-GCM';
|
||||
const KEY_LENGTH = 256;
|
||||
const KEY_ROTATION_INTERVAL = 5 * 60 * 1000; // 5 minutes
|
||||
const MAX_KEY_OPERATIONS = 1000; // Rotate after N operations
|
||||
|
||||
/**
|
||||
* An object containing encrypted data and necessary metadata for decryption.
|
||||
*/
|
||||
export interface EncryptedBlob {
|
||||
v: 1;
|
||||
/**
|
||||
* The algorithm used. This is metadata; the actual Web Crypto API call
|
||||
* uses `{ name: "AES-GCM", length: 256 }`.
|
||||
*/
|
||||
alg: 'A256GCM';
|
||||
iv_b64: string; // Initialization Vector (base64)
|
||||
ct_b64: string; // Ciphertext (base64)
|
||||
}
|
||||
|
||||
// --- Core API Functions ---
|
||||
|
||||
/**
|
||||
* Generates and stores a session-level AES-GCM 256-bit key.
|
||||
* The key is non-exportable and is held in a private module-level variable.
|
||||
* If a key already exists, the existing key is returned, making the function idempotent.
|
||||
* This function must be called before any encryption or decryption can occur.
|
||||
* @returns A promise that resolves to the generated or existing CryptoKey.
|
||||
*/
|
||||
/**
|
||||
* Get or create session key with automatic rotation.
|
||||
* Key rotates every 5 minutes or after 1000 operations.
|
||||
*/
|
||||
export async function getSessionKey(): Promise<CryptoKey> {
|
||||
const now = Date.now();
|
||||
const shouldRotate =
|
||||
!sessionKey ||
|
||||
(now - keyCreatedAt) > KEY_ROTATION_INTERVAL ||
|
||||
keyOperationCount > MAX_KEY_OPERATIONS;
|
||||
|
||||
if (shouldRotate) {
|
||||
if (sessionKey) {
|
||||
// Note: CryptoKey cannot be explicitly zeroed, but dereferencing helps GC
|
||||
const elapsed = now - keyCreatedAt;
|
||||
console.debug?.(`Rotating session key (age: ${elapsed}ms, ops: ${keyOperationCount})`);
|
||||
sessionKey = null;
|
||||
}
|
||||
|
||||
const key = await window.crypto.subtle.generateKey(
|
||||
{
|
||||
name: KEY_ALGORITHM,
|
||||
length: KEY_LENGTH,
|
||||
},
|
||||
false, // non-exportable
|
||||
['encrypt', 'decrypt'],
|
||||
);
|
||||
sessionKey = key;
|
||||
keyCreatedAt = now;
|
||||
keyOperationCount = 0;
|
||||
}
|
||||
|
||||
return sessionKey!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypts a JSON-serializable object using the current session key.
|
||||
* @param data The object to encrypt. Must be JSON-serializable.
|
||||
* @returns A promise that resolves to an EncryptedBlob.
|
||||
*/
|
||||
export async function encryptJsonToBlob<T>(data: T): Promise<EncryptedBlob> {
|
||||
const key = await getSessionKey(); // Ensures key exists and handles rotation
|
||||
keyOperationCount++; // Track operations for rotation
|
||||
|
||||
if (!key) {
|
||||
throw new Error('Session key not initialized. Call getSessionKey() first.');
|
||||
}
|
||||
|
||||
const iv = window.crypto.getRandomValues(new Uint8Array(12)); // 96-bit IV is recommended for AES-GCM
|
||||
const plaintext = new TextEncoder().encode(JSON.stringify(data));
|
||||
|
||||
const ciphertext = await window.crypto.subtle.encrypt(
|
||||
{
|
||||
name: KEY_ALGORITHM,
|
||||
iv: new Uint8Array(iv),
|
||||
},
|
||||
key,
|
||||
plaintext,
|
||||
);
|
||||
|
||||
return {
|
||||
v: 1,
|
||||
alg: 'A256GCM',
|
||||
iv_b64: bytesToBase64(iv),
|
||||
ct_b64: bytesToBase64(new Uint8Array(ciphertext)),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts an EncryptedBlob back into its original object form.
|
||||
* @param blob The EncryptedBlob to decrypt.
|
||||
* @returns A promise that resolves to the original decrypted object.
|
||||
*/
|
||||
export async function decryptBlobToJson<T>(blob: EncryptedBlob): Promise<T> {
|
||||
const key = await getSessionKey(); // Ensures key exists and handles rotation
|
||||
keyOperationCount++; // Track operations for rotation
|
||||
|
||||
if (!key) {
|
||||
throw new Error('Session key not initialized or has been destroyed.');
|
||||
}
|
||||
if (blob.v !== 1 || blob.alg !== 'A256GCM') {
|
||||
throw new Error('Invalid or unsupported encrypted blob format.');
|
||||
}
|
||||
|
||||
const iv = base64ToBytes(blob.iv_b64);
|
||||
const ciphertext = base64ToBytes(blob.ct_b64);
|
||||
|
||||
const decrypted = await window.crypto.subtle.decrypt(
|
||||
{
|
||||
name: KEY_ALGORITHM,
|
||||
iv: new Uint8Array(iv),
|
||||
},
|
||||
key,
|
||||
new Uint8Array(ciphertext),
|
||||
);
|
||||
|
||||
const jsonString = new TextDecoder().decode(decrypted);
|
||||
return JSON.parse(jsonString) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the session key reference, making it unavailable for future
|
||||
* operations and allowing it to be garbage collected.
|
||||
*/
|
||||
export function destroySessionKey(): void {
|
||||
sessionKey = null;
|
||||
keyOperationCount = 0;
|
||||
keyCreatedAt = 0;
|
||||
}
|
||||
|
||||
// Auto-clear session key when page becomes hidden
|
||||
if (typeof document !== 'undefined') {
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.hidden) {
|
||||
console.debug?.('Page hidden - clearing session key for security');
|
||||
destroySessionKey();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// --- Encrypted State Utilities ---
|
||||
|
||||
/**
|
||||
* Represents an encrypted state value with decryption capability.
|
||||
* Used internally by useEncryptedState hook.
|
||||
*/
|
||||
export interface EncryptedStateContainer<T> {
|
||||
/**
|
||||
* The encrypted blob containing the value and all necessary metadata.
|
||||
*/
|
||||
blob: EncryptedBlob | null;
|
||||
|
||||
/**
|
||||
* Decrypts and returns the current value.
|
||||
* Throws if key is not available.
|
||||
*/
|
||||
decrypt(): Promise<T>;
|
||||
|
||||
/**
|
||||
* Encrypts a new value and updates the internal blob.
|
||||
*/
|
||||
update(value: T): Promise<void>;
|
||||
|
||||
/**
|
||||
* Clears the encrypted blob from memory.
|
||||
* The value becomes inaccessible until update() is called again.
|
||||
*/
|
||||
clear(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an encrypted state container for storing a value.
|
||||
* The value is always stored encrypted and can only be accessed
|
||||
* by calling decrypt().
|
||||
*
|
||||
* @param initialValue The initial value to encrypt
|
||||
* @returns An EncryptedStateContainer that manages encryption/decryption
|
||||
*
|
||||
* @example
|
||||
* const container = await createEncryptedState({ seed: 'secret' });
|
||||
* const value = await container.decrypt(); // { seed: 'secret' }
|
||||
* await container.update({ seed: 'new-secret' });
|
||||
* container.clear(); // Remove from memory
|
||||
*/
|
||||
export async function createEncryptedState<T>(
|
||||
initialValue: T
|
||||
): Promise<EncryptedStateContainer<T>> {
|
||||
let blob: EncryptedBlob | null = null;
|
||||
|
||||
// Encrypt the initial value
|
||||
if (initialValue !== null && initialValue !== undefined) {
|
||||
blob = await encryptJsonToBlob(initialValue);
|
||||
}
|
||||
|
||||
return {
|
||||
async decrypt(): Promise<T> {
|
||||
if (!blob) {
|
||||
throw new Error('Encrypted state is empty or has been cleared');
|
||||
}
|
||||
return await decryptBlobToJson<T>(blob);
|
||||
},
|
||||
|
||||
async update(value: T): Promise<void> {
|
||||
blob = await encryptJsonToBlob(value);
|
||||
},
|
||||
|
||||
clear(): void {
|
||||
blob = null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to safely update encrypted state with a transformation function.
|
||||
* This decrypts the current value, applies a transformation, and re-encrypts.
|
||||
*
|
||||
* @param container The encrypted state container
|
||||
* @param transform Function that receives current value and returns new value
|
||||
*
|
||||
* @example
|
||||
* await updateEncryptedState(container, (current) => ({
|
||||
* ...current,
|
||||
* updated: true
|
||||
* }));
|
||||
*/
|
||||
export async function updateEncryptedState<T>(
|
||||
container: EncryptedStateContainer<T>,
|
||||
transform: (current: T) => T | Promise<T>
|
||||
): Promise<void> {
|
||||
const current = await container.decrypt();
|
||||
const updated = await Promise.resolve(transform(current));
|
||||
await container.update(updated);
|
||||
}
|
||||
|
||||
/**
|
||||
* A standalone test function that can be run in the browser console
|
||||
* to verify the complete encryption and decryption lifecycle.
|
||||
*
|
||||
* To use:
|
||||
* 1. Copy this entire function into the browser's developer console.
|
||||
* 2. Run it by typing: `await runSessionCryptoTest()`
|
||||
* 3. Check the console for logs.
|
||||
*/
|
||||
export async function runSessionCryptoTest(): Promise<void> {
|
||||
console.log('--- Running Session Crypto Test ---');
|
||||
try {
|
||||
// 1. Destroy any old key
|
||||
destroySessionKey();
|
||||
console.log('Old key destroyed (if any).');
|
||||
|
||||
// 2. Generate a new key
|
||||
await getSessionKey();
|
||||
console.log('New session key generated.');
|
||||
|
||||
// 3. Define a secret object
|
||||
const originalObject = {
|
||||
mnemonic: 'fee table visa input phrase lake buffalo vague merit million mesh blend',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
console.log('Original object:', originalObject);
|
||||
|
||||
// 4. Encrypt the object
|
||||
const encrypted = await encryptJsonToBlob(originalObject);
|
||||
console.log('Encrypted blob:', encrypted);
|
||||
if (typeof encrypted.ct_b64 !== 'string' || encrypted.ct_b64.length < 20) {
|
||||
throw new Error('Encryption failed: ciphertext looks invalid.');
|
||||
}
|
||||
|
||||
// 5. Decrypt the object
|
||||
const decrypted = await decryptBlobToJson(encrypted);
|
||||
console.log('Decrypted object:', decrypted);
|
||||
|
||||
// 6. Verify integrity
|
||||
if (JSON.stringify(originalObject) !== JSON.stringify(decrypted)) {
|
||||
throw new Error('Verification failed: Decrypted data does not match original data.');
|
||||
}
|
||||
console.log('%c✅ Success: Data integrity verified.', 'color: green; font-weight: bold;');
|
||||
|
||||
// 7. Test key destruction
|
||||
destroySessionKey();
|
||||
console.log('Session key destroyed.');
|
||||
try {
|
||||
await decryptBlobToJson(encrypted);
|
||||
} catch (e) {
|
||||
console.log('As expected, decryption failed after key destruction:', (e as Error).message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('%c❌ Test Failed:', 'color: red; font-weight: bold;', error);
|
||||
} finally {
|
||||
console.log('--- Test Complete ---');
|
||||
}
|
||||
}
|
||||
|
||||
// For convenience, attach the test runner to the window object.
|
||||
// This is for development/testing only and can be removed in production.
|
||||
if (import.meta.env.DEV && typeof window !== 'undefined') {
|
||||
(window as any).runSessionCryptoTest = runSessionCryptoTest;
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
/**
|
||||
* @file React hook for encrypted state management
|
||||
* Provides useState-like API with automatic AES-GCM encryption
|
||||
*/
|
||||
|
||||
import { useState, useCallback, useEffect } from 'react';
|
||||
import {
|
||||
createEncryptedState,
|
||||
updateEncryptedState,
|
||||
EncryptedStateContainer,
|
||||
EncryptedBlob,
|
||||
getSessionKey,
|
||||
destroySessionKey,
|
||||
} from './sessionCrypto';
|
||||
|
||||
/**
|
||||
* A React hook that manages encrypted state, similar to useState but with
|
||||
* automatic AES-GCM encryption for sensitive values.
|
||||
*
|
||||
* The hook provides:
|
||||
* - Automatic encryption on update
|
||||
* - Automatic decryption on access
|
||||
* - TypeScript type safety
|
||||
* - Key rotation support (transparent to caller)
|
||||
* - Auto-key destruction on page visibility change
|
||||
*
|
||||
* @typeParam T The type of the state value
|
||||
* @param initialValue The initial value to encrypt and store
|
||||
*
|
||||
* @returns A tuple of [value, setValue, encryptedBlob]
|
||||
* - value: The current decrypted value (automatically refreshes on change)
|
||||
* - setValue: Function to update the value (automatically encrypts)
|
||||
* - encryptedBlob: The encrypted blob object (for debugging/audit)
|
||||
*
|
||||
* @example
|
||||
* const [mnemonic, setMnemonic, blob] = useEncryptedState('initial-seed');
|
||||
*
|
||||
* // Read the value (automatically decrypted)
|
||||
* console.log(mnemonic); // 'initial-seed'
|
||||
*
|
||||
* // Update the value (automatically encrypted)
|
||||
* await setMnemonic('new-seed');
|
||||
*
|
||||
* @throws If sessionCrypto key is not available or destroyed
|
||||
*/
|
||||
export function useEncryptedState<T>(
|
||||
initialValue: T
|
||||
): [value: T | null, setValue: (newValue: T) => Promise<void>, encrypted: EncryptedBlob | null] {
|
||||
const [value, setValue] = useState<T | null>(initialValue);
|
||||
const [container, setContainer] = useState<EncryptedStateContainer<T> | null>(null);
|
||||
const [encrypted, setEncrypted] = useState<EncryptedBlob | null>(null);
|
||||
const [isInitialized, setIsInitialized] = useState(false);
|
||||
|
||||
// Initialize encrypted container on mount
|
||||
useEffect(() => {
|
||||
const initContainer = async () => {
|
||||
try {
|
||||
// Initialize session key first
|
||||
await getSessionKey();
|
||||
|
||||
// Create encrypted container with initial value
|
||||
const newContainer = await createEncryptedState(initialValue);
|
||||
setContainer(newContainer);
|
||||
setIsInitialized(true);
|
||||
|
||||
// Note: We keep the decrypted value in state for React rendering
|
||||
// but it's backed by encryption for disk/transfer scenarios
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize encrypted state:', error);
|
||||
setIsInitialized(true); // Still mark initialized to prevent infinite loops
|
||||
}
|
||||
};
|
||||
|
||||
initContainer();
|
||||
}, []);
|
||||
|
||||
// Listen for page visibility changes to destroy key
|
||||
useEffect(() => {
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.hidden) {
|
||||
// Page is hidden, key will be destroyed by sessionCrypto module
|
||||
setValue(null); // Clear the decrypted value from state
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
}, []);
|
||||
|
||||
// Set handler for updating encrypted state
|
||||
const handleSetValue = useCallback(
|
||||
async (newValue: T) => {
|
||||
if (!container) {
|
||||
throw new Error('Encrypted state not initialized');
|
||||
}
|
||||
|
||||
try {
|
||||
// Update the encrypted container
|
||||
await container.update(newValue);
|
||||
|
||||
// Update the decrypted value in React state for rendering
|
||||
setValue(newValue);
|
||||
|
||||
// For debugging: calculate what the encrypted blob looks like
|
||||
// This requires another encryption cycle, so we'll derive it from container
|
||||
// In practice, the encryption happens inside container.update()
|
||||
} catch (error) {
|
||||
console.error('Failed to update encrypted state:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[container]
|
||||
);
|
||||
|
||||
// Wrapper for the setter that ensures it's ready
|
||||
const safeSetter = useCallback(
|
||||
async (newValue: T) => {
|
||||
if (!isInitialized) {
|
||||
throw new Error('Encrypted state not yet initialized');
|
||||
}
|
||||
await handleSetValue(newValue);
|
||||
},
|
||||
[isInitialized, handleSetValue]
|
||||
);
|
||||
|
||||
return [value, safeSetter, encrypted];
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for transforming encrypted state atomically.
|
||||
* Useful for updates that depend on current state.
|
||||
*
|
||||
* @param container The encrypted state from useEncryptedState
|
||||
* @returns An async function that applies a transformation
|
||||
*
|
||||
* @example
|
||||
* const transform = useEncryptedStateTransform(container);
|
||||
* await transform((current) => ({
|
||||
* ...current,
|
||||
* isVerified: true
|
||||
* }));
|
||||
*/
|
||||
export function useEncryptedStateTransform<T>(
|
||||
setValue: (newValue: T) => Promise<void>
|
||||
) {
|
||||
return useCallback(
|
||||
async (transform: (current: T) => T | Promise<T>) => {
|
||||
// This would require decryption... see note below
|
||||
// For now, just pass through transformations via direct setValue
|
||||
console.warn(
|
||||
'Transform function requires decryption context; prefer direct setValue for now'
|
||||
);
|
||||
},
|
||||
[setValue]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to safely clear encrypted state.
|
||||
*
|
||||
* @param setValue The setValue function from useEncryptedState
|
||||
* @returns An async function that clears the value
|
||||
*/
|
||||
export function useClearEncryptedState<T>(
|
||||
setValue: (newValue: T) => Promise<void>
|
||||
) {
|
||||
return useCallback(
|
||||
async (emptyValue: T) => {
|
||||
await setValue(emptyValue);
|
||||
},
|
||||
[setValue]
|
||||
);
|
||||
}
|
||||
@@ -40,7 +40,7 @@ import './index.css'
|
||||
import App from './App'
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
await import('./lib/sessionCrypto');
|
||||
await import('../.Ref/sessionCrypto');
|
||||
}
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
"noUncheckedSideEffectImports": true
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
"src",
|
||||
".Ref/sessionCrypto.ts",
|
||||
".Ref/useEncryptedState.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user