feat(backend): Audit Logger — signature verification, SQL optimization, circuit-breaker error recovery, security hardening#848
Merged
emdevelopa merged 1 commit intoMay 28, 2026
Conversation
…n, error recovery, security audit - Add verifyAuditSignature() using crypto.timingSafeEqual for constant-time HMAC comparison to prevent timing-oracle attacks (issue emdevelopa#769) - Add validateAuditAction() with an allowlist of permitted action identifiers to block log-injection through crafted action strings (issue emdevelopa#772) - Replace two-query COUNT + SELECT pattern in getAuditLogs with a single COUNT(*) OVER() window-function query, halving round-trips to the DB; leverages the existing (merchant_id, timestamp) composite index (issue emdevelopa#770) - Add circuit-breaker state to insertAuditLog in both audit.js and auditService.js: after a configurable failure threshold the circuit opens and all writes route directly to the fallback log for a reset window before the next probe attempt, preventing DB overload cascades (issue emdevelopa#771) - Expand test coverage: 35 tests across audit-security, audit, and auditService — all passing Closes emdevelopa#769 Closes emdevelopa#770 Closes emdevelopa#771 Closes emdevelopa#772
|
@gboigwe is attempting to deploy a commit to the Emmanuel's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@gboigwe 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.
Summary
This PR addresses all four Audit Logger backend issues assigned from the Stellar Wave Program:
Changes
#769 — Cryptographic Signature Verification (
audit-security.js)verifyAuditSignature(payload, signature, secret)which recomputes the expected HMAC and compares with the stored value usingcrypto.timingSafeEqual(constant-time comparison). This prevents timing-oracle attacks where an attacker could deduce a valid signature byte-by-byte by measuring response times.falsefor null signatures, missing secrets, or length-mismatched values — no exceptions leak information.#770 — SQL Query Optimization (
auditService.js)SELECT COUNT(*)+SELECT … LIMIT … OFFSET) ingetAuditLogswith a single query usingCOUNT(*) OVER()(PostgreSQL window function).(merchant_id, timestamp)(migration20260425000000) is used by theORDER BY timestamp DESCclause, keeping query plans efficient as the table grows.total_countcolumn is stripped from returned row objects before the response is sent.#771 — Enhanced Error Recovery (
audit.js+auditService.js)insertAuditLogimplementations.AUDIT_CIRCUIT_FAILURE_THRESHOLD(default 5) consecutive DB failures the circuit opens; all subsequent write attempts bypass the DB and route directly to the fallback file log forAUDIT_CIRCUIT_RESET_MS(default 60 s).#772 — Security Audit Hardening (
audit-security.js+audit.js+auditService.js)validateAuditAction(action)with an explicit allowlist of permitted action identifiers (login,update,create,delete,payment_initiated, etc.).logLoginAttemptandauditService.logEventnow reject any action value not in the allowlist before it reaches the DB, blocking log-injection attacks via crafted action strings.$1,$2, …) — no SQL injection surface.sanitizeAuditKeyandsanitizeAuditValue.timingSafeEqual— no timing-oracle leakage.Test Coverage
35 tests passing across three files:
audit-security.test.js:verifyAuditSignature(valid, tampered payload, tampered signature, null signature, no secret, length mismatch),validateAuditAction(allowed, disallowed, case-insensitive, null/undefined)audit.test.js: circuit-breaker opens after repeated failures, fallback routing when circuit is open, circuit resets on successauditService.test.js: single window-function query forgetAuditLogs, zero-row edge case, page/limit clamping, action validation drop, circuit-breaker opens and routes to fallbackAll pre-existing tests continue to pass.