Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { cache } from "~/utils/cache.server";
|
||||
import { getClientIPAddress } from "~/utils/server/IpUtils";
|
||||
|
||||
// Brute-Force-Soft-Gate fuer den Login (R-P2a): ab dem N-ten FEHLversuch (pro IP+Email)
|
||||
// wird ein mCaptcha verlangt, bevor der Passwort-Check ueberhaupt laeuft. Freundlicher als
|
||||
// ein harter Lockout und bremst automatisierte Versuche. Der separate Failure-Counter
|
||||
// (nur Hochzaehlen bei Fehlschlag, Reset bei Erfolg) ergaenzt den generischen Rate-Limiter,
|
||||
// der weiterhin als harter Backstop dient.
|
||||
export const LOGIN_CAPTCHA_AFTER_FAILURES = 3;
|
||||
|
||||
// Zeitfenster, in dem Fehlversuche zusammengezaehlt werden (gleitend, Reset bei Erfolg).
|
||||
const FAILED_LOGIN_WINDOW_MS = 15 * 60 * 1000;
|
||||
|
||||
type FailedLoginEntry = { count: number; resetAt: number };
|
||||
type Identified = { request: Request; identifier?: string | null };
|
||||
|
||||
function failedLoginKey({ request, identifier }: Identified) {
|
||||
const ipAddress = getClientIPAddress(request.headers)?.toString() ?? "unknown";
|
||||
const normalizedIdentifier = identifier?.trim().toLowerCase() ?? "";
|
||||
const digest = createHash("sha256").update(`login-failures:${ipAddress}:${normalizedIdentifier}`).digest("hex");
|
||||
return `auth-login-failures:${digest}`;
|
||||
}
|
||||
|
||||
export function getFailedLoginCount(args: Identified): number {
|
||||
const entry = cache.get(failedLoginKey(args))?.value as FailedLoginEntry | undefined;
|
||||
if (!entry || entry.resetAt <= Date.now()) {
|
||||
return 0;
|
||||
}
|
||||
return entry.count;
|
||||
}
|
||||
|
||||
export function recordFailedLogin(args: Identified): number {
|
||||
const key = failedLoginKey(args);
|
||||
const now = Date.now();
|
||||
const entry = cache.get(key)?.value as FailedLoginEntry | undefined;
|
||||
const current = entry && entry.resetAt > now ? entry : { count: 0, resetAt: now + FAILED_LOGIN_WINDOW_MS };
|
||||
const count = current.count + 1;
|
||||
cache.set(key, { value: { count, resetAt: current.resetAt }, metadata: { createdTime: now } }, { ttl: Math.max(1, current.resetAt - now) });
|
||||
return count;
|
||||
}
|
||||
|
||||
export function clearFailedLogins(args: Identified): void {
|
||||
cache.delete(failedLoginKey(args));
|
||||
}
|
||||
|
||||
export function loginRequiresCaptcha(failedCount: number): boolean {
|
||||
return failedCount >= LOGIN_CAPTCHA_AFTER_FAILURES;
|
||||
}
|
||||
Reference in new issue
Block a user