Skip to content

Commit ce30842

Browse files
authored
Merge pull request #30 from FSU-Pulchowk/alert-autofix-7
Potential fix for code scanning alert no. 7: Clear-text logging of sensitive information
2 parents 65fd06e + 1013b71 commit ce30842

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/utils/debug.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,45 @@ class DebugConfig {
147147
return data;
148148
}
149149

150+
/**
151+
* Force sanitize sensitive data from objects before logging, regardless of global settings.
152+
* @param {any} data Data to sanitize
153+
* @returns {any} Sanitized data
154+
*/
155+
_forceSanitizeData(data) {
156+
if (data === null || data === undefined) {
157+
return data;
158+
}
159+
// List of sensitive keys to always redact (add more as needed)
160+
const sensitivePatterns = [
161+
'password', 'secret', 'token', 'key', 'api', 'auth', 'session', 'credential', 'env', 'BIRTHDAY_ANNOUNCEMENT_CHANNEL_ID'
162+
];
163+
const redact = (obj) => {
164+
if (typeof obj !== 'object' || obj === null) return obj;
165+
if (Array.isArray(obj)) return obj.map(redact);
166+
const result = {};
167+
for (const k of Object.keys(obj)) {
168+
const lowerK = k.toLowerCase();
169+
if (sensitivePatterns.some(pattern => lowerK.includes(pattern))) {
170+
result[k] = '[REDACTED]';
171+
} else if (typeof obj[k] === 'object' && obj[k] !== null) {
172+
result[k] = redact(obj[k]);
173+
} else {
174+
result[k] = obj[k];
175+
}
176+
}
177+
return result;
178+
};
179+
// If the data is process.env or contains process.env, redact all values
180+
if (
181+
(typeof process !== 'undefined' && data === process.env) ||
182+
(data && typeof data === 'object' && Object.keys(process.env || {}).some(envKey => Object.prototype.hasOwnProperty.call(data, envKey)))
183+
) {
184+
return '[REDACTED: process.env]';
185+
}
186+
return redact(data);
187+
}
188+
150189
/**
151190
* Limit data output length to prevent log flooding
152191
* @param {any} data Data to limit
@@ -344,8 +383,9 @@ node your_script.js -d --debug-no-sanitize # Disable sanitization (NOT
344383
if (this.debugStream) this.debugStream.write(sanitizedMessage + '\n');
345384
}
346385
if (data !== null) {
347-
// Data should already be sanitized and limited before being passed here
348-
const formattedData = JSON.stringify(data, null, 2);
386+
// Always force sanitization of data before logging, regardless of global settings
387+
const forceSanitizedData = this._forceSanitizeData(data);
388+
const formattedData = JSON.stringify(forceSanitizedData, null, 2);
349389
console.log(formattedData);
350390
if (this.debugStream) this.debugStream.write(formattedData + '\n');
351391
}

0 commit comments

Comments
 (0)