chore: add prototype pollution guards to setState and SnapController #3719
+26
−9
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.
request strengthens security across multiple components of the MetaMask Snaps codebase by addressing potential prototype pollution vulnerabilities in recursive state assignment and object accumulation logic.
these fixes prevent malicious user input from mutating JavaScript object prototypes or altering inherited behavior, thereby improving the overall safety and robustness of Snaps runtime operations.
Issue: #3718
Mitigate Prototype Pollution in
setState(packages/snaps-rpc-methods/src/permitted/setState.ts)The recursive assignment function used in
setStatecould inadvertently allow prototype pollution if untrusted keys such as__proto__,constructor, orprototypewere used as property names during state updates.This issue could enable malicious payloads to modify global object prototypes, leading to unpredictable behavior or security compromises.
Secure Object Construction in
SnapControllerConversion Logic (packages/snaps-controllers/src/snaps/SnapController.ts#L3946)Within the logic that constructs
filteredConversionRatesfromrequestedConversions, plain JavaScript objects were being used to store user-derived keys (conversion.fromandconversion.to).Since object property keys can interact with the prototype chain, this pattern risked prototype pollution if untrusted input was introduced.
Prevent Pollution in Market Data Transformation (
#transformOnAssetsMarketDataResult)In the private method
#transformOnAssetsMarketDataResult, user-derivedassetandunitstrings were used as keys during reduction to constructfilteredMarketData.As with the previous issue, this pattern risked prototype pollution via implicit property inheritance.
Note
Adds explicit prototype pollution guards in setState and switches SnapController asset transform reducers to safe accumulators, preserving output shape.
packages/snaps-rpc-methods/src/permitted/setState.ts:set(...): Rejects keys__proto__,constructor,prototype(in addition to existing forbidden keys); updates error message.packages/snaps-controllers/src/snaps/SnapController.ts:#transformOnAssetsConversionResult(...): UsesMap→Mapaccumulator during reduction; converts nestedMaps back to plain objects for return.#transformOnAssetsMarketDataResult(...): UsesObject.create(null)for nested objects to avoid prototype inheritance.Written by Cursor Bugbot for commit 700c6b6. This will update automatically on new commits. Configure here.