StillWorking commited on
Commit
092d7fb
·
verified ·
1 Parent(s): f0f0992

Upload SECURITY.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. SECURITY.md +185 -0
SECURITY.md ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Security Guide — Sentinel-AI
2
+
3
+ This project is a hackathon/demo system that performs face recognition and
4
+ biometric attribute search. Treat it as **sensitive infrastructure**, not a
5
+ normal web app.
6
+
7
+ The default configuration is intentionally convenient for a local demo. It is
8
+ **not** safe to expose to a network as-is.
9
+
10
+ ---
11
+
12
+ ## 1. Deployment profiles
13
+
14
+ | Profile | Auth | Rate limit | TLS | Suitable for |
15
+ |---------|------|------------|-----|--------------|
16
+ | Local demo (default) | Off (`SENTINEL_API_KEY` empty) | On, loopback only | No | Your own machine |
17
+ | Shared / internal | Bearer token | On | Reverse proxy | Trusted team network |
18
+ | Public | Bearer token + SSO/WAF | On (proxy + app) | Required | Not recommended |
19
+
20
+ The local demo binds to `127.0.0.1` unless you deliberately use `--host 0.0.0.0`.
21
+ If you bind to all interfaces, set `SENTINEL_API_KEY` first.
22
+
23
+ ---
24
+
25
+ ## 2. Secrets
26
+
27
+ ### Where keys live
28
+
29
+ - `E:\AI Infra\.env` and `E:\AI Infra\backend\.env` (gitignored; backend wins).
30
+ - Provider console keys: `GEMINI_API_KEY` / `GOOGLE_API_KEY`, `GROQ_API_KEY`,
31
+ `COHERE_API_KEY`.
32
+ - The dashboard never receives provider keys. `VITE_API_KEY` is only an
33
+ application access token and is visible in the browser bundle; never put a
34
+ provider key there.
35
+
36
+ ### Rotation
37
+
38
+ The audit found plaintext keys in `.env` files. Because those files may have
39
+ been copied or archived, **rotate all three keys** before sharing the folder:
40
+
41
+ 1. **Gemini** — <https://aistudio.google.com/app/apikey> → revoke the old key,
42
+ create a new one.
43
+ 2. **Groq** — <https://console.groq.com/keys> → delete, create a new key.
44
+ 3. **Cohere** — <https://dashboard.cohere.com/api-keys> → revoke, create a new
45
+ key.
46
+
47
+ Then update **both** `.env` files (or remove the duplicate and keep one),
48
+ restart the backend, and verify `/health` plus one live alert.
49
+
50
+ ```powershell
51
+ # Check nothing secret is staged for sharing
52
+ python scripts/check_secrets.py
53
+ ```
54
+
55
+ Do not paste keys into chat, issues, screenshots, logs, or commits. If a key
56
+ leaks, rotate it; deleting the file is not enough because the value may already
57
+ be in history or a backup.
58
+
59
+ ---
60
+
61
+ ## 3. API access control
62
+
63
+ Enable the built-in Bearer-token check:
64
+
65
+ ```dotenv
66
+ SENTINEL_API_KEY=change-me-to-a-long-random-demo-key
67
+ RATE_LIMIT_PER_MINUTE=60
68
+ RATE_LIMIT_BURST=20
69
+ ```
70
+
71
+ Every `/api/v1/*` request must then send either:
72
+
73
+ ```http
74
+ Authorization: Bearer <key>
75
+ ```
76
+
77
+ or:
78
+
79
+ ```http
80
+ X-API-Key: <key>
81
+ ```
82
+
83
+ `/health`, `/docs`, `/redoc`, and `/openapi.json` stay open for probes and
84
+ documentation. `OPTIONS` preflight requests bypass auth so CORS works.
85
+
86
+ Generate a key with:
87
+
88
+ ```powershell
89
+ # PowerShell
90
+ -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 48 | ForEach-Object { [char]$_ })
91
+ ```
92
+
93
+ The rate limiter is an in-process token bucket keyed by API key (authenticated)
94
+ or client IP (anonymous). It is per-worker: if you run multiple uvicorn workers,
95
+ enforce a shared limit at the reverse proxy as well.
96
+
97
+ ---
98
+
99
+ ## 4. TLS and reverse proxy
100
+
101
+ Terminate TLS in front of uvicorn. A sample nginx configuration is in
102
+ [`deploy/nginx.conf.example`](deploy/nginx.conf.example). Minimum requirements:
103
+
104
+ - HTTPS only, HSTS enabled.
105
+ - `client_max_body_size` slightly above `MAX_UPLOAD_BYTES` (default 10 MiB).
106
+ - Forward `X-Forwarded-For` / `X-Forwarded-Proto`.
107
+ - Set `TRUST_PROXY_HEADERS=1` only when the proxy is trusted and overwrites
108
+ those headers.
109
+ - Do not expose port 8000 directly when a proxy is in use.
110
+
111
+ ```powershell
112
+ # Backend behind nginx
113
+ cd backend
114
+ python -m uvicorn main:app --host 127.0.0.1 --port 8000
115
+ ```
116
+
117
+ ---
118
+
119
+ ## 5. CORS
120
+
121
+ `CORS_ORIGINS` is a comma-separated allow-list. The default includes the local
122
+ Vite dev server on ports 8080/5173 and the preview server on 4173. Add your real
123
+ frontend origin for a deployment. Avoid `CORS_ORIGINS=*`; it is incompatible
124
+ with credentialed deployments and makes every site able to call the API.
125
+
126
+ Allowed headers include `Content-Type`, `Authorization`, and `X-API-Key`.
127
+
128
+ ---
129
+
130
+ ## 6. Input handling
131
+
132
+ - `/analyze-frame` rejects empty uploads (422), oversized uploads (413), and
133
+ undecodable images (422). The size cap is `MAX_UPLOAD_BYTES`.
134
+ - Uploaded bytes are decoded in memory and are not written to disk.
135
+ - `/images/{id}` accepts only zero-padded six-digit `.jpg` names inside
136
+ `CELEBA_IMAGES_DIR`; traversal attempts are rejected before filesystem access.
137
+ - `/generate-person` returns sanitised errors and preserves meaningful HTTP
138
+ status codes. It does not echo provider internals.
139
+ - Error responses never include stack traces or absolute paths.
140
+
141
+ Keep `MAX_UPLOAD_BYTES` aligned with the proxy limit, and keep the image
142
+ directory outside any path served by the proxy.
143
+
144
+ ---
145
+
146
+ ## 7. Dependency isolation
147
+
148
+ The project pins a tested environment in `requirements.lock` and targets Python
149
+ 3.11. Use a virtual environment; do not install into a global interpreter that
150
+ contains unrelated packages.
151
+
152
+ ```powershell
153
+ .\scripts\bootstrap.ps1 # CUDA 12.6 (verified environment)
154
+ .\scripts\bootstrap.ps1 -Cpu # CPU-only
155
+ ```
156
+
157
+ The bootstrap script creates `.venv`, installs the lock/requirements, runs the
158
+ secret check, and warns about missing AdaFace assets. It never overwrites an
159
+ existing `.env`.
160
+
161
+ ---
162
+
163
+ ## 8. Logging and incident response
164
+
165
+ - The API logs provider failures (exception text) but not prompts containing
166
+ biometric attributes at INFO level.
167
+ - Access logs contain client IPs. Treat them as personal data; shorten retention.
168
+ - If a key is exposed: revoke it at the provider, issue a replacement, restart
169
+ the backend, and search logs/archives for the old value.
170
+ - If a biometric image or report is exposed: follow the incident process in
171
+ [PRIVACY.md](PRIVACY.md).
172
+
173
+ ---
174
+
175
+ ## 9. Pre-sharing checklist
176
+
177
+ - [ ] Gemini, Groq, and Cohere keys rotated.
178
+ - [ ] `.env` files contain the new keys and are gitignored.
179
+ - [ ] `python scripts/check_secrets.py` exits 0.
180
+ - [ ] `SENTINEL_API_KEY` set if the API is reachable by anyone else.
181
+ - [ ] `CORS_ORIGINS` limited to the real frontend origin.
182
+ - [ ] TLS/reverse proxy configured; port 8000 not public.
183
+ - [ ] `Data/`, `backend/index_artifacts/`, and `backend/models_cache/` excluded
184
+ from the shared archive.
185
+ - [ ] Privacy notice and lawful basis reviewed for your jurisdiction.