This document defines the baseline security requirements for the StellarAid API. It applies to current code and to planned auth, wallet, and notification modules. Security decisions that affect HTTP responses are also described in security headers.
Startup configuration is validated by validateEnvironment in src/config/environment.ts, which src/main.ts calls before creating the Nest application. Invalid values stop startup with an error that names the affected variable without printing its value.
NODE_ENVmust bedevelopment,test, orproduction.PORTmust be an integer from1to65535and defaults to3000.DATABASE_URLmust be a PostgreSQL URL and is required whenNODE_ENV=production.REDIS_URL, when set, must be a Redis URL.CORS_ORIGINis a comma-separated allowlist of barehttp(s)origins. Wildcards, paths, query strings, and embedded credentials are rejected.STELLAR_NETWORKmust betestnetorpublic.- Numeric settings such as
REQUEST_TIMEOUT_MS,RATE_LIMIT_MAX,RATE_LIMIT_WINDOW_MS,PRISMA_SLOW_QUERY_MS, andREVOKED_TOKEN_RETENTION_DAYSmust be positive integers when set. - Boolean feature flags such as
METRICS_ENABLEDandREDIS_ENABLEDaccept onlytrueorfalse.
To add a variable, add it to environmentSchema with its type and constraints, update the relevant .env.*.example files with a placeholder value, document it here, and add a validation test. Never add a secret to a tracked example file or to an error message.
Password-based login is not implemented yet. When it is added:
- Prefer Argon2id with per-password random salt and tuned memory, iteration, and parallelism parameters. Bcrypt is acceptable only when Argon2id is unavailable in the deployment target.
- Never store plaintext, reversible encrypted passwords, or passwords in logs, metrics, analytics events, or error reports.
- Enforce a minimum length of 12 characters and check new passwords against a breached-password list where practical.
- Hash on the server before any persistence or queue write. Client-side hashing does not replace server-side hashing.
- Rate-limit login, password reset, and verification attempts per account and per source IP, with exponential backoff after repeated failures.
- Compare secrets with constant-time helpers when a framework helper is not available.
- Prefer short-lived opaque access tokens stored server-side when the API controls the full session lifecycle. If JWTs are required, allowlist the expected algorithm and issuer, verify audience and expiry, and reject
alg: none. - Access tokens should expire within 15 minutes. Refresh tokens should be long-lived only when necessary, rotated on every refresh, and checked for reuse across all sessions.
- Store only token identifiers or hashes when a token must be looked up after issuance. Do not store raw bearer tokens.
- Support explicit revocation and logout. Revoked token identifiers should be retained only for the configured
REVOKED_TOKEN_RETENTION_DAYSwindow and then deleted. - Bind tokens to the minimum required scopes and audience. Admin and analytics operations remain fail-closed until role-based authorization is implemented.
- Send cookies with
HttpOnly,Secure, andSameSite=Laxor stricter. Prefer bearer tokens in theAuthorizationheader for first-party API clients.
Wallet and email-verification flows must treat nonces as single-use authentication material.
- Generate nonces with a cryptographically secure random source using at least 128 bits of entropy.
- Store only a hash of the nonce when the plaintext is not needed after issuance.
- Apply a short expiry, bind each nonce to its purpose and wallet address or email address, and delete it atomically on successful verification.
- Never log nonces, include them in URLs that are persisted by proxies, or reuse them across flows.
- Load secrets from the process environment or a managed secret store. Never read them from committed files.
- Use distinct values per environment and rotate them when personnel change or exposure is suspected.
- Keep secrets out of source control, Swagger examples, analytics payloads, and stack traces. Redact
Authorization,Cookie,Set-Cookie, andx-api-keyheaders, plus fields namedpassword,token,secret,apiKey, orprivateKey. - Run
npm run security:secretslocally before committing. CI runsnpm run security:checkon pushes and pull requests. - For a full secret scan with Gitleaks, run
gitleaks detect --config .gitleaks.tomlwhen the Gitleaks CLI is installed.
Cross-origin access is denied by default. The API no longer reflects arbitrary Origin headers when CORS_ORIGIN is unset.
- Set
CORS_ORIGINto an explicit comma-separated allowlist, for examplehttps://app.stellaraid.example,https://admin.stellaraid.example. - Keep credentials disabled unless a cookie-based auth flow is deliberately introduced. If credentials are enabled, the allowlist must contain exact origins, never
*. - Treat
CORS_ORIGINas a security control. Adding an origin grants browsers the ability to read authenticated API responses for users of that origin. - Verify browser access with a preflight request after changing the allowlist.
Requests that authenticate with a bearer token in the Authorization header are not automatically sent by browsers, so they do not rely on ambient cookie authority. Cookie-based authentication changes that model and requires CSRF protection.
When cookie-based auth is introduced:
- Send CSRF tokens in a custom header, not only in a cookie, and validate them on every state-changing request.
- Prefer
SameSite=StrictorSameSite=Laxcookies and setSecureandHttpOnly. - Reject cross-origin state-changing requests whose
OriginorRefereris not on the explicit CORS allowlist. - Document the cookie and CSRF flow here before enabling it.
- Validate all inbound request bodies, query parameters, and route parameters with DTOs and
ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }). - Put authentication and authorization in guards, not in individual service methods. Deny by default.
- Keep secrets in the configuration layer and inject typed configuration rather than reading
process.envthroughout feature code. - Use pipes and interceptors for redaction and error normalization so early failures still produce safe responses.
- Treat admin, media upload, and auth endpoints as high-risk areas: require explicit roles, validate file content before storage, and audit sensitive actions.