Skip to content

security: API key rotation grace period, encryption at rest, CSP headers, SQL injection prevention - #783

Merged
Smartdevs17 merged 1 commit into
Smartdevs17:mainfrom
hardcordev:security/issues-756-754-751-750
Aug 28, 2026
Merged

security: API key rotation grace period, encryption at rest, CSP headers, SQL injection prevention#783
Smartdevs17 merged 1 commit into
Smartdevs17:mainfrom
hardcordev:security/issues-756-754-751-750

Conversation

@hardcordev

Copy link
Copy Markdown
Contributor

Summary

Hardens the API against four security gaps: immediate (no grace-period) API key rotation, database/secrets encryption relying on AWS default-managed keys instead of a dedicated key, CSP headers that were written but never applied, and a "safe" SQL query helper plus a raw-SQL repository that both allowed unescaped values/identifiers into query strings.

Changes

  • API key rotation (grace period + usage tracking): added backend/src/services/keys/rotation.ts with rotateApiKeyWithGracePeriod (keeps the predecessor key active until a configurable grace-period deadline while issuing a new key) and settleGracePeriod (lazy expiry once the deadline passes). Wired into backend/src/routes/api-keys.ts's /rotate, list, get, and usage endpoints, which now also report rotation/grace-period status. Added rotatedAt, gracePeriodEndsAt, predecessorKeyId, successorKeyId columns to the ApiKey model plus a matching migration.
  • Encryption at rest: added a customer-managed KMS key (infra/main.tf) and wired it into RDS storage encryption, RDS Performance Insights, both Secrets Manager secrets, and the AWS Backup vault, replacing reliance on AWS default-managed keys.
  • CSP / XSS prevention: applied the existing (previously unused) contentSecurityPolicy() middleware in backend/src/index.ts, and added the equivalent CSP plus supporting security headers (X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy) to frontend/next.config.ts.
  • SQL injection prevention: SQLInjectionPrevention.createSafeQuery in backend/src/middleware/security.ts no longer interpolates escaped values into the query string — it now returns genuinely parameterized $n placeholders for the caller to bind. TimescaleRepository.ts's dynamic table/column identifiers (which can't be parameterized) are now validated against a strict allowlist before being interpolated.

Issues

Resolves #756
Resolves #754
Resolves #751
Resolves #750

Verification

Manual code review completed for every change (git status / diff / full diff reviewed file-by-file). cargo build and cargo test were not run — verification was manual review only, per repository guidance. No snapshots were added or modified.

- rotate API keys with a grace period instead of immediate revocation,
  and surface grace-period/rotation status in usage tracking
- add customer-managed KMS key and wire it into RDS, secrets manager,
  and backup vault encryption at rest
- apply the existing CSP middleware/headers to the backend and frontend
- parameterize the SQL helper and validate dynamic SQL identifiers used
  by the timescale repository
@drips-wave

drips-wave Bot commented Aug 28, 2026

Copy link
Copy Markdown

@hardcordev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@vercel

vercel Bot commented Aug 28, 2026

Copy link
Copy Markdown

@hardcordev is attempting to deploy a commit to the smartdevs17's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Smartdevs17
Smartdevs17 merged commit fdf403a into Smartdevs17:main Aug 28, 2026
5 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants