|
10 | 10 |
|
11 | 11 | A NestJS API designed to run inside a Trusted Execution Environment (TEE), giving users cryptographic guarantees that the operator cannot access their data during processing. |
12 | 12 |
|
13 | | -## What is a TEE? |
14 | | - |
15 | | -The server runs inside a hardware-isolated enclave (AMD SEV-SNP, Intel TDX, or AWS Nitro). The host OS — including the operator — cannot read the enclave's memory. TLS terminates inside the enclave, so plaintext never passes through host-controlled infrastructure. Users can verify the exact code running via the `/attestation` endpoint and compare it against this repository. |
16 | | - |
17 | | -## Features |
18 | | - |
19 | | -- ✅ **Hardware-isolated execution** - Code runs in a TEE enclave |
20 | | -- ✅ **TLS termination inside enclave** - Host never sees plaintext |
21 | | -- ✅ **Remote attestation** - Cryptographic proof of running code |
22 | | -- ✅ **SIWE authentication** - Sign-In with Ethereum for Web3 auth |
23 | | -- ✅ **Sanitized logging** - No sensitive data in logs |
24 | | -- ✅ **KMS integration** - Secrets fetched after attestation |
25 | | -- ✅ **Rate limiting** - DoS protection |
26 | | -- ✅ **Security headers** - Helmet.js integration |
27 | | -- ✅ **Input validation** - All requests validated |
28 | | -- ✅ **Health checks** - `/health`, `/health/ready`, `/health/live` |
29 | | -- ✅ **API documentation** - Swagger/OpenAPI integration |
30 | | - |
31 | | -## Security Model |
32 | | - |
33 | | -### Threat Model |
34 | | - |
35 | | -**Protected against:** |
36 | | -- Malicious host operator reading memory |
37 | | -- Network eavesdropping (TLS in enclave) |
38 | | -- Log-based data exfiltration |
39 | | -- Stack trace information leakage |
40 | | - |
41 | | -**NOT protected against:** |
42 | | -- Side-channel attacks (timing, cache) - See [docs/SIDE_CHANNEL_ATTACKS.md](docs/SIDE_CHANNEL_ATTACKS.md) for mitigations |
43 | | -- Physical access to hardware |
44 | | -- Compromised TEE firmware |
45 | | -- Application logic bugs |
46 | | - |
47 | | -### Trust Assumptions |
48 | | - |
49 | | -You must trust: |
50 | | -1. The TEE hardware vendor (AMD/Intel/AWS) |
51 | | -2. This application code (verify via attestation) |
52 | | -3. The KMS that releases secrets |
53 | | - |
54 | | -You do NOT need to trust: |
55 | | -- The host OS |
56 | | -- The cloud provider operator |
57 | | -- Network infrastructure |
58 | | - |
59 | | -## Installation |
| 13 | +## Install |
60 | 14 |
|
61 | 15 | ```bash |
62 | | -pnpm install |
| 16 | +pnpm i |
63 | 17 | ``` |
64 | 18 |
|
65 | | -## Development Setup |
| 19 | +## Run |
66 | 20 |
|
67 | | -1. Copy environment template: |
| 21 | +Copy environment template: |
68 | 22 | ```bash |
69 | 23 | cp .env.template .env |
70 | 24 | ``` |
71 | 25 |
|
72 | | -2. Generate self-signed TLS certificates: |
| 26 | +Generate self-signed TLS certificates: |
73 | 27 | ```bash |
74 | 28 | mkdir -p secrets |
75 | 29 | openssl req -x509 -newkey rsa:4096 -keyout secrets/tls.key -out secrets/tls.cert -days 365 -nodes -subj "/CN=localhost" |
76 | 30 | ``` |
77 | 31 |
|
78 | | -3. Start the dev server: |
| 32 | +Start the dev server: |
79 | 33 | ```bash |
80 | 34 | pnpm start:dev |
81 | 35 | ``` |
82 | 36 |
|
83 | | -4. Access the API documentation: |
| 37 | +Access the API documentation: |
84 | 38 | ``` |
85 | 39 | https://localhost:3000 |
86 | 40 | ``` |
87 | | -(Accept the self-signed certificate warning in your browser) |
88 | | - |
89 | | -## Production Deployment |
90 | | - |
91 | | -> **📖 For detailed platform-specific deployment instructions, see [docs/TEE_SETUP.md](docs/TEE_SETUP.md)** |
92 | | -
|
93 | | -### Prerequisites |
94 | | - |
95 | | -- TEE-enabled hardware (AMD SEV-SNP, Intel TDX, or AWS Nitro) |
96 | | -- KMS endpoint configured to verify attestation |
97 | | -- Production TLS certificates generated inside the enclave |
98 | | - |
99 | | -### Environment Variables |
100 | | - |
101 | | -```bash |
102 | | -NODE_ENV=production |
103 | | -KMS_URL=https://your-kms.example.com/release |
104 | | -``` |
105 | | - |
106 | | -### TEE Platform Integration |
107 | | - |
108 | | -The application now includes full attestation support for: |
109 | | - |
110 | | -1. **AMD SEV-SNP** - Uses `snpguest` or `sev-guest-get-report` tools |
111 | | - - Reads attestation reports from `/dev/sev-guest` device |
112 | | - - Extracts measurement hash for verification |
113 | | - - Install: `apt-get install snpguest` or build from AMD's sev-guest tools |
114 | | - |
115 | | -2. **Intel TDX** - Uses `tdx-attest` tool or direct `/dev/tdx-guest` access |
116 | | - - Generates TDX quotes containing MRTD measurements |
117 | | - - Install: `apt-get install tdx-attest` or build from Intel TDX SDK |
118 | | - |
119 | | -3. **AWS Nitro Enclaves** - Uses Nitro Security Module (NSM) |
120 | | - - Generates CBOR-encoded attestation documents with PCR measurements |
121 | | - - Requires: `nitro-cli` and NSM device (`/dev/nsm`) |
122 | | - |
123 | | -4. **Development Mode** - Automatically detects non-TEE environments |
124 | | - - Returns mock attestation with clear warnings |
125 | | - - Platform field set to 'none' for easy detection |
126 | | - - Safe for local development and testing |
127 | | - |
128 | | -### Platform Detection |
129 | | - |
130 | | -The service automatically detects the TEE platform at startup: |
131 | | -- Checks for `/dev/sev-guest` → AMD SEV-SNP |
132 | | -- Checks for `/dev/tdx-guest` → Intel TDX |
133 | | -- Checks for `/dev/nsm` → AWS Nitro |
134 | | -- Otherwise → Development mode (no TEE) |
135 | | - |
136 | | -### Installation of Platform Tools |
137 | | - |
138 | | -**AMD SEV-SNP:** |
139 | | -```bash |
140 | | -# Install from package (Ubuntu/Debian) |
141 | | -apt-get install snpguest |
142 | | - |
143 | | -# Or build from source |
144 | | -git clone https://github.com/virtee/snpguest |
145 | | -cd snpguest && cargo build --release |
146 | | -``` |
147 | | - |
148 | | -**Intel TDX:** |
149 | | -```bash |
150 | | -# Install Intel TDX tools |
151 | | -wget https://download.01.org/intel-sgx/latest/linux-latest/distro/ubuntu22.04-server/tdx-attest.deb |
152 | | -dpkg -i tdx-attest.deb |
153 | | -``` |
154 | | - |
155 | | -**AWS Nitro:** |
156 | | -```bash |
157 | | -# Install AWS Nitro CLI |
158 | | -amazon-linux-extras install aws-nitro-enclaves-cli |
159 | | -# Or for Ubuntu: |
160 | | -wget https://github.com/aws/aws-nitro-enclaves-cli/releases/latest/download/nitro-cli.deb |
161 | | -dpkg -i nitro-cli.deb |
162 | | -``` |
163 | | - |
164 | | -## API Endpoints |
165 | 41 |
|
166 | | -Full API documentation is available via Swagger UI at `https://localhost:3000` (development) or your production URL. |
| 42 | +__Accept the self-signed certificate warning in your browser. Please note the 's' in 'https'.__ |
167 | 43 |
|
168 | | -### Authentication |
| 44 | +## Docs |
169 | 45 |
|
170 | | -**Sign-In with Ethereum (SIWE)** - Guard-based Web3 authentication |
171 | | - |
172 | | -- `POST /auth/nonce` - Generate a cryptographically secure nonce for SIWE authentication |
173 | | - - Returns nonce with 5-minute expiration |
174 | | - - Single-use nonces (consumed after verification) |
175 | | - |
176 | | -- Protected endpoints use `SiweGuard` for authentication |
177 | | - - Credentials passed via `x-siwe-message` and `x-siwe-signature` headers |
178 | | - - Guard validates signature, checks nonce, and attaches address to request |
179 | | - - Returns 401 Unauthorized if validation fails |
180 | | - |
181 | | -**Example Flow:** |
182 | | -```bash |
183 | | -# 1. Get nonce |
184 | | -curl -X POST https://localhost:3000/auth/nonce |
185 | | - |
186 | | -# 2. Sign SIWE message with your wallet (using w3pk, MetaMask, etc.) |
187 | | - |
188 | | -# 3. Access protected endpoint with authentication headers |
189 | | -curl -X POST https://localhost:3000/hello \ |
190 | | - -H "x-siwe-message: localhost wants you to sign in..." \ |
191 | | - -H "x-siwe-signature: 0x..." |
192 | | -``` |
193 | | - |
194 | | -**Security Features:** |
195 | | -- **No JWT required** - Direct signature verification on each request |
196 | | -- **Header-based credentials** - Clean separation from request body |
197 | | -- **Guard pattern** - Declarative protection with `@UseGuards(SiweGuard)` |
198 | | -- **Request decoration** - Verified address available at `req.user.address` |
199 | | - |
200 | | -> **📖 For detailed SIWE usage, client examples (w3pk, MetaMask, ethers), and troubleshooting, see [docs/SIWE.md](docs/SIWE.md)** |
201 | | -
|
202 | | -### Core Endpoints |
203 | | - |
204 | | -#### `GET /` |
205 | | -Health check - returns greeting message. |
206 | | - |
207 | | -#### `GET /attestation` |
208 | | -Returns the TEE attestation report. Clients should: |
209 | | -1. Fetch this endpoint |
210 | | -2. Verify the report signature with TEE platform verification service |
211 | | -3. Compare measurement hash against published Docker image SHA |
212 | | -4. Only send sensitive data if verification succeeds |
213 | | - |
214 | | -#### `GET /health` |
215 | | -Basic health check for load balancers. |
216 | | - |
217 | | -#### `GET /health/ready` |
218 | | -Readiness probe - indicates if service is ready to accept traffic. |
219 | | - |
220 | | -#### `GET /health/live` |
221 | | -Liveness probe - indicates if service is alive. |
222 | | - |
223 | | -## Verifying the Deployment |
224 | | - |
225 | | -```bash |
226 | | -# Get attestation report |
227 | | -curl -k https://your-server.com/attestation |
228 | | - |
229 | | -# Example response: |
230 | | -# { |
231 | | -# "platform": "amd-sev-snp", // or "intel-tdx", "aws-nitro", "none" |
232 | | -# "report": "base64-encoded-attestation-report", |
233 | | -# "measurement": "hex-encoded-measurement-hash", |
234 | | -# "timestamp": "2026-03-17T...", |
235 | | -# "instructions": "Verify this report at..." |
236 | | -# } |
237 | | - |
238 | | -# If platform is "none", you're NOT in a TEE (development mode) |
239 | | -# If platform is a TEE type, verify the report cryptographically |
240 | | -``` |
241 | | - |
242 | | -### Verification Steps |
243 | | - |
244 | | -1. **Check Platform**: Ensure `platform` is not `"none"` |
245 | | -2. **Verify Signature**: Use platform-specific verification service |
246 | | - - AMD SEV-SNP: Use AMD's KDS service |
247 | | - - Intel TDX: Use Intel's attestation verification API |
248 | | - - AWS Nitro: Use `aws-nitro-enclaves-cose` library |
249 | | -3. **Compare Measurement**: Match against published Docker image SHA256 |
250 | | -4. **Trust Decision**: Only send sensitive data if verification passes |
251 | | - |
252 | | -## Development |
253 | | - |
254 | | -```bash |
255 | | -# Install dependencies |
256 | | -pnpm install |
257 | | - |
258 | | -# Run in development mode |
259 | | -pnpm start:dev |
260 | | - |
261 | | -# Build for production |
262 | | -pnpm build |
263 | | - |
264 | | -# Run production build |
265 | | -pnpm start:prod |
266 | | - |
267 | | -# Lint code |
268 | | -pnpm lint |
269 | | - |
270 | | -# Format code |
271 | | -pnpm format |
272 | | - |
273 | | -# Run tests |
274 | | -pnpm test |
275 | | -``` |
276 | | - |
277 | | -## Architecture |
278 | | - |
279 | | -``` |
280 | | -┌─────────────────────────────────────────┐ |
281 | | -│ TEE Enclave │ |
282 | | -│ ┌───────────────────────────────────┐ │ |
283 | | -│ │ NestJS Application │ │ |
284 | | -│ │ - Attestation Controller │ │ |
285 | | -│ │ - Business Logic │ │ |
286 | | -│ │ - TLS Termination │ │ |
287 | | -│ └───────────────────────────────────┘ │ |
288 | | -│ ↕ │ |
289 | | -│ ┌───────────────────────────────────┐ │ |
290 | | -│ │ Secrets Service │ │ |
291 | | -│ │ (KMS integration) │ │ |
292 | | -│ └───────────────────────────────────┘ │ |
293 | | -└─────────────────────────────────────────┘ |
294 | | - ↕ (encrypted) |
295 | | - ┌──────────────┐ |
296 | | - │ KMS │ |
297 | | - │ (external) │ |
298 | | - └──────────────┘ |
299 | | -``` |
| 46 | +- [**Overview**](docs/OVERVIEW.md) - Complete project overview, features, installation, and API endpoints |
| 47 | +- [**TEE Setup**](docs/TEE_SETUP.md) - Platform-specific deployment instructions for AMD SEV-SNP, Intel TDX, AWS Nitro, and Phala Network |
| 48 | +- [**SIWE Authentication**](docs/SIWE.md) - Sign-In with Ethereum integration guide and client examples |
| 49 | +- [**Side Channel Attacks**](docs/SIDE_CHANNEL_ATTACKS.md) - Security considerations and mitigation strategies |
300 | 50 |
|
301 | 51 | ## License |
302 | 52 |
|
|
0 commit comments