I’m trying to use a JavaScript Proxy to hide a property from detection while keeping it in the target object. The has trap successfully makes 'secret' in proxy return false, but the property is still accessible directly.
Code Example:
const target = { secret: 123, public: 456 };
const proxy = new Proxy(target, {
has(t, prop) {
if (prop === 'secret') return false; // Hide from `in` checks
return prop in t;
}
});
console.log('secret' in proxy);
console.log(proxy.secret);
Questions:
in operator not block direct access to the property?get, ownKeys) that must be implemented together for consistency?Observed Behavior:
'secret' in proxy correctly returns false.proxy.secret still returns the value 123.Expected Behavior:
If the in operator returns false for 'secret', accessing proxy.secret should throw an error or return undefined.
Attempts to Fix:
has trap.get and ownKeys but unsure how to coordinate them.Yes, you need to coordinate the all related traps, you could make a generic function to hide object keys.
Note that in looks in the prototype chain so for handling class instances with inheritance there could be more complex logic required.
const hideKeys = (target, keys) => {
keys = new Set(keys);
return new Proxy(target, {
get(t, prop){
if(keys.has(prop)) return;
return Reflect.get(...arguments)
},
has(t, prop) {
if (keys.has(prop)) return false; // Hide from `in` checks
return Reflect.has(...arguments);
},
ownKeys(){
return Reflect.ownKeys(...arguments).filter(key => !keys.has(key));
},
getOwnPropertyDescriptor(t, prop){
if(keys.has(prop)) return;
return Reflect.getOwnPropertyDescriptor(...arguments);
}
});
}
const target = { secret: 123, public: 456 };
const proxy = hideKeys(target, ['secret']);
console.log('secret' in proxy);
console.log(Object.hasOwn(proxy, 'secret'));
for(const k in proxy){
console.log(k);
}
console.log(proxy.propertyIsEnumerable('secret'));
console.log(proxy.secret);
console.log(JSON.stringify(proxy));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With