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

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

Merged
merged 19 commits into from
Feb 15, 2024

Conversation

MajorLift
Copy link
Contributor

@MajorLift MajorLift commented Nov 16, 2023

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)

src/ppom-controller.ts Outdated Show resolved Hide resolved
src/ppom-controller.ts Outdated Show resolved Hide resolved
src/ppom-controller.ts Outdated Show resolved Hide resolved
src/ppom-controller.ts Outdated Show resolved Hide resolved
@jiexi

This comment was marked as outdated.

@MajorLift MajorLift requested a review from jpuri November 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 main January 29, 2024 19:26
@MajorLift MajorLift force-pushed the 231115-fix-any-provider-typing branch from 6a5a04a to 39e9f8c Compare January 29, 2024 19:30

This comment was marked as outdated.

@MajorLift MajorLift force-pushed the 231115-fix-any-provider-typing branch from 39e9f8c to 934940f Compare January 29, 2024 19:35
Copy link

socket-security bot commented Jan 29, 2024

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

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

Alert Package Note
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 MajorLift force-pushed the 231115-fix-any-provider-typing branch 2 times, most recently from 8812de5 to 4d84e41 Compare January 29, 2024 20:01
@MajorLift MajorLift force-pushed the 231115-fix-any-provider-typing branch 2 times, most recently from d03e901 to f413c9d Compare January 29, 2024 20:11
@MajorLift MajorLift force-pushed the 231115-fix-any-provider-typing branch from f413c9d to 46c8936 Compare January 30, 2024 04:51
@MajorLift MajorLift changed the title Typing fixes for #85 Typing fixes for provider, ppom and other any usage Jan 30, 2024
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';
Copy link
Contributor Author

@MajorLift MajorLift Jan 30, 2024

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.

src/ppom-controller.ts Outdated Show resolved Hide resolved
src/ppom-controller.ts Outdated Show resolved Hide resolved
@MajorLift MajorLift force-pushed the 231115-fix-any-provider-typing branch from 9f5c709 to da5a54f Compare February 2, 2024 15:49
jiexi
jiexi previously approved these changes Feb 2, 2024
Copy link
Contributor

@jiexi jiexi left a comment

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 })
Copy link
Contributor Author

@MajorLift MajorLift Feb 2, 2024

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
Copy link
Collaborator

@mcmire mcmire left a comment

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.

jest.config.js Outdated
@@ -39,10 +39,10 @@ module.exports = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 100,
branches: 98.18,
Copy link
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
Contributor Author

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%! 3ff8ebe 572cab2

export type PPOMControllerEvents =
| PPOMControllerInitialisationStateChangeEvent
| NetworkControllerStateChangeEvent;
export type PPOMControllerEvents = PPOMControllerInitialisationStateChangeEvent;
Copy link
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
Contributor Author

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
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 572cab2 February 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
Contributor Author

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
Contributor Author

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
Contributor Author

Choose a reason for hiding this comment

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

Same as above.

@MajorLift MajorLift force-pushed the 231115-fix-any-provider-typing branch from e358aa5 to 2d2a2de Compare February 9, 2024 06:36
@MajorLift
Copy link
Contributor Author

Added Changelog with breaking changes highlighted.

CHANGELOG.md Outdated
@@ -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

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
Contributor Author

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.

Copy link
Contributor Author

@MajorLift MajorLift Feb 9, 2024

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 MajorLift force-pushed the 231115-fix-any-provider-typing branch 5 times, most recently from 7cdd21f to 5fc353e Compare February 9, 2024 16:38
@MajorLift MajorLift force-pushed the 231115-fix-any-provider-typing branch from 5fc353e to 9af9e41 Compare February 9, 2024 16:39
@MajorLift MajorLift merged commit 2d00a58 into main Feb 15, 2024
13 of 14 checks passed
@MajorLift MajorLift deleted the 231115-fix-any-provider-typing branch February 15, 2024 05:47
Sign up for free to 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