Add unit tests for security analysis utilities#687
Open
BigJohn-dev wants to merge 1 commit into
Open
Conversation
- Implement tests for various detection functions including rate anomalies, off-hours activity, actor velocity anomalies, multi-actor bursts, escalation chains, repeated failures, session drift, and category concentration. - Create test factories for generating mock entries to simulate different scenarios. - Validate scoring and risk computation functions with comprehensive test cases. - Ensure integration tests cover the full analysis pipeline with realistic audit data.
|
@BigJohn-dev is attempting to deploy a commit to the nanle-code's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@BigJohn-dev 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.
Closes #587
Pull Request: AI-Powered Security Audit Trail Analysis System
Summary
Implements a comprehensive AI-driven security analysis engine that monitors audit log trails in real-time to identify anomalies, behavioral patterns, and compliance violations. Provides actionable security insights with risk scoring and recommendations.
Motivation
The existing audit system (
src/utils/audit.js,src/lib/securityEvents.js) records tamper-evident, hash-chained audit entries and runs lightweight brute-force detection — but lacks deeper statistical analysis, pattern recognition, and a unified risk scoring model. This PR adds a dedicated analysis layer that operates on top of the existing audit infrastructure.Files Changed / Added
src/utils/securityAnalysis.jssrc/hooks/useSecurityAnalysis.jssrc/components/dashboard/SecurityAuditPanel.jsxtests/unit/utils/securityAnalysis.test.jsNo existing files were modified.
Architecture
Design principle: The core engine consists entirely of pure functions operating on audit entry arrays. No side effects, no global state. This makes every detector independently testable and composable.
Core Engine —
src/utils/securityAnalysis.jsAnomaly Detection
detectRateAnomalies()detectOffHoursActivity()detectActorVelocityAnomalies()detectMultiActorBursts()Pattern Recognition
detectEscalationChains()detectRepeatedFailures()detectSessionDrift()detectCategoryConcentration()Risk Scoring
(severity_weight + outcome_penalty) × category_multiplierConfiguration
All thresholds and weights are configurable via
DEFAULT_CONFIG:Report Generation
generateReport()runs all 8 detectors and produces:Streaming Engine
createStreamingEngine()maintains a bounded buffer (max 2000 entries) with lazy-evaluated, invalidated caches:React Integration —
src/hooks/useSecurityAnalysis.jssubscribeAudit()for real-time entry notificationsfilterEntries()Dashboard UI —
src/components/dashboard/SecurityAuditPanel.jsxFollows existing project conventions:
var(--bg-card),var(--cyan), etc.)Card.tsxdesign system primitiveSections:
Test Coverage
61 tests across 10
describeblocks:detectRateAnomaliesdetectOffHoursActivitydetectActorVelocityAnomaliesdetectMultiActorBurstsdetectEscalationChainsdetectRepeatedFailuresdetectSessionDriftdetectCategoryConcentrationscoreEntry/computeAggregateRisk/riskGradegenerateReport/createStreamingEngineAll 61 tests pass. No regressions in existing test suite (207 total tests pass).
Acceptance Criteria Verification
Integration Points
src/utils/audit.js) — reads from the same ring buffer and IndexedDB persistencesrc/lib/securityEvents.js) — analysis runs on the same entries thattrackSecurityEvent()recordssrc/hooks/useAudit.js) —useSecurityAnalysisuses the samegetAuditEntriesandsubscribeAuditAPIsCard.tsxprimitive, CSS custom properties, Lucide iconsNotes
vitest@4.1.5was incompatible withvite@5.4.21(requires vite 6+). Tests ran successfully usingvitest@2.1.9.src/hooks/useSearch.js:39(stray...spread) preventsvite build— unrelated to this PR.src/components/dashboard/tests/Overview.test.jsx(vi.mock hoisting issue) — unrelated to this PR.