Skip to content

Commit

Permalink
fix: Add line beginning to origin regex
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 7, 2024
1 parent 90b8e43 commit 5bc3936
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions packages/snaps-utils/src/json-rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ describe('isOriginAllowed', () => {
expect(isOriginAllowed(origins, SubjectType.Website, 'bar')).toBe(false);
});

it('matches full string', () => {
const origins: RpcOrigins = {
allowedOrigins: ['https://metamask.io'],
};
expect(
isOriginAllowed(origins, SubjectType.Website, 'https://notmetamask.io'),
).toBe(false);
});

it('supports wildcards', () => {
const origins: RpcOrigins = {
allowedOrigins: ['*'],
Expand Down Expand Up @@ -195,7 +204,7 @@ describe('isOriginAllowed', () => {

it('supports partial strings with wildcards', () => {
const origins: RpcOrigins = {
allowedOrigins: ['*.metamask.io'],
allowedOrigins: ['https://*.metamask.io'],
};

expect(
Expand All @@ -219,7 +228,7 @@ describe('isOriginAllowed', () => {

it('supports multiple wildcards', () => {
const origins: RpcOrigins = {
allowedOrigins: ['*.metamask.*'],
allowedOrigins: ['https://*.metamask.*'],
};

expect(
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-utils/src/json-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function createOriginRegExp(matcher: string) {
const escaped = matcher.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
// Support wildcards
const regex = escaped.replace(/\\\*/gu, '.*');
return RegExp(`${regex}$`, 'u');
return RegExp(`^${regex}$`, 'u');
}

/**
Expand Down

0 comments on commit 5bc3936

Please sign in to comment.