-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters