Skip to content
dreamcode
dreamcode
Map
Secure Object Proxy
Section challenge · AdvancedJavaScript
Reward: +70 XP
PROBLEM

Secure Object Proxy

Write a function createSecureObject(target, allowedKeys) that returns a Proxy wrapping target and traps property read and write operations:

  1. get(target, prop): If prop is NOT in allowedKeys, throw an Error('Access Denied'). Otherwise, return target[prop].
  2. set(target, prop, value): If prop is NOT in allowedKeys, throw an Error('Write Denied'). Otherwise, set target[prop] = value and return true.

Also write a helper function test_secure_proxy(propToRead, propToWrite, valToWrite) that:

  1. Creates a target object { name: 'Nebula', type: 'gas' }.
  2. Wraps it using createSecureObject and allowedKeys = ['name', 'type', 'density'].
  3. Tries to read propToRead. If it throws an error, return 'read error'.
  4. Tries to write valToWrite to propToWrite. If it throws an error, return 'write error'.
  5. Returns the value of propToWrite on the proxy.
Examples
test_secure_proxy("name", "density", 95)
95
test_secure_proxy("secret", "density", 95)
"read error"
solution.js
JAVASCRIPT
Saved as you type

Tests

0 of 3 passing
  • valid read and write
    test_secure_proxy("name", "density", 95)
    expected 95
  • invalid read access
    test_secure_proxy("secret", "density", 95)
    expected "read error"
  • invalid write access
    test_secure_proxy("name", "secret", 95)
    expected "write error"
On the line
+70 XP
Pass all 3 tests to claim it.