-
-
Notifications
You must be signed in to change notification settings - Fork 724
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
fix: secret function #3707
fix: secret function #3707
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kobenguyent I think you can solve all of this by just changing the return value in the get
handler in secret.js to return fieldsToHide.includes(prop) ? new Secret(obj[prop]) : obj[prop];
:
function secretObject(obj, fieldsToHide = []) {
const handler = {
get(obj, prop) {
if (prop === 'toString') {
return function () {
const maskedObject = deepClone(obj);
fieldsToHide.forEach(f => maskedObject[f] = maskStr);
return JSON.stringify(maskedObject);
};
}
// Just this change ...
return fieldsToHide.includes(prop) ? new Secret(obj[prop]) : obj[prop];
},
};
return new Proxy(obj, handler);
}
This makes the extracted item a secret - in my local tests this seems to work well (my tests can run login scripts etc.. with this approach):
simple helper fn to demo:
useMyFunc(secret_string, secret_object, secret_item_from_object) {
console.log(">>>> types", {
secret_string: typeof secret_string,
secret_object: typeof secret_object,
secret_item_from_object: typeof secret_item_from_object,
});
console.log(">>>> values", {
secret_string,
secret_item_from_object: secret_item_from_object,
});
}
Send the secrets to see what's logged...
const secret_string = secret("password");
const secret_object = secret({ a: "pass123", b: "bob" }, "a");
I.useMyFunc(secret_string, secret_object, secret_object.a);
OUTPUT
I use my func *****, {"a":"****","b":"bob"}, *****
>>>> types {
secret_string: 'object',
secret_object: 'object',
secret_item_from_object: 'object'
}
>>>> values {
secret_string: Secret { _secret: 'password' },
secret_item_from_object: Secret { _secret: 'pass123' }
}
Thanks @roycollings such an elegant fix than mine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of non-blockers :)
Motivation/Description of the PR
fix: secret function
Type of change
Checklist:
npm run docs
)npm run lint
)npm test
)