mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-21 15:05:56 +00:00
fix(webdav): skip credential-less probes and amortize the lockout sweep
A request with no Authorization header is the client's challenge probe, not a password guess: answer 401 without spending one of the caller's attempts, so a client that probes before sending credentials is not locked out. Sweep expired failure entries at most once per second instead of on every failure once the map passes 256 clients, so a burst of distinct addresses no longer turns each failed attempt into a full scan. Active lockouts are kept.
This commit is contained in:
committed by
diegosouzapw
parent
20334e5dc7
commit
b15396963a
@@ -252,9 +252,31 @@ export function verifyBasicAuth(authHeader, expectedUsername, expectedPassword)
|
||||
export const WEBDAV_AUTH_MAX_FAILURES = 10;
|
||||
export const WEBDAV_AUTH_WINDOW_MS = 60 * 1000; // 60 seconds
|
||||
export const WEBDAV_AUTH_LOCKOUT_MS = 5 * 60 * 1000; // 5 minutes
|
||||
/** Expired failure entries are swept at most once per interval, never on every failure. */
|
||||
export const WEBDAV_AUTH_SWEEP_INTERVAL_MS = 1000;
|
||||
|
||||
/** @type {Map<string, { count: number, resetAt: number, lockedUntil: number | null }>} */
|
||||
const authFailureMap = new Map();
|
||||
const WEBDAV_AUTH_SWEEP_THRESHOLD = 256;
|
||||
let lastAuthFailureSweepAt = 0;
|
||||
|
||||
/**
|
||||
* Drop expired failure entries once the map grows past the threshold. Rate-limited to one pass
|
||||
* per WEBDAV_AUTH_SWEEP_INTERVAL_MS so a burst of distinct clients cannot turn every failed
|
||||
* attempt into a full scan. Active lockouts are kept until they expire.
|
||||
*
|
||||
* @param {number} now
|
||||
*/
|
||||
function sweepExpiredWebdavAuthFailures(now) {
|
||||
if (authFailureMap.size <= WEBDAV_AUTH_SWEEP_THRESHOLD) return;
|
||||
if (now - lastAuthFailureSweepAt < WEBDAV_AUTH_SWEEP_INTERVAL_MS) return;
|
||||
lastAuthFailureSweepAt = now;
|
||||
for (const [k, v] of authFailureMap) {
|
||||
if ((v.lockedUntil && v.lockedUntil <= now) || (!v.lockedUntil && now > v.resetAt)) {
|
||||
authFailureMap.delete(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise a client IP address for the WebDAV rate-limit key.
|
||||
@@ -322,14 +344,7 @@ export function checkWebdavRateLimit(rawIp, now = Date.now()) {
|
||||
*/
|
||||
export function recordWebdavAuthFailure(rawIp, now = Date.now()) {
|
||||
const ip = normalizeWebdavClientIp(rawIp);
|
||||
|
||||
if (authFailureMap.size > 256) {
|
||||
for (const [k, v] of authFailureMap) {
|
||||
if ((v.lockedUntil && v.lockedUntil <= now) || (!v.lockedUntil && now > v.resetAt)) {
|
||||
authFailureMap.delete(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
sweepExpiredWebdavAuthFailures(now);
|
||||
|
||||
const existing = authFailureMap.get(ip);
|
||||
let state;
|
||||
@@ -366,11 +381,34 @@ export function resetWebdavAuthFailures(rawIp) {
|
||||
authFailureMap.delete(ip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Number of client addresses with tracked failures (for testing).
|
||||
*
|
||||
* @returns {number}
|
||||
*/
|
||||
export function getWebdavAuthFailureStateSize() {
|
||||
return authFailureMap.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all rate limiting state (for testing).
|
||||
*/
|
||||
export function clearWebdavRateLimits() {
|
||||
authFailureMap.clear();
|
||||
lastAuthFailureSweepAt = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count a rejected Basic-auth attempt. A request that carries no Authorization header is the
|
||||
* client's challenge probe, not a password guess: it gets the 401 without spending an attempt.
|
||||
*
|
||||
* @param {string | undefined} authHeader
|
||||
* @param {string} clientIp
|
||||
* @returns {{ allowed: boolean, retryAfter?: number }}
|
||||
*/
|
||||
function recordRejectedWebdavAuth(authHeader, clientIp) {
|
||||
if (!authHeader) return { allowed: true };
|
||||
return recordWebdavAuthFailure(clientIp);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1064,8 +1102,9 @@ export async function maybeHandleWebdav(req, res) {
|
||||
}
|
||||
|
||||
// Authenticate
|
||||
if (!verifyBasicAuth(req.headers["authorization"], config.username, config.password)) {
|
||||
const failureDecision = recordWebdavAuthFailure(clientIp);
|
||||
const authHeader = req.headers["authorization"];
|
||||
if (!verifyBasicAuth(authHeader, config.username, config.password)) {
|
||||
const failureDecision = recordRejectedWebdavAuth(authHeader, clientIp);
|
||||
if (!failureDecision.allowed) {
|
||||
sendError(res, 429, "Too Many Requests", {
|
||||
"Retry-After": String(failureDecision.retryAfter || 60),
|
||||
|
||||
@@ -1,26 +1,94 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import http from "node:http";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { createRequire } from "node:module";
|
||||
import {
|
||||
checkWebdavRateLimit,
|
||||
recordWebdavAuthFailure,
|
||||
resetWebdavAuthFailures,
|
||||
clearWebdavRateLimits,
|
||||
getWebdavAuthFailureStateSize,
|
||||
maybeHandleWebdav,
|
||||
normalizeWebdavClientIp,
|
||||
getWebdavClientIp,
|
||||
WEBDAV_AUTH_MAX_FAILURES,
|
||||
WEBDAV_AUTH_WINDOW_MS,
|
||||
WEBDAV_AUTH_LOCKOUT_MS,
|
||||
WEBDAV_AUTH_SWEEP_INTERVAL_MS,
|
||||
} from "../../scripts/dev/webdav-handler.mjs";
|
||||
|
||||
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
|
||||
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-webdav-rate-limit-"));
|
||||
const TEST_VAULT_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-webdav-rate-limit-vault-"));
|
||||
|
||||
test.beforeEach(() => {
|
||||
clearWebdavRateLimits();
|
||||
});
|
||||
|
||||
test.after(() => {
|
||||
clearWebdavRateLimits();
|
||||
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
||||
fs.rmSync(TEST_VAULT_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
||||
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
|
||||
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
|
||||
});
|
||||
|
||||
/** Seed the obsidian WebDAV settings the handler reads straight from storage.sqlite. */
|
||||
function seedWebdavConfig(username: string, password: string) {
|
||||
const Database = createRequire(import.meta.url)("better-sqlite3");
|
||||
const db = new Database(path.join(TEST_DATA_DIR, "storage.sqlite"));
|
||||
try {
|
||||
db.exec(
|
||||
"CREATE TABLE IF NOT EXISTS key_value (namespace TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (namespace, key))"
|
||||
);
|
||||
const upsert = db.prepare(
|
||||
"INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES (?, ?, ?)"
|
||||
);
|
||||
upsert.run("obsidian", "webdav_enabled", JSON.stringify(true));
|
||||
upsert.run("obsidian", "webdav_username", JSON.stringify(username));
|
||||
upsert.run("obsidian", "webdav_password", JSON.stringify(password));
|
||||
upsert.run("obsidian", "vault_path", JSON.stringify(TEST_VAULT_DIR));
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
/** Run one request through the real handler and return the response status. */
|
||||
function webdavStatus(headers: Record<string, string>, peer: string): Promise<number> {
|
||||
const req = Object.assign(new EventEmitter(), {
|
||||
method: "PROPFIND",
|
||||
url: "/api/v1/webdav/",
|
||||
headers,
|
||||
socket: { remoteAddress: peer },
|
||||
}) as unknown as http.IncomingMessage;
|
||||
setImmediate(() => req.emit("end"));
|
||||
return new Promise((resolve) => {
|
||||
let status = 200;
|
||||
const res = {
|
||||
headersSent: false,
|
||||
writeHead(code: number) {
|
||||
status = code;
|
||||
this.headersSent = true;
|
||||
},
|
||||
write() {},
|
||||
end() {
|
||||
resolve(status);
|
||||
},
|
||||
destroy() {
|
||||
resolve(status);
|
||||
},
|
||||
on() {
|
||||
return this;
|
||||
},
|
||||
} as unknown as http.ServerResponse;
|
||||
void maybeHandleWebdav(req, res);
|
||||
});
|
||||
}
|
||||
|
||||
test("normalizeWebdavClientIp strips IPv4-mapped IPv6 prefix and trims whitespace", () => {
|
||||
assert.equal(normalizeWebdavClientIp("::ffff:192.168.1.10"), "192.168.1.10");
|
||||
assert.equal(normalizeWebdavClientIp(" 10.0.0.1 "), "10.0.0.1");
|
||||
@@ -133,3 +201,59 @@ test("rotating X-Forwarded-For from one direct client still reaches the lockout"
|
||||
assert.equal(last.allowed, false, "the peer is locked out whatever header it sends");
|
||||
assert.equal(checkWebdavRateLimit("203.0.113.51", t0 + 2_000).allowed, false);
|
||||
});
|
||||
|
||||
test("a request without credentials gets the 401 challenge but does not spend an attempt", async () => {
|
||||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||||
seedWebdavConfig("obsidian", "right-password");
|
||||
const peer = "203.0.113.60";
|
||||
const basic = (pass: string) => `Basic ${Buffer.from(`obsidian:${pass}`).toString("base64")}`;
|
||||
|
||||
// WebDAV clients probe without credentials to read the challenge; those probes are not guesses.
|
||||
for (let i = 0; i < WEBDAV_AUTH_MAX_FAILURES + 5; i++) {
|
||||
assert.equal(await webdavStatus({}, peer), 401, `credential-less probe ${i + 1} gets 401`);
|
||||
}
|
||||
assert.equal(checkWebdavRateLimit(peer).allowed, true);
|
||||
|
||||
// A wrong password still counts, and the right one still gets in.
|
||||
assert.equal(await webdavStatus({ authorization: basic("wrong") }, peer), 401);
|
||||
assert.equal(getWebdavAuthFailureStateSize(), 1);
|
||||
assert.equal(
|
||||
await webdavStatus({ authorization: basic("right-password"), depth: "0" }, peer),
|
||||
207
|
||||
);
|
||||
});
|
||||
|
||||
test("expired failure entries are swept at most once per interval, not on every failure", () => {
|
||||
const t0 = 10_000_000;
|
||||
// More than the sweep threshold of distinct clients, all inside one window.
|
||||
for (let i = 0; i < 300; i++) recordWebdavAuthFailure(`10.1.${i >> 8}.${i & 255}`, t0);
|
||||
assert.equal(getWebdavAuthFailureStateSize(), 300);
|
||||
|
||||
// Once the window has passed, the next failure sweeps the expired entries.
|
||||
const t1 = t0 + WEBDAV_AUTH_WINDOW_MS + 1;
|
||||
recordWebdavAuthFailure("10.2.0.1", t1);
|
||||
assert.equal(getWebdavAuthFailureStateSize(), 1);
|
||||
|
||||
// Entries that expire right after that sweep are not re-scanned by every later failure...
|
||||
for (let i = 0; i < 300; i++) {
|
||||
recordWebdavAuthFailure(`10.3.${i >> 8}.${i & 255}`, t1 - WEBDAV_AUTH_WINDOW_MS - 10);
|
||||
}
|
||||
recordWebdavAuthFailure("10.2.0.2", t1 + 1);
|
||||
assert.equal(getWebdavAuthFailureStateSize(), 302, "no sweep inside the interval");
|
||||
|
||||
// ...but they go on the first failure after the interval.
|
||||
recordWebdavAuthFailure("10.2.0.3", t1 + WEBDAV_AUTH_SWEEP_INTERVAL_MS);
|
||||
assert.equal(getWebdavAuthFailureStateSize(), 3);
|
||||
});
|
||||
|
||||
test("the sweep never drops an active lockout", () => {
|
||||
const t0 = 20_000_000;
|
||||
const locked = "198.51.100.200";
|
||||
for (let i = 0; i < WEBDAV_AUTH_MAX_FAILURES; i++) recordWebdavAuthFailure(locked, t0);
|
||||
for (let i = 0; i < 300; i++) recordWebdavAuthFailure(`10.4.${i >> 8}.${i & 255}`, t0);
|
||||
|
||||
const afterWindow = t0 + WEBDAV_AUTH_WINDOW_MS + WEBDAV_AUTH_SWEEP_INTERVAL_MS;
|
||||
recordWebdavAuthFailure("10.5.0.1", afterWindow);
|
||||
assert.equal(getWebdavAuthFailureStateSize(), 2);
|
||||
assert.equal(checkWebdavRateLimit(locked, afterWindow).allowed, false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user