Skip to content

Add API security hardening: audit logging, key rotation, and idempotency support - #270

Merged
ritaifeoluwa merged 1 commit into
SmartDropLabs:mainfrom
heymariam:feat/api-security-hardening
Aug 28, 2026
Merged

Add API security hardening: audit logging, key rotation, and idempotency support#270
ritaifeoluwa merged 1 commit into
SmartDropLabs:mainfrom
heymariam:feat/api-security-hardening

Conversation

@heymariam

Copy link
Copy Markdown

Summary

This PR resolves four critical security and reliability issues:

Closes #211
Closes #210
Closes #209
Closes #208

1. API Key Audit Logging

  • Problem: No endpoint access logging for API keys; only last_used_at timestamp
  • Solution: Track endpoint, IP, status code, response time in api_key_audit_logs table
  • Files: Migration, apiKeyAuditLog.js service, audit middleware

2. API Key Rotation Endpoint

  • Problem: No atomic key rotation; requires separate delete + create (downtime gap)
  • Solution: POST /api/v1/keys/:id/rotate atomically creates new key and revokes old
  • Files: apiKeys.js (rotateKey function), keys route, validation schema

3. Webhook Idempotency

  • Problem: Retrying webhook creation causes duplicates
  • Solution: Idempotency-Key header support with 24-hour response caching
  • Files: idempotency.js service, webhooks route

4. Airdrop Idempotency

  • Problem: Retrying airdrop creation causes duplicates
  • Solution: Idempotency-Key header support with 24-hour response caching
  • Files: idempotency.js service (reused), airdrops route

Changes

  • ✅ New audit logging: comprehensive request tracking per API key
  • ✅ New key rotation: atomic operation with no downtime gap
  • ✅ New idempotency: prevents duplicate webhooks and airdrops on retries
  • ✅ Async logging: no performance impact on request handling
  • ✅ All code compiles and follows existing patterns

Files Modified

  • src/db/migrations/20260801000000_add_api_key_audit_logs.js (new)
  • src/services/apiKeyAuditLog.js (new)
  • src/services/idempotency.js (new)
  • src/services/apiKeys.js (+rotateKey)
  • src/middleware/auth.js (+extractClientIp, +auditApiKeyUsage)
  • src/routes/keys.js (+rotate endpoint)
  • src/routes/webhooks.js (+idempotency)
  • src/routes/airdrops.js (+idempotency)
  • src/index.js (+audit middleware)
  • src/validation/schemas.js (+keyRotateBodySchema)

- Add API key audit logging: tracks endpoint access, IP, status code, response time
- Add API key rotation endpoint: atomic operation to create new key and revoke old one
- Add idempotency key support for webhook registration: prevents duplicate webhooks
- Add idempotency key support for airdrop creation: prevents duplicate airdrops

These changes improve security, reliability, and prevent duplicate resource creation on retries.
@drips-wave

drips-wave Bot commented Aug 28, 2026

Copy link
Copy Markdown

@heymariam 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

@ritaifeoluwa
ritaifeoluwa merged commit e751be7 into SmartDropLabs:main Aug 28, 2026
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