-
Notifications
You must be signed in to change notification settings - Fork 31
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
Original object is modified and not restored in any way #50
Comments
Lines 180 to 188 in 41339e6
|
ouch, it seem we have a bug in the restore function. |
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. |
Will try to find a fix. Need to dig in the |
#51 this could be a fix. Needs some more investigation. |
function specialSet (o, k, path, afterPath, censor, isCensorFct, censorFctTakesPath) {
const afterPathLen = afterPath.length
const lastPathIndex = afterPathLen - 1
const originalKey = k
var i = -1
var n
var nv
var ov
var oov = null
var exists = true
var wc = null
ov = n = o[k]
if (typeof n !== 'object') return { value: null, parent: null, exists }
while (n != null && ++i < afterPathLen) {
k = afterPath[i]
oov = ov
if (k !== '*' && !wc && !(typeof n === 'object' && k in n)) {
exists = false
break
}
if (k === '*') {
wc = k
if (i !== lastPathIndex) {
continue
}
}
if (wc) {
const wcKeys = Object.keys(n)
for (var j = 0; j < wcKeys.length; j++) {
const wck = wcKeys[j]
const wcov = n[wck]
const kIsWc = k === '*'
if (kIsWc || (typeof wcov === 'object' && wcov !== null && k in wcov)) {
if (kIsWc) {
ov = wcov
} else {
ov = wcov[k]
}
nv = (i !== lastPathIndex)
? ov
: (isCensorFct
? (censorFctTakesPath ? censor(ov, [...path, originalKey, ...afterPath]) : censor(ov))
: censor)
if (kIsWc) {
n[wck] = nv
} else {
if (wcov[k] === nv) {
exists = false
} else {
wcov[k] = (nv === undefined && censor !== undefined) || (has(wcov, k) && nv === ov) ? wcov[k] : nv
}
}
}
}
wc = null
} else {
ov = n[k]
nv = (i !== lastPathIndex)
? ov
: (isCensorFct
? (censorFctTakesPath ? censor(ov, [...path, originalKey, ...afterPath]) : censor(ov))
: censor)
n[k] = (has(n, k) && nv === ov) || (nv === undefined && censor !== undefined) ? n[k] : nv
n = n[k]
}
if (typeof n !== 'object') break
if (ov === oov) {
exists = false
}
}
return { value: ov, parent: oov, exists }
} This can fix pino issue. but it's a Band Aid if (ov === oov) {
exists = false
} |
Hello @mrjwt , @mcollina Additionally I tried the restore function in an attempt to fix it and that appears to undo the redactions on object generated by Thanks |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
We should at least put warning "do not use multiple levels of wildcards!" to readme because this issue is actually breaking services, mutating data potentially causing damage This mutating issue happens on:
|
Pull requests to resolve issues are welcome. |
/*
/test/index.js
*/
test('redact.restore function places original values back in place (deeply nested wildcards)', ({ end, is }) => {
const censor = 'test'
const value = 'hidden'
const paths = ['*.c2.*.d']
const redact = fastRedact({ paths, censor, serialize: false })
const o = {
x: { c1: [ { d: value, e: 'leave me be' } ] },
y: { c2: [ { d: value, f: 'I want to live' } ] },
z: { c3: [ { d: value, g: 'I want to run in a stream' } ] }
}
redact(o)
is(o.x.c1[0].d, value)
is(o.y.c2[0].d, censor)
is(o.z.c3[0].d, value)
redact.restore(o)
is(o.x.c1[0].d, value)
is(o.y.c2[0].d, value)
is(o.z.c3[0].d, value)
end()
}) It seems to be happening when combined with arrays ...testcase that started this issue works now, this one does not |
i've opened PR that should fix this issue, if someone can take a look #59 |
I want to be able the redact a key in multiple levels. So i wrote this code:
The output is:
You can see that the original object is modified.
Seems like something is wrong with the
nestedRestore
and thenestedRedact
functions.When i added multiple levels that do not exist in the object. Seems like that
nestedRedact
with thespecialSet
functions doesn't build the store properly.The text was updated successfully, but these errors were encountered: