Improve transport safety and log redaction
This commit is contained in:
41
plugin/core/logging.ts
Normal file
41
plugin/core/logging.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
const DEFAULT_VISIBLE_EDGE = 4;
|
||||
|
||||
export type RedactableValue = string | null | undefined;
|
||||
|
||||
export function redactSecret(value: RedactableValue, visibleEdge: number = DEFAULT_VISIBLE_EDGE): string {
|
||||
return redactValue(value, { visibleEdge, label: "secret" });
|
||||
}
|
||||
|
||||
export function redactPairingCode(value: RedactableValue, visibleEdge: number = DEFAULT_VISIBLE_EDGE): string {
|
||||
return redactValue(value, { visibleEdge, label: "pairingCode" });
|
||||
}
|
||||
|
||||
export function redactKey(value: RedactableValue, visibleEdge: number = DEFAULT_VISIBLE_EDGE): string {
|
||||
return redactValue(value, { visibleEdge, label: "key" });
|
||||
}
|
||||
|
||||
export function redactValue(
|
||||
value: RedactableValue,
|
||||
options: { visibleEdge?: number; label?: string } = {}
|
||||
): string {
|
||||
const visibleEdge = options.visibleEdge ?? DEFAULT_VISIBLE_EDGE;
|
||||
const label = options.label ?? "value";
|
||||
|
||||
if (!value) {
|
||||
return `<redacted:${label}:empty>`;
|
||||
}
|
||||
|
||||
if (value.length <= visibleEdge * 2) {
|
||||
return `<redacted:${label}:${value.length}>`;
|
||||
}
|
||||
|
||||
return `${value.slice(0, visibleEdge)}…${value.slice(-visibleEdge)} <redacted:${label}:${value.length}>`;
|
||||
}
|
||||
|
||||
export function safeErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
return String(error);
|
||||
}
|
||||
Reference in New Issue
Block a user