Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+44
-5
@@ -10,7 +10,8 @@ import { createHash } from "node:crypto";
|
||||
|
||||
const HIBP_API_BASE = "https://api.pwnedpasswords.com/range/";
|
||||
const HIBP_TIMEOUT_MS = 3000;
|
||||
const ABSOLUTE_MAX_LENGTH = 1024;
|
||||
const HIBP_RESPONSE_MAX_BYTES = 128 * 1024;
|
||||
export const PASSWORD_ABSOLUTE_MAX_LENGTH = 1024;
|
||||
|
||||
export type PasswordPolicyConfig = {
|
||||
minLength: number;
|
||||
@@ -88,8 +89,8 @@ export async function validatePassword(
|
||||
if (plain.length < policy.minLength) {
|
||||
errors.push({ code: "auth.password.errors.tooShort", args: { minLength: policy.minLength } });
|
||||
}
|
||||
if (plain.length > ABSOLUTE_MAX_LENGTH) {
|
||||
errors.push({ code: "auth.password.errors.tooLong", args: { maxLength: ABSOLUTE_MAX_LENGTH } });
|
||||
if (plain.length > PASSWORD_ABSOLUTE_MAX_LENGTH) {
|
||||
errors.push({ code: "auth.password.errors.tooLong", args: { maxLength: PASSWORD_ABSOLUTE_MAX_LENGTH } });
|
||||
}
|
||||
if (policy.requireUpper && !/[A-Z]/.test(plain)) {
|
||||
errors.push({ code: "auth.password.errors.missingUpper" });
|
||||
@@ -136,7 +137,14 @@ async function checkHibp(plain: string, maxOccurrences: number, fetcher?: typeof
|
||||
|
||||
let response: Response;
|
||||
try {
|
||||
response = await withTimeout(usedFetcher(url, { headers: { "Add-Padding": "true" } }), HIBP_TIMEOUT_MS);
|
||||
response = await withTimeout(
|
||||
usedFetcher(url, {
|
||||
headers: { "Add-Padding": "true" },
|
||||
redirect: "error",
|
||||
signal: AbortSignal.timeout(HIBP_TIMEOUT_MS),
|
||||
}),
|
||||
HIBP_TIMEOUT_MS
|
||||
);
|
||||
} catch {
|
||||
return "bypassed";
|
||||
}
|
||||
@@ -144,7 +152,7 @@ async function checkHibp(plain: string, maxOccurrences: number, fetcher?: typeof
|
||||
|
||||
let body: string;
|
||||
try {
|
||||
body = await response.text();
|
||||
body = await readLimitedText(response, HIBP_RESPONSE_MAX_BYTES);
|
||||
} catch {
|
||||
return "bypassed";
|
||||
}
|
||||
@@ -159,6 +167,37 @@ async function checkHibp(plain: string, maxOccurrences: number, fetcher?: typeof
|
||||
return "clean";
|
||||
}
|
||||
|
||||
async function readLimitedText(response: Response, maxBytes: number): Promise<string> {
|
||||
const contentLength = response.headers.get("content-length");
|
||||
if (contentLength && /^\d+$/u.test(contentLength) && Number(contentLength) > maxBytes) {
|
||||
await response.body?.cancel().catch(() => undefined);
|
||||
throw new Error("hibp_response_too_large");
|
||||
}
|
||||
if (!response.body) return "";
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const chunks: Uint8Array[] = [];
|
||||
let totalBytes = 0;
|
||||
while (true) {
|
||||
const chunk = await reader.read();
|
||||
if (chunk.done) break;
|
||||
totalBytes += chunk.value.byteLength;
|
||||
if (totalBytes > maxBytes) {
|
||||
await reader.cancel().catch(() => undefined);
|
||||
throw new Error("hibp_response_too_large");
|
||||
}
|
||||
chunks.push(chunk.value);
|
||||
}
|
||||
|
||||
const bytes = new Uint8Array(totalBytes);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
bytes.set(chunk, offset);
|
||||
offset += chunk.byteLength;
|
||||
}
|
||||
return new TextDecoder().decode(bytes);
|
||||
}
|
||||
|
||||
async function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const timeout = new Promise<T>((_, reject) => {
|
||||
|
||||
Reference in new issue
Block a user