Skip to content

Commit

Permalink
fix: Add line beginning to origin regex (#2876)
Browse files Browse the repository at this point in the history
Add line beginning to regex to prevent incorrect matches with certain
types of origins.
  • Loading branch information
FrederikBolding authored Nov 11, 2024
1 parent 90b8e43 commit f2d8547
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 f2d8547

Please sign in to comment.