Skip to content

Commit

Permalink
redact auth headers in logs
Browse files Browse the repository at this point in the history
!restart
  • Loading branch information
siddharthvp committed Jun 1, 2024
1 parent 5ac43bb commit ec506ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions botbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ export function emailOnError(err: Error, taskname: string, isFatal = true) {
// exit normally
}
export function logFullError(err: any, isFatal = true) {
if (err.request?.headers?.Authorization) {
err.request.headers.Authorization = '***';
}
redactSecretsInError(err);
// datetime similar to what mwn log produces, but can't use that directly as mwn may not have loaded
const dateTimeString = new Date().toISOString().slice(0, 19).replace('T', ' ');
console.log(`[${dateTimeString}] ${isFatal ? '[E] Fatal error' : '[E] Error'}`);
console.log(err);
}
export function redactSecretsInError(err: any) {
if (err.request?.headers?.Authorization) {
err.request.headers.Authorization = '***';
}
if (err.config?.headers?.Authorization) {
err.config.headers.Authorization = '***';
}
}

// Errors occurring inside async functions are caught by emailOnError(),
// this is only for anything else, such as failing imports
Expand Down
5 changes: 4 additions & 1 deletion utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {bot, fs, log} from "./botbase";
import {bot, fs, log, redactSecretsInError} from "./botbase";
import {spawn} from "child_process";
import {ENWIKI_DB_HOST, TOOLS_DB_HOST} from "./db";
import {REDIS_HOST} from "./redis";
Expand All @@ -23,6 +23,9 @@ export function createLogStream(file: string) {
});

var logger = function (msg) {
if (typeof msg === 'object') {
redactSecretsInError(msg);
}
let ts = new bot.date().format('YYYY-MM-DD HH:mm:ss');
let stringified;
if (typeof msg === 'string') {
Expand Down

0 comments on commit ec506ee

Please sign in to comment.