-
Notifications
You must be signed in to change notification settings - Fork 2
feat: operationalize SecurityAuditService across core auth flows #724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+332
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
be5e791
feat: operationalize SecurityAuditService across core auth flows (#535)
2witstudios aadb476
fix: add missing securityAudit mock to login-redirect test
2witstudios 352f462
refactor: consolidate audit exports into single statement
2witstudios 0f55fb1
ci: add workflow_dispatch trigger to test suite
2witstudios 23b3237
merge: resolve conflicts with master and align audit imports
2witstudios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
160 changes: 160 additions & 0 deletions
160
packages/lib/src/audit/__tests__/security-audit-adapter.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
|
|
||
| vi.mock('../security-audit', () => ({ | ||
| securityAudit: { | ||
| logEvent: vi.fn().mockResolvedValue(undefined), | ||
| logAuthSuccess: vi.fn().mockResolvedValue(undefined), | ||
| logAuthFailure: vi.fn().mockResolvedValue(undefined), | ||
| logAccessDenied: vi.fn().mockResolvedValue(undefined), | ||
| logTokenCreated: vi.fn().mockResolvedValue(undefined), | ||
| logTokenRevoked: vi.fn().mockResolvedValue(undefined), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('@pagespace/db', () => ({ | ||
| securityAuditLog: {}, | ||
| })); | ||
|
|
||
| import { auditAuthEvent, auditSecurityEvent } from '../security-audit-adapter'; | ||
| import { securityAudit } from '../security-audit'; | ||
|
|
||
| describe('Security Audit Adapter', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('auditAuthEvent', () => { | ||
| it('dispatches login success to securityAudit.logEvent', () => { | ||
| auditAuthEvent('login', 'user-1', 'test@example.com', '1.2.3.4'); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: 'auth.login.success', | ||
| userId: 'user-1', | ||
| ipAddress: '1.2.3.4', | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('dispatches login failure with risk score', () => { | ||
| auditAuthEvent('failed', undefined, 'bad@example.com', '1.2.3.4', 'invalid_password'); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: 'auth.login.failure', | ||
| riskScore: 0.3, | ||
| details: expect.objectContaining({ | ||
| reason: 'invalid_password', | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('dispatches logout event', () => { | ||
| auditAuthEvent('logout', 'user-1', undefined, '1.2.3.4'); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: 'auth.logout', | ||
| userId: 'user-1', | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('dispatches token refresh event', () => { | ||
| auditAuthEvent('refresh', 'user-1', undefined, '1.2.3.4'); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: 'auth.token.created', | ||
| userId: 'user-1', | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('masks email in audit details', () => { | ||
| auditAuthEvent('login', 'user-1', 'longuser@example.com', '1.2.3.4'); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| details: expect.objectContaining({ | ||
| email: 'lo***@example.com', | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('does not throw when securityAudit.logEvent rejects', () => { | ||
| vi.mocked(securityAudit.logEvent).mockRejectedValueOnce(new Error('DB down')); | ||
|
|
||
| expect(() => { | ||
| auditAuthEvent('login', 'user-1', 'test@example.com', '1.2.3.4'); | ||
| }).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('auditSecurityEvent', () => { | ||
| it('dispatches unauthorized as access denied', () => { | ||
| auditSecurityEvent('unauthorized', { | ||
| userId: 'user-1', | ||
| ip: '1.2.3.4', | ||
| reason: 'csrf_failed', | ||
| }); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: 'authz.access.denied', | ||
| userId: 'user-1', | ||
| ipAddress: '1.2.3.4', | ||
| details: expect.objectContaining({ | ||
| reason: 'csrf_failed', | ||
| originalEvent: 'unauthorized', | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('dispatches rate_limit event', () => { | ||
| auditSecurityEvent('rate_limit', { | ||
| ip: '1.2.3.4', | ||
| endpoint: '/api/auth/login', | ||
| }); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: 'security.rate.limited', | ||
| ipAddress: '1.2.3.4', | ||
| riskScore: 0.4, | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('dispatches account_locked_login_attempt with high risk score', () => { | ||
| auditSecurityEvent('account_locked_login_attempt', { | ||
| email: 'locked@test.com', | ||
| ip: '1.2.3.4', | ||
| }); | ||
|
|
||
| expect(securityAudit.logEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: 'security.brute.force.detected', | ||
| riskScore: 0.8, | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('skips unmapped security events without calling logEvent', () => { | ||
| auditSecurityEvent('login_csrf_missing', { ip: '1.2.3.4' }); | ||
|
|
||
| expect(securityAudit.logEvent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not throw when securityAudit.logEvent rejects', () => { | ||
| vi.mocked(securityAudit.logEvent).mockRejectedValueOnce(new Error('DB down')); | ||
|
|
||
| expect(() => { | ||
| auditSecurityEvent('unauthorized', { userId: 'user-1' }); | ||
| }).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 591
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 2312
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 45704
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 807
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 47
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 1262
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 1969
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 47
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 853
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 1119
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 1049
🏁 Script executed:
Repository: 2witstudios/PageSpace
Length of output: 810
Mask email before passing to
securityAudit.logAuthFailureto comply with PII retention policies.The email is passed directly to
logAuthFailurewithout masking. Unlike the signup-passkey route which masks PII before logging (using the patternemail.substring(0, 3) + '***@' + domain), the login route passes the full email unmasked. This is inconsistent and fails to comply with data retention policies.Apply the same masking pattern used in signup-passkey/route.ts before the call:
Note: Also review
logAuthEventandtrackAuthEventcalls on the same line block, as they also receive the unmasked email.🤖 Prompt for AI Agents