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

feat(base-controller): Allow using internal events/actions #2050

Merged
merged 4 commits into from
Nov 17, 2023

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Nov 16, 2023

Explanation

The restricted controller messenger now allows using all internal events and actions. Previously internal events were treated the same as external events, they had to be explicitly allowed.

This is a non-breaking change; internal actions/events are still permitted to be list as "allowed", it's just no longer necessary.

References

Resolves #2047

Changelog

@metamask/base-controller

  • CHANGED: The restricted controller messenger now allows calling all internal events and actions
    • Previously internal events and actions were only usable if they were listed as "allowed". They are still permitted to be listed as "allowed" now, but it is no longer necessary.

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

@Gudahtt Gudahtt requested a review from a team as a code owner November 16, 2023 15:15
@MajorLift
Copy link
Contributor

MajorLift commented Nov 16, 2023

Looks good! I added some updates building on this: #2051

* @returns Whether the event type is allowed.
*/
#isAllowedEvent(eventType: Event['type']): eventType is AllowedEvent {
// Safely upcast to allow runtime check
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, good trick.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I thought so! I was considering suggesting this strategy in our docs.

That is, if you want to upcast something (which is always safe to do), assign it to a new variable. This lets TypeScript validate that it's a safe upcast.

Whereas the as operator also allows downcasting, so it's not as safe to use.

The restricted controller messenger now allows using all internal
events and actions. Previously internal events were treated the same as
external events, they had to be explicitly allowed.

This is a non-breaking change; internal actions/events are still
permitted to be list as "allowed", it's just no longer necessary.

Resolves #2047
@Gudahtt Gudahtt force-pushed the do-not-require-allowing-own-actions-events branch from 4fd01b5 to 6542f62 Compare November 16, 2023 21:26
@Gudahtt
Copy link
Member Author

Gudahtt commented Nov 16, 2023

Thanks for the feedback @MajorLift , I removed the unnecessary allowed action/event types from the tests and I've propagated the new helper function to those three spots you found. The rest of the changes in that PR seem better to handle separately, but let me know if I've missed something

@MajorLift
Copy link
Contributor

Agreed on the scope of the PR, but I think the @throws tags might belong here. Otherwise everything looks good!

Copy link
Contributor

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

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

LGTM!

@Gudahtt Gudahtt merged commit 410e782 into main Nov 17, 2023
128 checks passed
@Gudahtt Gudahtt deleted the do-not-require-allowing-own-actions-events branch November 17, 2023 00:16
mcmire added a commit that referenced this pull request Nov 22, 2023
…ot initialized with internal actions/events in allow lists (#2051)

## Explanation

- `RestrictedControllerMessenger` constructor and
`ControllerMessenger.getRestricted()` method now raise a type error if
an internal action is passed into `allowedActions`, or an internal event
into `allowedEvents`.
	- ` Type 'string' is not assignable to type 'never'.`
- `Type '"SomeController:internalAction"' is not assignable to type
'"OtherController:externalAction1" |
"OtherController:externalAction2"'.`
- Fixes breaking tests in downstream controllers in core repo.

## Impact

- The `allowed{Actions,Events}` arrays in
`ControllerMessenger.getRestricted()` now align with the
`Allowed{Actions,Events}` generic params of
`RestrictedControllerMessenger`: Both only enumerate external
actions/events.
- This commit disallows `allowed{Actions,Events}` arrays that contain an
incomplete list of internal actions/events.
- A partial list doesn't provide any useful information as all internal
actions/events will be available to the messenger regardless of whether
any of them are allowlisted.
- A partial list also gives the confusing impression that some internal
actions/events can be *disallowed* through omission, which is not true
as of #2050.

---------

Co-authored-by: Elliot Winkler <[email protected]>
mcmire added a commit that referenced this pull request Nov 22, 2023
…ot initialized with internal actions/events in allow lists (#2051)

## Explanation

- `RestrictedControllerMessenger` constructor and
`ControllerMessenger.getRestricted()` method now raise a type error if
an internal action is passed into `allowedActions`, or an internal event
into `allowedEvents`.
	- ` Type 'string' is not assignable to type 'never'.`
- `Type '"SomeController:internalAction"' is not assignable to type
'"OtherController:externalAction1" |
"OtherController:externalAction2"'.`
- Fixes breaking tests in downstream controllers in core repo.

## Impact

- The `allowed{Actions,Events}` arrays in
`ControllerMessenger.getRestricted()` now align with the
`Allowed{Actions,Events}` generic params of
`RestrictedControllerMessenger`: Both only enumerate external
actions/events.
- This commit disallows `allowed{Actions,Events}` arrays that contain an
incomplete list of internal actions/events.
- A partial list doesn't provide any useful information as all internal
actions/events will be available to the messenger regardless of whether
any of them are allowlisted.
- A partial list also gives the confusing impression that some internal
actions/events can be *disallowed* through omission, which is not true
as of #2050.

---------

Co-authored-by: Elliot Winkler <[email protected]>
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.

[base-controller]: Always allow using actions/events from the same controller
3 participants