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

BREAKING: Restrict proposed name in manifest #2796

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"branches": 99.74,
"functions": 98.91,
"lines": 99.45,
"statements": 96.3
"statements": 96.31
}
1 change: 1 addition & 0 deletions packages/snaps-utils/src/manifest/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ describe('isSnapManifest', () => {
{ name: 'foo' },
{ version: '1.0.0' },
getSnapManifest({ version: 'foo bar' }),
getSnapManifest({ proposedName: '😄' }),
])('returns false for an invalid snap manifest', (value) => {
expect(isSnapManifest(value)).toBe(false);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/snaps-utils/src/manifest/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ import { KeyringOriginsStruct, RpcOriginsStruct } from '../json-rpc';
import { ChainIdStruct } from '../namespace';
import { SnapIdStruct } from '../snaps';
import { mergeStructs, type InferMatching } from '../structs';
import { NameStruct, NpmSnapFileNames, uri } from '../types';
import {
NameStruct,
NpmSnapFileNames,
ProposedNameStruct,
uri,
} from '../types';

// BIP-43 purposes that cannot be used for entropy derivation. These are in the
// string form, ending with `'`.
Expand Down Expand Up @@ -266,7 +271,7 @@ export type InitialConnections = Infer<typeof InitialConnectionsStruct>;
export const SnapManifestStruct = object({
version: VersionStruct,
description: size(string(), 1, 280),
proposedName: size(string(), 1, 214),
proposedName: ProposedNameStruct,
repository: optional(
type({
type: size(string(), 1, Infinity),
Expand Down
15 changes: 14 additions & 1 deletion packages/snaps-utils/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { enums, is, literal } from '@metamask/superstruct';

import { isValidUrl, uri } from './types';
import { ProposedNameStruct, isValidUrl, uri } from './types';

describe('ProposedNameStruct', () => {
it.each(['foo', 'bar baz', 'foo-bar', '@foo/bar', '@^_-()[]'])(
'accepts %p',
(value) => {
expect(is(value, ProposedNameStruct)).toBe(true);
},
);

it.each([1, '', '😄', String.fromCharCode(8206)])('rejects %p', (value) => {
expect(is(value, ProposedNameStruct)).toBe(false);
});
});

describe.each([
isValidUrl,
Expand Down
6 changes: 6 additions & 0 deletions packages/snaps-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const NameStruct = size(
214,
);

/**
* A struct that matches anything from ASCII index 32 (space) to 126 (~), i.e.,
* all printable ASCII characters, between 1 and 214 characters long.
*/
export const ProposedNameStruct = size(pattern(string(), /^[ -~]+$/u), 1, 214);

// Note we use `type` instead of `object` here, because the latter does not
// allow unknown keys.
export const NpmSnapPackageJsonStruct = type({
Expand Down
Loading