Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[base-controller] Make allowed{Actions,Events} required parameters of getRestricted #4035

Conversation

MajorLift
Copy link
Contributor

@MajorLift MajorLift commented Mar 8, 2024

Explanation

This commit implements a fundamental solution to aligning runtime and type-level behavior for getRestricted: removing the option to omit its function parameters altogether.

Impact

  • This makes the expected runtime behavior of the method explicit and predictable.
  • This completely prevents type inference from deriving ambiguous conclusions from the omission of function or generic parameters, or otherwise exhibiting unexpected behavior.

The downsides of this approach are a bit more redundant code and inconvenience, but having to write an extra empty array or two doesn't seem like a terrible cost for getting unambiguous, explicit behavior.

Caveat

Note that this solution does not directly resolve the observed bug that motivated this ticket. It does make the bug irrelevant for getRestricted, because passing in both allowlist params fixes the issue, but in general, there may still be cases where generic default arguments fail to apply.

See #4033 for repro steps and more details on this bug.

Overview

  • This change is implemented entirely in this commit: 9988d18
  • All of the other diffs in the test files are simply applying this breaking change.

References

Changelog

@metamask/base-controller

Changed

  • BREAKING: The getRestricted method of the ControllerMessenger class now expects both allowedActions and allowedEvents as required parameters.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@MajorLift MajorLift self-assigned this Mar 8, 2024
@MajorLift MajorLift changed the title [base-controller] Fix getRestricted not inferring omitted allowed{Actions,Events} as never[] when used with unrestrictedControllerMessenger pattern [base-controller] Fix getRestricted not inferring omitted allowed{Actions,Events} as never[] when used with unrestrictedControllerMessenger pattern Mar 8, 2024
@MajorLift MajorLift force-pushed the 240307-fix-getRestricted-not-infers-omitted-allowlists-as-empty-array branch from e855785 to 522ece0 Compare March 8, 2024 05:57
@MajorLift MajorLift changed the title [base-controller] Fix getRestricted not inferring omitted allowed{Actions,Events} as never[] when used with unrestrictedControllerMessenger pattern [base-controller] Fix getRestricted not inferring omitted Allowed{Actions,Events} as never when used with unrestrictedControllerMessenger pattern Mar 8, 2024
@MajorLift MajorLift changed the title [base-controller] Fix getRestricted not inferring omitted Allowed{Actions,Events} as never when used with unrestrictedControllerMessenger pattern [base-controller] Fix getRestricted not inferring omitted allowlists as empty Mar 8, 2024
@MajorLift MajorLift changed the title [base-controller] Fix getRestricted not inferring omitted allowlists as empty [base-controller] Fix getRestricted not inferring allowlists as empty, when they are omitted in both function and generic arguments Mar 8, 2024
…and function params and assigning directly to `messenger` in controller constructor prevents `Allowed{Action,Event}` generic defaults (`never`) from being applied. Assigning restricted messenger to variable first fixes issue.
@MajorLift MajorLift force-pushed the 240307-fix-getRestricted-not-infers-omitted-allowlists-as-empty-array branch from 522ece0 to 142cc22 Compare March 8, 2024 17:39
@MajorLift MajorLift changed the title [base-controller] Fix getRestricted not inferring allowlists as empty, when they are omitted in both function and generic arguments [base-controller] Make allowed{Actions,Events} required parameters of getRestricted Mar 8, 2024
@MajorLift MajorLift marked this pull request as ready for review March 8, 2024 18:39
@MajorLift MajorLift requested review from a team as code owners March 8, 2024 18:39
@MajorLift MajorLift force-pushed the 240307-fix-getRestricted-not-infers-omitted-allowlists-as-empty-array branch from 0a9af45 to 8fcaf68 Compare March 8, 2024 18:43
Mrtenz
Mrtenz previously approved these changes Mar 8, 2024
@@ -45,6 +45,8 @@ function getRestrictedMessenger(
) {
return controllerMessenger.getRestricted({
name,
allowedActions: [],
allowedEvents: [],
}) as RateLimitMessenger<RateLimitedApis>;
Copy link
Member

Choose a reason for hiding this comment

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

Can this be removed now? I noticed you removed some type casts in other files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I missed that one. Removed here: 9cdcbf1

@MajorLift MajorLift force-pushed the 240307-fix-getRestricted-not-infers-omitted-allowlists-as-empty-array branch from 8fcaf68 to 9cdcbf1 Compare March 8, 2024 19:04
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

@MajorLift MajorLift merged commit 7ccd8e9 into main Mar 8, 2024
139 checks passed
@MajorLift MajorLift deleted the 240307-fix-getRestricted-not-infers-omitted-allowlists-as-empty-array branch March 8, 2024 20:26
@Gudahtt
Copy link
Member

Gudahtt commented Mar 8, 2024

@metamaskbot publish-preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants