Harden CMS security and theme contrast
This commit is contained in:
1 parent
2465ff2170
commit
4a1e1115b3
57 files changed
+1023
-231
No files matched your search
@@ -0,0 +1,39 @@
|
||||
type LogScalar = string | number | boolean | null;
|
||||
type LogContext = Record<string, unknown>;
|
||||
|
||||
const SENSITIVE_KEY = /(authorization|cookie|password|secret|token|api[-_]?key)/i;
|
||||
|
||||
export interface ServerErrorRecord {
|
||||
level: "error";
|
||||
event: string;
|
||||
message: string;
|
||||
context: Record<string, LogScalar>;
|
||||
}
|
||||
|
||||
export function serverErrorRecord(
|
||||
event: string,
|
||||
error: unknown,
|
||||
context: LogContext = {},
|
||||
): ServerErrorRecord {
|
||||
return {
|
||||
level: "error",
|
||||
event,
|
||||
message: error instanceof Error ? error.message.slice(0, 500) : "Unknown server error",
|
||||
context: Object.fromEntries(Object.entries(context).map(([key, value]) => [
|
||||
key,
|
||||
SENSITIVE_KEY.test(key) ? "[REDACTED]" : logScalar(value),
|
||||
])),
|
||||
};
|
||||
}
|
||||
|
||||
export function logServerError(event: string, error: unknown, context: LogContext = {}): void {
|
||||
console.error(JSON.stringify(serverErrorRecord(event, error, context)));
|
||||
}
|
||||
|
||||
function logScalar(value: unknown): LogScalar {
|
||||
return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
||||
? value
|
||||
: value === null
|
||||
? null
|
||||
: "[NON_SCALAR]";
|
||||
}
|
||||
Reference in new issue
Block a user