Skip to content
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

improve masking fn #249

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"commander": "^12.0.0",
"env-paths": "^3.0.0",
"inquirer": "8.0.0",
"lodash.clonedeep": "^4.5.0",
"mkdirp": "^3.0.1",
"winston": "^3.13.0",
"x25519": "^0.1.0"
Expand Down
51 changes: 26 additions & 25 deletions src/common/masking.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
import cloneDeep from 'lodash.clonedeep'

const DEFAULT_MASKED_FIELDS = [
const PREFIX = 'data:application/UI8A;base64,'
const DEFAULT_MASKED_FIELDS = new Set([
'seed',
'secretKey',
'addressRaw',
];
]);

export function maskPayload (payload: any): any {
if (typeof payload === 'string') return payload

const clonedPayload = cloneDeep(payload);
const maskedPayload = {}
if (
typeof payload === 'string' ||
typeof payload === 'boolean' ||
payload === null
) return payload

if (!clonedPayload) {
return clonedPayload;
}
const maskedPayload = Array.isArray(payload)
? []
: {}

// maskJSONFields doesn't handle nested objects very well so we'll
// need to recursively walk to object and mask them one by one
for (const [property, value] of Object.entries(clonedPayload)) {
if (value && typeof value === 'object') {
if (Object.keys(clonedPayload[property]).filter(key => isNaN(parseInt(key))).length === 0) {
const reconstructedUintArr: number[] = Object.values(clonedPayload[property])
maskedPayload[property] = "base64:" + Buffer.from(reconstructedUintArr).toString("base64");
} else {
maskedPayload[property] = maskPayload(value);
}
} else if (value && typeof value === 'string' && DEFAULT_MASKED_FIELDS.includes(property)) {
maskedPayload[property] = "*".repeat(clonedPayload[property].length)
} else {
maskedPayload[property] = value
return Object.entries(payload).reduce((acc, [property, value]) => {
if (DEFAULT_MASKED_FIELDS.has(property)) {
// @ts-expect-error .length does not exist on type "unknown"
acc[property] = "*".repeat(value?.length || 32)
}
else if (value instanceof Uint8Array) {
acc[property] = PREFIX + Buffer.from(value).toString('base64')
}
else if (typeof value === 'object') {
acc[property] = maskPayload(value);
}
else {
acc[property] = value
}
}

return maskedPayload;
return acc
}, maskedPayload)
}
53 changes: 53 additions & 0 deletions tests/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import test from 'tape'

import { maskPayload } from '../src/common/masking'

test('common/masking', async (t) => {

t.deepEqual(
maskPayload('dog'),
'dog',
'handles string'
)

t.deepEqual(
maskPayload(null),
null,
'handles null'
)

t.deepEqual(
maskPayload(true),
true,
'handles bool'
)

const buildPayload = () => {
return {
nested: {
seed: 'secrets',
secretKey: new Uint8Array([1,2,3]),
publicKey: new Uint8Array([4,5,6]),
arr: ['a', 'b', 'c'],
obj: { "0": 17, "1": 23 }
}
}
}
const payload = buildPayload()
const expected = {
nested: {
seed: '*'.repeat('secrets'.length),
secretKey: '***',
publicKey: 'data:application/UI8A;base64,BAUG',
arr: ['a', 'b', 'c'],
obj: { "0": 17, "1": 23 }
}
}
t.deepEqual(maskPayload(payload), expected, 'nested mess')
t.deepEqual(payload, buildPayload(), 'maskPayload does not mutate')


t.deepEqual(maskPayload([payload, payload]), [expected, expected], 'arrays')

t.end()
})
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2658,11 +2658,6 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
integrity sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
Expand Down
Loading