Skip to content

Commit

Permalink
feat: strip sensitive info form logs
Browse files Browse the repository at this point in the history
fix(commons): fix serializing uint8array
  • Loading branch information
blakebyrnes committed Apr 5, 2024
1 parent f3a7191 commit 8fec49d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 43 deletions.
43 changes: 25 additions & 18 deletions commons/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
import { inspect } from 'util';
import ILog, { ILogData } from '../interfaces/ILog';

declare global {
function UlixeeLogCreator(module: NodeModule): {
log: ILog;
};

// eslint-disable-next-line no-var,vars-on-top
var UlxLogPrototype: Log;
// eslint-disable-next-line no-var,vars-on-top
var UlxLogFilters: any;
// eslint-disable-next-line no-var,vars-on-top
var UlxLoggerSessionIdNames: Map<string, string>;
// eslint-disable-next-line no-var,vars-on-top
var UlxSubscriptions: Map<number, (log: ILogEntry) => any>;
}

const hasBeenLoggedSymbol = Symbol.for('UlxHasBeenLogged');

global.UlxLogFilters ??= {
Expand Down Expand Up @@ -122,8 +137,13 @@ class Log implements ILog {
}
}

function translateValueToPrintable(value: any, depth = 0): any {
function translateValueToPrintable(key: string, value: any, depth = 0): any {
if (value === undefined || value === null) return;

if (key === 'password' || key === 'suri') {
return '********';
}

if (value instanceof Error) {
return value.toString();
}
Expand All @@ -150,11 +170,11 @@ function translateValueToPrintable(value: any, depth = 0): any {

if (typeof value === 'object') {
if (Array.isArray(value)) {
return value.map(x => translateValueToPrintable(x, depth + 1));
return value.map((x, i) => translateValueToPrintable(i as any, x, depth + 1));
}
const result: any = {};
for (const [key, subValue] of Object.entries(value)) {
result[key] = translateValueToPrintable(subValue, depth + 1);
for (const [subKey, subValue] of Object.entries(value)) {
result[subKey] = translateValueToPrintable(subKey, subValue, depth + 1);
}
return result;
}
Expand Down Expand Up @@ -188,7 +208,7 @@ export function translateToPrintable(
continue;
}

const printable = translateValueToPrintable(value);
const printable = translateValueToPrintable(key, value);
if (printable === null || printable === undefined) continue;
printData[key] = printable;
}
Expand All @@ -197,17 +217,6 @@ export function translateToPrintable(

const logLevels = { stats: 0, info: 1, warn: 2, error: 3 };

declare global {
function UlixeeLogCreator(module: NodeModule): {
log: ILog;
};
// eslint-disable-next-line no-var,vars-on-top
var UlixeeLogClass: typeof Log;
// eslint-disable-next-line no-var,vars-on-top
var UlxLoggerSessionIdNames: Map<string, string>;
// eslint-disable-next-line no-var,vars-on-top
var UlxSubscriptions: Map<number, (log: ILogEntry) => any>;
}

if (!global.UlixeeLogCreator) {
global.UlixeeLogCreator = (module: NodeModule): { log: ILog } => {
Expand All @@ -224,8 +233,6 @@ if (!global.UlixeeLogCreator) {
};
}

global.UlixeeLogClass ??= Log;

export default function logger(module: NodeModule): ILogBuilder {
return global.UlixeeLogCreator(module);
}
Expand Down
14 changes: 8 additions & 6 deletions commons/lib/TypeSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ export default class TypeSerializer {
}

if (ArrayBuffer.isView(value)) {
// @ts-ignore
const binary = new TextDecoder('utf8').decode(value.buffer);
const binary = Array.from(new Uint8Array(value.buffer, value.byteOffset, value.byteLength))
.map(byte => String.fromCharCode(byte))
.join('');
return {
__type: Types.ArrayBuffer64,
value: globalThis.btoa(binary),
Expand All @@ -186,8 +187,7 @@ export default class TypeSerializer {
};
}
if (value instanceof ArrayBuffer) {
// @ts-ignore
const binary = new TextDecoder('utf8').decode(value);
const binary = Array.from(new Uint8Array(value)).map(byte => String.fromCharCode(byte)).join('');
return {
__type: Types.ArrayBuffer64,
value: globalThis.btoa(binary),
Expand Down Expand Up @@ -219,8 +219,10 @@ export default class TypeSerializer {
}

const decoded = globalThis.atob(value);
// @ts-ignore
const uint8Array = new TextEncoder().encode(decoded);
const uint8Array = new Uint8Array(new ArrayBuffer(decoded.length));
for (let i = 0; i < decoded.length; i++) {
uint8Array[i] = decoded.charCodeAt(i);
}
if (!entry.args) return uint8Array;

const { arrayType, byteOffset, byteLength } = entry.args;
Expand Down
19 changes: 3 additions & 16 deletions commons/lib/dirUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@ import * as Path from 'path';
import * as Fs from 'fs';
import { existsAsync } from './fileUtils';

export function getCacheDirectory(): string {
if (process.platform === 'linux') {
return process.env.XDG_CACHE_HOME || Path.join(os.homedir(), '.cache');
}

if (process.platform === 'darwin') {
return Path.join(os.homedir(), 'Library', 'Caches');
}

if (process.platform === 'win32') {
return process.env.LOCALAPPDATA || Path.join(os.homedir(), 'AppData', 'Local');
}

throw new Error(`Unsupported platform: ${process.platform}`);
}

export function getDataDirectory(): string {
if (process.env.ULX_DATA_DIR) {
return process.env.ULX_DATA_DIR;
}
if (process.platform === 'linux') {
return process.env.XDG_DATA_HOME || Path.join(os.homedir(), '.local', 'share');
}
Expand Down
5 changes: 2 additions & 3 deletions commons/lib/envUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Fs from 'fs';
import * as Path from 'path';
import * as Os from 'os';
import { getCacheDirectory, getDataDirectory } from './dirUtils';
import * as Path from 'path';
import { getDataDirectory } from './dirUtils';

/**
* Will load env files with this precedence (.env.defaults, .env.<NODE_ENV>, .env)
Expand Down Expand Up @@ -70,7 +70,6 @@ export function parseEnvInt(envvar: string): number | null {
export function parseEnvPath(envvar: string, relativeTo?: string): string {
if (!envvar) return undefined;
if (envvar?.startsWith('~')) envvar = Path.join(Os.homedir(), envvar.slice(1));
if (envvar?.startsWith('<CACHE>')) envvar = envvar.replace('<CACHE>', getCacheDirectory());
if (envvar?.startsWith('<DATA>')) envvar = envvar.replace('<DATA>', getDataDirectory());
if (envvar?.startsWith('<TMP>')) envvar = envvar.replace('<TMP>', Os.tmpdir());
if (Path.isAbsolute(envvar)) return envvar;
Expand Down

0 comments on commit 8fec49d

Please sign in to comment.