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

Validate Snap arguments #2

Closed
vlopes11 opened this issue Oct 9, 2023 · 0 comments · Fixed by #7
Closed

Validate Snap arguments #2

vlopes11 opened this issue Oct 9, 2023 · 0 comments · Fixed by #7

Comments

@vlopes11
Copy link

vlopes11 commented Oct 9, 2023

          As noted by the comment, type validation is required here. It's very much okay to track this in a separate issue without further modifications to the current PR, but I want to stress how important it is for the Snap to validate every last piece of data it receives from RPC. It's very easy to introduce security holes if we don't.

Upstream Metamask Snaps sources seem to rely on superstruct quite heavily, and they actually provide some helpers here. Our validation logic may look something like this:

import { Bytes } from '@metamask/utils';
import { Bip32PathStruct } from '@metamask/snaps-utils/*';
import { Infer, boolean, enums, object, optional, type } from 'superstruct';

export const SupportedCurveEnums = enums(['ed25519', 'sec256k1']);

/**
 * The supported curves for deriving a BIP-32 account.
 */
export type SupportedCurve = Infer<typeof SupportedCurveEnums>;

/**
 * `type` is used instead of `object` to allow unknown properties.
 */
export const GetBip32PublicKeyParamsStruct = type({
  /**
   * The BIP-32 path to the account.
   */
  path: Bip32PathStruct,
  /**
   * The curve used to derive the account.
   */
  curve: SupportedCurveEnums,
  /**
   * Whether to return the public key in compressed form.
   */
  compressed: optional(boolean()),
});

/**
 * The parameters for calling the `getPublicKey` JSON-RPC method.
 *
 * Unknown properties are ignored and passed to `snap_getBip32PublicKey`.
 */
export type GetBip32PublicKeyParams = Infer<
  typeof GetBip32PublicKeyParamsStruct
>;

export const SignMessageParamsStruct = object({
  /**
   * The BIP-32 path to the account.
   */
  message: Bytes,
  /**
   * The curve used to derive the account.
   */
  curve: SupportedCurveEnums,
  /**
   * The BIP-32 path to the account.
   */
  path: Bip32PathStruct,
});

/**
 * The parameters for calling the `signMessage` JSON-RPC method.
 */
export type SignMessageParams = Infer<typeof SignMessageParamsStruct>;

Originally posted by @neysofu in #1 (comment)

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