Merge pull request #410 from TNG-dev:zkldi/issue-409-Make-PrValidate-severity-configurable

Make PrValidate log severity configurable.
This commit is contained in:
zkldi
2021-11-10 04:20:13 +00:00
committed by GitHub
2 changed files with 56 additions and 25 deletions
@@ -1,4 +1,10 @@
import Prudence, { MiddlewareErrorHandler } from "prudence";
import Prudence, {
MiddlewareErrorHandler,
PrudenceSchema,
ErrorMessages,
PrudenceOptions,
} from "prudence";
import { RequestHandler } from "express-serve-static-core";
import CreateLogCtx from "lib/logger/logger";
const logger = CreateLogCtx(__filename);
@@ -6,31 +12,52 @@ const logger = CreateLogCtx(__filename);
const printf = (message: string, stringVal: string | null, keychain: string | null) =>
`[${keychain}] ${message}${stringVal ? ` (Received ${stringVal})` : ""}`;
const API_ERR_HANDLER: MiddlewareErrorHandler = (req, res, next, error) => {
let stringVal = error.userVal;
if (error.keychain && error.keychain.includes("password") && error.userVal) {
stringVal = "****";
}
const API_ERR_HANDLER =
(logLevel: TachiLogLevels): MiddlewareErrorHandler =>
(req, res, next, error) => {
let stringVal = error.userVal;
if (error.keychain && error.keychain.includes("password") && error.userVal) {
stringVal = "****";
}
if (typeof stringVal === "object" && stringVal !== null && !stringVal.toString) {
// this is probably null-prototype
stringVal = null;
} else if (stringVal === undefined) {
stringVal = "nothing";
} else {
stringVal = String(stringVal);
}
if (typeof stringVal === "object" && stringVal !== null && !stringVal.toString) {
// this is probably null-prototype
stringVal = null;
} else if (stringVal === undefined) {
stringVal = "nothing";
} else {
stringVal = String(stringVal);
}
logger.info(`Prudence rejection: ${error.message}, ${stringVal} [K:${error.keychain}]`, {
userVal: error.userVal,
});
logger[logLevel](
`Prudence rejection: ${error.message}, ${stringVal} [K:${error.keychain}]`,
{
userVal: error.userVal,
}
);
return res.status(400).json({
success: false,
description: printf(error.message, stringVal as string | null, error.keychain),
});
};
return res.status(400).json({
success: false,
description: printf(error.message, stringVal as string | null, error.keychain),
});
};
const prValidate = Prudence.CurryMiddleware(API_ERR_HANDLER);
// Cache all of the possible API_ERROR_HANDLERS to avoid function creation
// overhead at runtime.
const API_ERROR_HANDLERS = Object.fromEntries(
(["crit", "severe", "error", "warn", "info", "verbose", "debug"] as const).map((e) => [
e,
API_ERR_HANDLER(e),
])
) as Record<TachiLogLevels, MiddlewareErrorHandler>;
type TachiLogLevels = "crit" | "severe" | "error" | "warn" | "info" | "verbose" | "debug";
const prValidate = (
s: PrudenceSchema,
errorMessage?: ErrorMessages,
options?: Partial<PrudenceOptions>,
level: TachiLogLevels = "info"
): RequestHandler => Prudence.CurryMiddleware(API_ERROR_HANDLERS[level])(s, errorMessage, options);
export default prValidate;
@@ -50,7 +50,9 @@ router.post(
username:
"Invalid username. Usernames cannot start with a number, and must be between 2 and 20 characters.",
captcha: "Please fill out the captcha.",
}
},
undefined,
"verbose"
),
async (req, res) => {
if (req.session.tachi?.user.id) {
@@ -171,7 +173,9 @@ router.post(
email: "Invalid email.",
inviteCode: "Invalid invite code.",
captcha: "Please fill out the captcha.",
}
},
undefined,
"verbose"
),
async (req, res) => {
logger.verbose(`Recieved register request with username ${req.body.username} (${req.ip})`);