Skip to content

Chore: Redact full objects #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/diff/diffMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getObjectChangeResult,
getPathHint,
maxKeysSecurityCheck,
shouldRedactValue,
timeoutSecurityCheck,
} from './shared';

Expand Down Expand Up @@ -34,6 +35,7 @@ function diffMaps({
timeoutSecurityCheck(startedAt, config);
}

const redactable = shouldRedactValue(key, config);
const keyInLhs = lhs.has(key);
const keyInRhs = rhs.has(key);
const lhsValue = keyInLhs ? lhs.get(key) : null;
Expand All @@ -42,7 +44,7 @@ function diffMaps({
const pathUpdate = hint ? [hint, key] : [key];
const updatedPath = [...path, ...pathUpdate];

if (isIterable(lhsValue) || isIterable(rhsValue)) {
if (!redactable && (isIterable(lhsValue) || isIterable(rhsValue))) {
result.push(
...recursiveDiff({
lhs: lhsValue,
Expand Down
4 changes: 3 additions & 1 deletion src/diff/diffObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
buildResult,
getObjectChangeResult,
maxKeysSecurityCheck,
shouldRedactValue,
timeoutSecurityCheck,
} from './shared';

Expand Down Expand Up @@ -36,13 +37,14 @@ function diffObjects({
timeoutSecurityCheck(startedAt, config);
}

const redactable = shouldRedactValue(key, config);
const lhsValue = Array.isArray(lhs) ? lhs[key as number] : lhs?.[key];
const rhsValue = Array.isArray(rhs) ? rhs[key as number] : rhs?.[key];
const numericKey = typeof key !== 'symbol' ? Number(key) : NaN;
const parsedKey = isNaN(numericKey) ? key : numericKey;
const updatedPath = [...path, parsedKey];

if (isIterable(lhsValue) || isIterable(rhsValue)) {
if (!redactable && (isIterable(lhsValue) || isIterable(rhsValue))) {
result.push(
...recursiveDiff({
lhs: lhsValue,
Expand Down
9 changes: 6 additions & 3 deletions src/diff/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ export function includeDiffType(type: ChangeType, config: DiffConfig) {
return config.include?.includes?.(type) && !config.exclude?.includes?.(type);
}

export function shouldRedactValue(key: any, config: DiffConfig) {
const rawKey = getRawValue(key);
export function shouldRedactValue(
key: string | number | symbol,
config: DiffConfig
) {
if (!isPrimitive(key)) return false;

return config.redactKeys?.includes?.(rawKey);
return config.redactKeys?.includes?.(key.toString());
}

export function createReplacer(config: DiffConfig, obj: any) {
Expand Down
8 changes: 6 additions & 2 deletions src/test/security.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ describe('Security checks', () => {
const symToken = Symbol('token');
const symSecret = Symbol('secret');

const redactedObj = {
foo: 'redacted object',
};

const a = {
password: 'abcde',
token: 'abcde',
Expand All @@ -72,7 +76,7 @@ describe('Security checks', () => {
[symToken]: 'abcde',
[symSecret]: 'abcde',
safe: 'safe field',
sensitive: 'secret',
sensitive: redactedObj, // replaces the entire object
};

const b = new Map([[a, 'foo']]);
Expand Down Expand Up @@ -118,7 +122,7 @@ describe('Security checks', () => {
type: ChangeType.ADD,
str: '"sensitive": "*****",',
depth: 1,
path: ['sensitive', { deleted: false, value: 'secret' }],
path: ['sensitive', { deleted: false, value: redactedObj }],
},
{
type: ChangeType.ADD,
Expand Down