Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request replaces the unsafe new Function implementation in CompiledGetterCache with a secure, custom path parser that safely resolves nested object paths, including dot notation, bracket notation, and array indices. It also adds a size-limited cache to prevent memory leaks and includes comprehensive unit tests. The feedback suggests an improvement to the getter function to allow accessing inherited properties and getters on class prototypes while still safely blocking function execution, which simplifies the logic and avoids overly restrictive hasOwnProperty checks.
| return (obj: any) => { | ||
| let current = obj; | ||
| for (const key of segments) { | ||
| if ( | ||
| current === null || | ||
| current === undefined || | ||
| (typeof current !== 'object' && typeof current !== 'string') | ||
| ) { | ||
| return undefined; | ||
| } | ||
| if (!Object.prototype.hasOwnProperty.call(current, key)) { | ||
| return undefined; | ||
| } | ||
| current = current[key]; | ||
| } | ||
| return current; | ||
| }; |
There was a problem hiding this comment.
Using Object.prototype.hasOwnProperty.call(current, key) prevents the getter from accessing inherited properties or getters defined on class prototypes (e.g., class instances with getters or helper methods). Since BLOCKED_KEYS already prevents prototype pollution by blocking __proto__, constructor, and prototype, we can safely allow prototype/inherited property access. To preserve the behavior of blocking prototype methods (like toString or valueOf), we can simply check if the resolved value is a function and return undefined if so. This also simplifies the code and improves performance by avoiding complex type checks and hasOwnProperty calls.
return (obj: any) => {
let current = obj;
for (const key of segments) {
if (current === null || current === undefined) {
return undefined;
}
const next = current[key];
if (typeof next === 'function') {
return undefined;
}
current = next;
}
return current;
};
fixing b/584413359