Verify ALTCHA before upload body parse with single-use replay store.

Run challenge TTL and replay tracking in middleware, remove the guard, and
wire ScheduleModule for pruning used solutions.
This commit is contained in:
2026-10-12 20:18:36 +02:00
parent 96383a7426
commit 7f99256863
15 changed files with 446 additions and 84 deletions
@@ -0,0 +1,29 @@
import { HttpStatus } from '@nestjs/common';
import type { RequestHandler } from 'express';
import { ALTCHA_REQUEST_HEADER } from '../consts/altchaHeader';
import type { AltchaService } from '../modules/altcha/services/AltchaService';
import { AltchaVerificationStatus } from '../modules/altcha/types/AltchaVerificationStatus';
const altchaVerificationMessage: Record<Exclude<AltchaVerificationStatus, AltchaVerificationStatus.Ok>, string> = {
[AltchaVerificationStatus.Missing]: 'ALTCHA verification required',
[AltchaVerificationStatus.Invalid]: 'ALTCHA verification failed',
[AltchaVerificationStatus.AlreadyUsed]: 'ALTCHA already used'
};
export const altchaMiddleware = (altchaService: AltchaService): RequestHandler => {
return async (req, res, next) => {
try {
const altchaHeader = req.get(ALTCHA_REQUEST_HEADER);
const status = await altchaService.verifySolution(altchaHeader);
if (status === AltchaVerificationStatus.Ok) {
return next();
}
return res.status(HttpStatus.BAD_REQUEST).send({ message: altchaVerificationMessage[status] });
} catch (error) {
return next(error);
}
};
};