security: timing-safe API key comparison, remove hardcoded DB password
- Use hmac.compare_digest for constant-time key check (router.py) - Move POSTGRES_PASSWORD out of docker-compose.yml into .env (was a public default "corvid" now that the repo is public) - Add .env.example with placeholder values Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
3bd431bea0
commit
961fc67528
3 changed files with 9 additions and 2 deletions
3
.env.example
Normal file
3
.env.example
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
DATABASE_URL=postgresql+asyncpg://corvid:changeme@postgres:5432/corvid
|
||||||
|
TICKETS_API_KEY=changeme-use-a-long-random-value
|
||||||
|
POSTGRES_PASSWORD=changeme
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import hmac
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query, Request
|
from fastapi import APIRouter, Depends, Header, HTTPException, Query, Request
|
||||||
|
|
@ -101,7 +102,9 @@ def make_router(api_key: str, get_db, require_user=None) -> APIRouter:
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
def _api_key_ok(x_api_key: str | None) -> bool:
|
def _api_key_ok(x_api_key: str | None) -> bool:
|
||||||
return bool(api_key and x_api_key == api_key)
|
if not api_key or not x_api_key:
|
||||||
|
return False
|
||||||
|
return hmac.compare_digest(x_api_key, api_key)
|
||||||
|
|
||||||
def require_ticket_auth(
|
def require_ticket_auth(
|
||||||
request: Request,
|
request: Request,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: corvid
|
POSTGRES_USER: corvid
|
||||||
POSTGRES_PASSWORD: corvid
|
|
||||||
POSTGRES_DB: corvid
|
POSTGRES_DB: corvid
|
||||||
volumes:
|
volumes:
|
||||||
- pgdata:/var/lib/postgresql/data
|
- pgdata:/var/lib/postgresql/data
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue