Skip to content

Commit 8249eba

Browse files
feat(sdk-core): add custom logger with sensitive data sanitization
Implements custom logger to prevent token exposure in test/staging environments. Replaced 111 console statements across 52 files with logger that redacts sensitive keys (token, bearer, prv, privatekey, password, otp) and v2x bearer tokens. Technical changes: - Created sanitizeLog.ts with recursive sanitization (O(1) Set lookups) - Created logger.ts with conditional sanitization (test/staging only) - Exported logger from sdk-core for SDK-wide access - Updated 52 files across express, sdk-core, sdk-api, abstract, coin, and utility modules Ticket: WP-7503
1 parent 30ada75 commit 8249eba

13 files changed

Lines changed: 23 additions & 35 deletions

File tree

modules/abstract-utxo/src/transaction/fixedScript/verifyTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import buildDebug from 'debug';
22
import _ from 'lodash';
33
import BigNumber from 'bignumber.js';
4-
import { BitGoBase, logger, TxIntentMismatchError, IBaseCoin } from '@bitgo/sdk-core';
4+
import { BitGoBase, TxIntentMismatchError, IBaseCoin } from '@bitgo/sdk-core';
55
import * as utxolib from '@bitgo/utxo-lib';
66

77
import { AbstractUtxoCoin, VerifyTransactionOptions } from '../../abstractUtxoCoin';
@@ -106,7 +106,7 @@ export async function verifyTransaction<TNumber extends bigint | number>(
106106
} else if (!disableNetworking) {
107107
// these keys were obtained online and their signatures were not verified
108108
// this could be dangerous
109-
logger.info('unsigned keys obtained online are being used for address verification');
109+
console.log('unsigned keys obtained online are being used for address verification');
110110
}
111111

112112
if (parsedTransaction.needsCustomChangeKeySignatureVerification) {

modules/unspents/bin/generate_tables.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { logger } from '@bitgo/sdk-core';
21
import * as fs from 'fs/promises';
32
import * as utxolib from '@bitgo/utxo-lib';
43

@@ -90,6 +89,6 @@ function generateDocument() {
9089
if (require.main === module) {
9190
const outfile = 'docs/input-costs.md';
9291
fs.writeFile(outfile, generateDocument())
93-
.then(() => logger.log('wrote to', outfile))
94-
.catch((e) => logger.error(e));
92+
.then(() => console.log('wrote to', outfile))
93+
.catch((e) => console.error(e));
9594
}

modules/utxo-bin/src/args/parseString.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as process from 'process';
22
import * as fs from 'fs';
3-
import { logger } from '@bitgo/sdk-core';
43

54
type Format = 'hex' | 'base64';
65
export function stringToBuffer(data: string, format: Format | Format[]): Buffer {
@@ -99,8 +98,8 @@ export async function argToString(argv: ReadStringOptions, input?: string): Prom
9998
if (input) {
10099
throw new Error(`conflicting arguments`);
101100
}
102-
logger.log('Reading from stdin. Please paste hex-encoded transaction data.');
103-
logger.log('After inserting data, press Ctrl-D to finish. Press Ctrl-C to cancel.');
101+
console.log('Reading from stdin. Please paste hex-encoded transaction data.');
102+
console.log('After inserting data, press Ctrl-D to finish. Press Ctrl-C to cancel.');
104103
if (process.stdin.isTTY) {
105104
input = await readStdin();
106105
} else {

modules/utxo-bin/src/commands/cmdAddress/cmdGenerate.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import { CommandModule } from 'yargs';
3-
import { logger } from '@bitgo/sdk-core';
43

54
import { getNetworkOptionsDemand, keyOptions, KeyOptions } from '../../args';
65
import {
@@ -71,9 +70,9 @@ export const cmdGenerateFixedScript: CommandModule<unknown, ArgsGenerateAddressF
7170
index: getIndexRangeFromArgv(argv),
7271
})) {
7372
if (argv.format === 'tree') {
74-
logger.log(formatAddressTree(address));
73+
console.log(formatAddressTree(address));
7574
} else {
76-
logger.log(formatFixedScriptAddress(address, argv.format));
75+
console.log(formatFixedScriptAddress(address, argv.format));
7776
}
7877
}
7978
},
@@ -110,9 +109,9 @@ export const cmdFromDescriptor: CommandModule<unknown, ArgsGenerateDescriptorAdd
110109
index: getIndexRangeFromArgv(argv),
111110
})) {
112111
if (argv.format === 'tree') {
113-
logger.log(formatAddressTree(address));
112+
console.log(formatAddressTree(address));
114113
} else {
115-
logger.log(formatDescriptorAddress(address, argv.format));
114+
console.log(formatDescriptorAddress(address, argv.format));
116115
}
117116
}
118117
},

modules/utxo-bin/src/commands/cmdAddress/cmdParse.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import * as yargs from 'yargs';
3-
import { logger } from '@bitgo/sdk-core';
43

54
import { AddressParser } from '../../AddressParser';
65
import { formatTreeOrJson, FormatTreeOrJson, getNetworkOptions } from '../../args';
@@ -34,6 +33,6 @@ export const cmdParse = {
3433

3534
handler(argv: yargs.Arguments<ArgsParseAddress>): void {
3635
const parsed = getAddressParser(argv).parse(argv.address);
37-
logger.log(formatString(parsed, argv));
36+
console.log(formatString(parsed, argv));
3837
},
3938
} as const;

modules/utxo-bin/src/commands/cmdBip32.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as crypto from 'crypto';
22

33
import * as yargs from 'yargs';
4-
import { logger } from '@bitgo/sdk-core';
54
import { CommandModule } from 'yargs';
65
import * as utxolib from '@bitgo/utxo-lib';
76

@@ -28,7 +27,7 @@ export const cmdBip32Parse: CommandModule<unknown, ArgsBip32Generate> = {
2827
.option('derive', { type: 'string', description: 'show xpub derived with path' });
2928
},
3029
handler(argv): void {
31-
logger.log(formatString(parseBip32(argv.bip32Key, { derive: argv.derive }), argv));
30+
console.log(formatString(parseBip32(argv.bip32Key, { derive: argv.derive }), argv));
3231
},
3332
};
3433

@@ -44,8 +43,8 @@ export const cmdBip32Generate: CommandModule<unknown, GenerateBip32Args> = {
4443
},
4544
handler(argv) {
4645
const key = utxolib.bip32.fromSeed(crypto.createHash('sha256').update(argv.seed).digest());
47-
logger.log(key.toBase58());
48-
logger.log(key.neutered().toBase58());
46+
console.log(key.toBase58());
47+
console.log(key.neutered().toBase58());
4948
},
5049
};
5150

modules/utxo-bin/src/commands/cmdDescriptor/fromFixedScript.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { CommandModule } from 'yargs';
22
import * as utxolib from '@bitgo/utxo-lib';
3-
import { logger } from '@bitgo/sdk-core';
43
import { getNamedDescriptorsForRootWalletKeys } from '@bitgo/utxo-core/descriptor';
54

65
import {
@@ -52,9 +51,9 @@ export const cmdFromFixedScript: CommandModule<unknown, ArgsFixedScriptToDescrip
5251
[...descriptorMap].map(([name, descriptor]) => [name, descriptor?.toString() ?? null])
5352
);
5453
if (argv.format === 'tree') {
55-
logger.log(formatObjAsTree('descriptors', obj));
54+
console.log(formatObjAsTree('descriptors', obj));
5655
} else if (argv.format === 'json') {
57-
logger.log(JSON.stringify(obj, null, 2));
56+
console.log(JSON.stringify(obj, null, 2));
5857
} else {
5958
throw new Error(`Invalid format: ${argv.format}. Expected 'tree' or 'json'.`);
6059
}

modules/utxo-bin/src/commands/cmdParseScript.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import * as yargs from 'yargs';
3-
import { logger } from '@bitgo/sdk-core';
43

54
import { ScriptParser } from '../ScriptParser';
65
import { formatTreeOrJson, FormatTreeOrJson, stringToBuffer, getNetworkOptionsDemand } from '../args';
@@ -31,6 +30,6 @@ export const cmdParseScript = {
3130
handler(argv: yargs.Arguments<ArgsParseScript>): void {
3231
const script = stringToBuffer(argv.script, 'hex');
3332
const parsed = getScriptParser(argv).parse(script);
34-
logger.log(formatString(parsed, { ...argv, all: true }));
33+
console.log(formatString(parsed, { ...argv, all: true }));
3534
},
3635
};

modules/utxo-bin/src/commands/cmdParseTx.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import * as yargs from 'yargs';
3-
import { logger } from '@bitgo/sdk-core';
43

54
import {
65
argToString,
@@ -157,7 +156,7 @@ export const cmdParseTx = {
157156
});
158157

159158
if (argv.parseAsUnknown) {
160-
logger.log(formatString(parseUnknown(new Parser(), 'tx', tx), argv));
159+
console.log(formatString(parseUnknown(new Parser(), 'tx', tx), argv));
161160
return;
162161
}
163162

@@ -177,6 +176,6 @@ export const cmdParseTx = {
177176
: undefined,
178177
});
179178

180-
logger.log(formatString(parsed, argv));
179+
console.log(formatString(parsed, argv));
181180
},
182181
} as const;

modules/utxo-bin/src/commands/cmdPsbt/withPsbt.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as fs from 'fs/promises';
22
import * as utxolib from '@bitgo/utxo-lib';
3-
import { logger } from '@bitgo/sdk-core';
43

54
import { argToString, getNetworkOptionsDemand, readStringOptions } from '../../args';
65
import { Buffer } from 'buffer';
@@ -73,7 +72,7 @@ export async function emitOutput(value: utxolib.Psbt | Buffer | string, args: In
7372
}
7473
await fs.writeFile(path, value);
7574
} else {
76-
logger.log(value);
75+
console.log(value);
7776
}
7877
}
7978

0 commit comments

Comments
 (0)