feat: Add per-endpoint audit logging for /api/forecast mutations - #851
Merged
greatest0fallt1me merged 1 commit intoJul 27, 2026
Merged
Conversation
Closes CalloraOrg#687 Implement audit logging for all state-changing mutations on /api/forecast: - POST /api/forecast (create) - audited as 'forecast.create' - PATCH /api/forecast/:id (update) - audited as 'forecast.update' - DELETE /api/forecast/:id (delete) - audited as 'forecast.delete' Key features: - Before/after state capture with correct ordering (before fetched BEFORE mutation) - Actor identity from authenticated context only (never from request body) - Correlation ID propagation via X-Request-Id header - Forensic metadata (clientIp, userAgent, bodyHash) - Input validation at boundary with Zod schemas - Standardized error handling with AppError hierarchy - 35 comprehensive test cases covering all scenarios - 100% coverage on audit logic Each audit record includes: - event: forecast.create|update|delete - actor: authenticated user ID (from JWT) - before/after: complete state before and after mutation - tenantId, clientIp, userAgent, correlationId, bodyHash for forensics Security notes: - Actor always from res.locals.authenticatedUser (JWT), never request body - Before-state captured BEFORE applying mutations (prevents reference sharing bug) - Audit persistence failures block mutations (fail-safe for compliance) - All mutations require JWT authentication
|
@onyemaechiezekiel9 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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: Add per-endpoint audit logging for /api/forecast mutations
Closes #687
Summary
Implements comprehensive audit logging for all state-changing mutations on the
/api/forecastendpoint. Every POST (create), PATCH (update), and DELETE (delete) operation is now audited with complete before/after state capture, authenticated actor recording, correlation ID propagation, and forensic metadata.Mutations Audited
/api/forecastforecast.create/api/forecast/:idforecast.update/api/forecast/:idforecast.delete/api/forecast/api/forecast/:idKey Features
1. Correct Before/After State Capture
2. Authenticated Actor Recording
res.locals.authenticatedUser.id(JWT context)3. Correlation ID Propagation
correlationId4. Forensic Metadata
Each audit record includes:
clientIp: Proxy-aware client IP addressuserAgent: Request User-Agent headerbodyHash: HMAC-SHA256(request body, secret) for tamper detectioncorrelationId: X-Request-Id for trace linking5. Input Validation at Boundary
6. Standardized Error Handling
AppErrorhierarchyAudit Record Example