fix: widen isSafeDynamicKey parameter type to PropertyKey - #9774
Open
ZayanKhan-12 wants to merge 2 commits into
Open
fix: widen isSafeDynamicKey parameter type to PropertyKey#9774ZayanKhan-12 wants to merge 2 commits into
isSafeDynamicKey parameter type to PropertyKey#9774ZayanKhan-12 wants to merge 2 commits into
Conversation
`isSafeDynamicKey` only accepted `string` keys and returned `false` for any non-string input, so valid dynamic keys of type `number` (e.g. array indices such as `currentIndex` in SmartTransactionsController) or `symbol` could not be validated without a lossy `String()` conversion. Number and symbol keys cannot be used for prototype pollution via the blocklisted string keys: numbers coerce to numeric strings that never collide with the blocklist, and symbol keys cannot alias string-keyed `Object.prototype` properties. The function now accepts any `PropertyKey`, keeps the blocklist check for strings, and still returns `false` for non-`PropertyKey` runtime values passed from untyped code. Fixes MetaMask/utils#200 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
isSafeDynamicKeyin@metamask/controller-utilsonly acceptsstringkeys and returnsfalsefor any non-string input. This means valid dynamic keys of typenumber(e.g. array indices such ascurrentIndexinSmartTransactionsController, the case that motivated the ticket) orsymbolcannot be validated without a lossyString()conversion.Neither number nor symbol keys can be used for a prototype pollution attack via the blocklisted keys:
'__proto__','constructor', or'prototype';Object.prototypeproperties.This PR widens the parameter type to
PropertyKey(string | number | symbol). Behavior:stringkeys: unchanged — checked againstPROTOTYPE_POLLUTION_BLOCKLIST.number/symbolkeys: now returntrue(previouslyfalse).PropertyKeyruntime values passed from untyped code (e.g.null,undefined): still returnfalse, preserving the existing defensive behavior.The change is backwards compatible for all existing (string-typed) callers.
Note: the tracking issue lives in the
MetaMask/utilsrepo, but the function itself lives here in@metamask/controller-utils(added in #4041), so this PR targetscore.References
Fixes MetaMask/utils#200
isSafeDynamicKeyshould handle allPropertyKeysubtypes so number indices can be validated without string conversion)Testing
yarn workspace @metamask/controller-utils run jest --no-coverage— 6 suites, 186 tests passedyarn workspace @metamask/controller-utils run test(with coverage) — passedyarn workspace @metamask/controller-utils run build— passedyarn eslinton the changed files — cleanPropertyKeysubtypes (blocklisted and safe strings, number keys incl.NaN, symbol keys incl.Symbol('__proto__')) plusnull/undefinedreturningfalseChecklist
🤖 Generated with Claude Code
Note
Low Risk
Small, intentional behavior change in a prototype-pollution helper; string semantics are unchanged and the new number/symbol paths are documented as non-polluting.
Overview
isSafeDynamicKeyin@metamask/controller-utilsnow acceptsPropertyKey(string | number | symbol) instead of onlystring, so callers can validate numeric indices and symbol keys without coercing to strings.String keys still use
PROTOTYPE_POLLUTION_BLOCKLISTas before. Number and symbol keys now returntrue(they werefalsefor any non-string).null/undefinedstill returnfalse. JSDoc explains why number/symbol keys are treated as safe; tests and the package changelog are updated.Reviewed by Cursor Bugbot for commit 2034caa. Bugbot is set up for automated code reviews on this repo. Configure here.