Skip to content

Commit

Permalink
Fix issue installing non-allowlisted Snaps in allowlist mode
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Feb 19, 2024
1 parent c644259 commit 0dfcd71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
24 changes: 12 additions & 12 deletions packages/snaps-controllers/src/snaps/registry/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,38 +354,38 @@ describe('JsonSnapsRegistry', () => {
expect(result).toBe('1.0.0');
});

it('throws if snap is not on the allowlist', async () => {
it('returns version range if snap is not on the allowlist', async () => {
fetchMock
.mockResponseOnce(
JSON.stringify({ verifiedSnaps: {}, blockedSnaps: [] }),
)
.mockResponseOnce(JSON.stringify(MOCK_EMPTY_SIGNATURE_FILE));

const range = '^1.0.0' as SemVerRange;
const { messenger } = getRegistry();
await expect(
messenger.call(
expect(
await messenger.call(
'SnapsRegistry:resolveVersion',
MOCK_SNAP_ID,
'^1.0.0' as SemVerRange,
range,
),
).rejects.toThrow('The snap is not on the allowlist');
).toBe(range);
});

it('throws if resolved version is not on the allowlist', async () => {
it('returns version range if resolved version is not on the allowlist', async () => {
fetchMock
.mockResponseOnce(JSON.stringify(MOCK_DATABASE))
.mockResponseOnce(JSON.stringify(MOCK_SIGNATURE_FILE));

const range = '^1.2.0' as SemVerRange;
const { messenger } = getRegistry();
await expect(
messenger.call(
expect(
await messenger.call(
'SnapsRegistry:resolveVersion',
MOCK_SNAP_ID,
'^1.2.0' as SemVerRange,
range,
),
).rejects.toThrow(
'No matching versions of the snap are on the allowlist',
);
).toBe(range);
});

it('refetches the database on allowlist miss if configured', async () => {
Expand Down
18 changes: 10 additions & 8 deletions packages/snaps-controllers/src/snaps/registry/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,12 @@ export class JsonSnapsRegistry extends BaseController<
}

/**
* Find an allowlisted version within a specified version range.
* Find an allowlisted version within a specified version range. Otherwise return the version range itself.
*
* @param snapId - The ID of the snap we are trying to resolve a version for.
* @param versionRange - The version range.
* @param refetch - An optional flag used to determine if we are refetching the registry.
* @returns An allowlisted version within the specified version range.
* @throws If an allowlisted version does not exist within the version range.
* @returns An allowlisted version within the specified version range if available otherwise returns the input version range.
*/
async #resolveVersion(
snapId: string,
Expand All @@ -304,7 +303,10 @@ export class JsonSnapsRegistry extends BaseController<
return this.#resolveVersion(snapId, versionRange, true);
}

assert(versions, 'The snap is not on the allowlist');
// If we cannot narrow down the version range we return the unaltered version range.
if (!versions) {
return versionRange;
}

const targetVersion = getTargetVersion(
Object.keys(versions) as SemVerVersion[],
Expand All @@ -316,10 +318,10 @@ export class JsonSnapsRegistry extends BaseController<
return this.#resolveVersion(snapId, versionRange, true);
}

assert(
targetVersion,
'No matching versions of the snap are on the allowlist',
);
// If we cannot narrow down the version range we return the unaltered version range.
if (!targetVersion) {
return versionRange;
}

// A semver version is technically also a valid semver range.
assertIsSemVerRange(targetVersion);
Expand Down

0 comments on commit 0dfcd71

Please sign in to comment.