Skip to content

Typing fixes for provider, ppom and other any usage - #89

Merged
MajorLift merged 19 commits into
mainfrom
231115-fix-any-provider-typing
Feb 15, 2024
Merged

Typing fixes for provider, ppom and other any usage#89
MajorLift merged 19 commits into
mainfrom
231115-fix-any-provider-typing

Conversation

@MajorLift

@MajorLiftMajorLift commented Nov 16, 2023

Copy link
Copy Markdown
Contributor

Explanation

Reduces repo-wide any count to 1 (excluding test files).

References

Changelog

Changed

  • Add @metamask/utils ^8.3.0 as a dependency. (#89)

Removed

  • BREAKING:NetworkControllerStateChangeEvent is removed from the PPOMControllerEvents union type. (#89)

Fixed

  • BREAKING: PPOMController class constructor option types are narrowed from any. (#89)
    • The constructor expects provider to be the Provider type from @metamask/network-controller (equivalent to SafeEventEmitterProvider from @metamask/eth-json-rpc-provider).
    • The constructor expects onPreferencesChange to be (callback: (preferencesState: { securityAlertsEnabled: boolean } & Record<string, Json>) => void) => void.
  • BREAKING: The UsePPOM type replaces any with the PPOM type in its definition. (#89)
  • BREAKING: When a PPOM class instance makes JSON-RPC requests to the provider, the params type is now widened from Record<string, unknown> to JsonRpcParams, and the response type is narrowed from any to JsonRpcSuccess<Json>. (#89)

Comment threadsrc/ppom-controller.ts Outdated
Comment threadsrc/ppom-controller.ts Outdated
Comment threadsrc/ppom-controller.ts Outdated
Comment threadsrc/ppom-controller.ts Outdated
@jiexi

This comment was marked as outdated.

@MajorLift
MajorLift requested a review from jpuriNovember 16, 2023 17:28
@mcmire

This comment was marked as outdated.

@MajorLift

This comment was marked as outdated.

@MajorLift
MajorLift changed the base branch from jl/use-networkClientId-3 to mainJanuary 29, 2024 19:26
@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch from 6a5a04a to 39e9f8cCompareJanuary 29, 2024 19:30
@socket-security

This comment was marked as outdated.

@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch from 39e9f8c to 934940fCompareJanuary 29, 2024 19:35
@socket-security

socket-securityBot commented Jan 29, 2024

Copy link
Copy Markdown

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

AlertPackageNote
Network access npm/agent-base@7.1.0
Network access npm/agent-base@7.1.0
Shell access npm/foreground-child@3.1.1
Shell access npm/foreground-child@3.1.1
Network access npm/@npmcli/agent@2.2.0
Network access npm/@npmcli/agent@2.2.0
Network access npm/@npmcli/agent@2.2.0
Network access npm/@npmcli/agent@2.2.0
Network access npm/@npmcli/agent@2.2.0
Network access npm/socks-proxy-agent@8.0.2

View full report↗︎

Next steps

What is network access?

This module accesses the network.

Packages should remove all network access that is functionally unnecessary. Consumers should audit network access to ensure legitimate use.

What is shell access?

This module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.

Packages should avoid accessing the shell which can reduce portability, and make it easier for malicious shell access to be introduced.

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/foo@1.0.0 or ignore all packages with @SocketSecurity ignore-all

  • @SocketSecurity ignore npm/agent-base@7.1.0
  • @SocketSecurity ignore npm/foreground-child@3.1.1
  • @SocketSecurity ignore npm/@npmcli/agent@2.2.0
  • @SocketSecurity ignore npm/socks-proxy-agent@8.0.2

@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch 2 times, most recently from 8812de5 to 4d84e41CompareJanuary 29, 2024 20:01
@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch 2 times, most recently from d03e901 to f413c9dCompareJanuary 29, 2024 20:11
@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch from f413c9d to 46c8936CompareJanuary 30, 2024 04:51
@MajorLiftMajorLift changed the title Typing fixes for #85Typing fixes for provider, ppom and other any usageJan 30, 2024
Comment threadsrc/ppom-controller.ts
Comment threadsrc/index.ts
Comment on lines +1 to +17
export type {
NativeCrypto,
PPOMState,
UsePPOM,
UpdatePPOM,
PPOMInitialisationStatusType,
PPOMControllerActions,
PPOMControllerInitialisationStateChangeEvent,
PPOMControllerEvents,
PPOMControllerMessenger,
} from './ppom-controller';
export {
REFRESH_TIME_INTERVAL,
NETWORK_CACHE_DURATION,
PPOMInitialisationStatus,
PPOMController,
} from './ppom-controller';

@MajorLiftMajorLiftJan 30, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

There should be no breaking changes here. This is the list of existing package-level exports from the ppom-controller file.

Explicit enumeration of exports is safer, and also allows sharing exports internally, which comes in handy with e.g. AllowedEvents, controllerName for test files.

Comment threadsrc/ppom-controller.ts Outdated
Comment threadsrc/ppom-controller.ts Outdated
@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch from 9f5c709 to da5a54fCompareFebruary 2, 2024 15:49
jiexi
jiexi previously approved these changes Feb 2, 2024

@jiexijiexi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but again deferring to @jpuri as this is her domain

Comment on lines +1035 to +1036
| JsonRpcSuccess<Json>
| (Omit<JsonRpcFailure, 'error'> & { error: unknown })

@MajorLiftMajorLiftFeb 2, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I used the JsonRpcResponse<Json> type here instead of unknown, even though this technically narrows the response type in the sendAsync callback compared to any (see line 1059).

This is because JsonRpcEngine is where the callback passed into sendAsync actually gets invoked, and that class consistently uses the JsonRpcResponse type for responses.

The error type in JsonRpcFailure is widened to unknown since that's the only type accepted by sendAsync. See https://github.com/MetaMask/ppom-validator/pull/89/files#r1470601394 for a discussion on this point.

Note: sendAsync is set to be deprecated in favor of request in a new EIP1193-compliant provider class. request will use JsonRpcResponse<Json>.

mcmire
mcmire previously approved these changes Feb 6, 2024

@mcmiremcmire left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes here make sense to me, although I will also defer to @jpuri for final approval.

Comment threadjest.config.js Outdated
coverageThreshold: {
global: {
branches: 100,
branches: 98.18,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would be nice to not reduce the test coverage here 😭 What caused this?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Brought it back up to 100%! 3ff8ebe572cab2

export type PPOMControllerEvents =
| PPOMControllerInitialisationStateChangeEvent
| NetworkControllerStateChangeEvent;
export type PPOMControllerEvents = PPOMControllerInitialisationStateChangeEvent;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For context, these changes align with changes we've been making to controllers in the core repo. For instance, see SelectedNetworkController: https://github.com/MetaMask/core/blob/d3861d03df0ff3f4cf2b479f746b46e602065d76/packages/selected-network-controller/src/SelectedNetworkController.ts#L71-L89

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Thanks for adding context here. To be clear, I didn't use the ControllerGetStateAction and ControllerStateChangeEvent types since those were introduced in base-controller v4, and this package is using v3.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks a lot @MajorLift for these improvements 🙏

@MajorLift
MajorLift dismissed stale reviews from mcmire and jiexi via 572cab2February 6, 2024 23:16
Comment on lines +65 to +71
// Provisional skeleton type for PPOM class
// TODO: Replace with actual PPOM class
type PPOM = {
new: (...args: unknown[]) => PPOM;
validateJsonRpc: () => Promise<unknown>;
free: () => void;
} & Record<string, unknown>;

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Wrote temporary PPOM type that expresses a minimal interface. Added TODO so this would be eventually replaced with a full type.

Comment on lines +294 to +298
// TOOD: Replace with `PreferencesState` from `@metamask/preferences-controller`
preferencesState: { securityAlertsEnabled: boolean } & Record<
string,
Json
>,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Wrote temporary PreferencesState surrogate to avoid introducing @metamask/preferences-controller as a dependency. Added TODO so this would be eventually replaced with the full type.

Comment on lines +568 to +572
// TOOD: Replace with `PreferencesState` from `@metamask/preferences-controller`
preferenceControllerState: { securityAlertsEnabled: boolean } & Record<
string,
Json
>,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Same as above.

@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch from e358aa5 to 2d2a2deCompareFebruary 9, 2024 06:36
@MajorLift

Copy link
Copy Markdown
ContributorAuthor

Added Changelog with breaking changes highlighted.

Comment threadCHANGELOG.md
@@ -5,6 +5,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm curious about the Changelog changes. Typically the changelog only includes user-facing changes and it is generated with the update-changelog script during the RC phase 🤔 for this reason I think we should not include these changes here?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I removed some entries that weren't user-facing. Thanks for pointing that out!

All of the remaining entries are major changes for package-level exports. See our documentation on reviewing release PRs for how these qualify as breaking changes.

As for why these are in the changelog file, the shared libraries team has been recording changelog entries for new PRs under the "Unreleased" heading, so that they're immediately available for reference when the next release is being created.

update-changelog auto-populates a list of unreleased commits to changelog, but these are only intended as starting points. Recording under "Unreleased" streamlines the process of retrieving the full changelog entries by making it unnecessary to visit each linked PR and check the changelog section of the PR description.

@MajorLiftMajorLiftFeb 9, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

That said, I've realized our team won't be creating releases for this repo, and introducing a new practice like this will be confusing.

I've removed the additions to the changelog file: 3d957cd. The changelog entries can be found in the PR description!

@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch 5 times, most recently from 7cdd21f to 5fc353eCompareFebruary 9, 2024 16:38
@MajorLift
MajorLiftforce-pushed the 231115-fix-any-provider-typing branch from 5fc353e to 9af9e41CompareFebruary 9, 2024 16:39
@MajorLift
MajorLift merged commit 2d00a58 into mainFeb 15, 2024
@MajorLift
MajorLift deleted the 231115-fix-any-provider-typing branch February 15, 2024 05:47
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ppom-validator] Replace use of any with proper types (non-test files only)

5 participants

@MajorLift@jiexi@mcmire@jpuri@seaona