Skip to content

Use property path parser in CompiledGetterCache - #1147

Open
joehan wants to merge 1 commit into
masterfrom
joehan/compiled-getter-parser
Open

joehan wants to merge 1 commit into
masterfrom
joehan/compiled-getter-parser

Conversation

@joehan

@joehan joehan commented Nov 24, 2026

Copy link
Copy Markdown
Member

fixing b/584413359

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +252 to +268
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;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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;
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant