-
Notifications
You must be signed in to change notification settings - Fork 13.5k
feat: implement smart login failure aggregation to mitigate write amplification #40148
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
Open
Saraabodeeb
wants to merge
7
commits into
RocketChat:develop
Choose a base branch
from
Saraabodeeb:feat/clean-anomaly-aggregator
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−15
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
813b919
feat(auth): implement smart rate-limiting and anomaly aggregation for…
Saraabodeeb caf2e40
fix: refine aggregation logic and address AI reviewer feedback
Saraabodeeb 5554c57
fix: implement LRU-style eviction to prevent buffer blind spots
Saraabodeeb 5ec70a2
fix: implement LRU-style eviction to prevent buffer blind spots
Saraabodeeb 05db531
feat: track IP and User independently to detect spray attacks
Saraabodeeb ecff4b1
chore: improve type safety for timeoutId in LoginAnomalyAggregator
Saraabodeeb 6ab419b
Merge branch 'develop' into feat/clean-anomaly-aggregator
Saraabodeeb 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
83 changes: 83 additions & 0 deletions
83
apps/meteor/app/authentication/server/lib/LoginAnomalyAggregator.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,83 @@ | ||
| interface FailedAttemptInfo { | ||
| key: string; | ||
| count: number; | ||
| firstAttempt: Date; | ||
| lastAttempt: Date; | ||
| timeoutId?: ReturnType<typeof setTimeout>; | ||
| } | ||
|
|
||
| class LoginAnomalyAggregator { | ||
| private buffer: Map<string, FailedAttemptInfo> = new Map(); | ||
| private readonly FLUSH_WINDOW_MS = 45 * 1000; | ||
| private readonly MAX_BUFFER_SIZE = 1000; | ||
|
|
||
| public logFailure(ip: string, userId?: string): void { | ||
| const now = new Date(); | ||
|
|
||
| const keys = [`ip:${ip}`]; | ||
| if (userId) { | ||
| keys.push(`user:${userId}`); | ||
| } | ||
|
|
||
| for (const key of keys) { | ||
| this.processFailureForKey(key, now); | ||
| } | ||
| } | ||
|
|
||
| private processFailureForKey(key: string, now: Date): void { | ||
| if (this.buffer.size >= this.MAX_BUFFER_SIZE && !this.buffer.has(key)) { | ||
| const oldestKey = this.buffer.keys().next().value; | ||
| if (oldestKey) { | ||
| void this.flushEvent(oldestKey); | ||
| } | ||
| } | ||
|
|
||
| if (this.buffer.has(key)) { | ||
| const entry = this.buffer.get(key)!; | ||
| entry.count += 1; | ||
| entry.lastAttempt = now; | ||
| } else { | ||
| const entry: FailedAttemptInfo = { | ||
| key, | ||
| count: 1, | ||
| firstAttempt: now, | ||
| lastAttempt: now, | ||
| }; | ||
|
|
||
| entry.timeoutId = setTimeout(() => { | ||
| void this.flushEvent(key); | ||
| }, this.FLUSH_WINDOW_MS); | ||
| this.buffer.set(key, entry); | ||
| } | ||
| } | ||
|
|
||
| private async flushEvent(key: string): Promise<void> { | ||
| const entry = this.buffer.get(key); | ||
| if (!entry) return; | ||
|
|
||
| if (entry.timeoutId) { | ||
| clearTimeout(entry.timeoutId); | ||
| } | ||
|
|
||
| this.buffer.delete(key); | ||
|
|
||
| if (entry.count > 3) { | ||
| await this.recordAnomaly(entry); | ||
| } | ||
| } | ||
|
|
||
| private async recordAnomaly(entry: Omit<FailedAttemptInfo, 'timeoutId'>): Promise<void> { | ||
| const severity = entry.count >= 10 ? 'high' : 'medium'; | ||
| const durationInSeconds = Math.round((entry.lastAttempt.getTime() - entry.firstAttempt.getTime()) / 1000); | ||
|
|
||
| console.error( | ||
| `🔥 [Auth Anomaly Aggregator] ` + | ||
| `Target: ${entry.key} | ` + | ||
| `Attempts: ${entry.count} | ` + | ||
| `Window: ${durationInSeconds}s | ` + | ||
| `Severity: ${severity}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export const anomalyAggregator = new LoginAnomalyAggregator(); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.