Skip to content

Commit 0a265dc

Browse files
fix: Properly terminate origin regex with $ (#2576)
Fixes an oversight in the regex generation for `allowedOrigins` that allows a bypass of the functionality due to the regex not being properly terminated. Also fixes a mistake in the regex generation that wouldn't properly add `.*` to the regex but instead use somewhat escaped characters.
1 parent c7f73bd commit 0a265dc

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/snaps-utils/src/json-rpc.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ describe('isOriginAllowed', () => {
208208
expect(
209209
isOriginAllowed(origins, SubjectType.Website, 'https://foo.metamask.io'),
210210
).toBe(true);
211+
expect(
212+
isOriginAllowed(
213+
origins,
214+
SubjectType.Website,
215+
'https://foo.metamask.io.bad.com',
216+
),
217+
).toBe(false);
211218
});
212219

213220
it('supports multiple wildcards', () => {

packages/snaps-utils/src/json-rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ function createOriginRegExp(matcher: string) {
107107
// Escape potential Regex characters
108108
const escaped = matcher.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
109109
// Support wildcards
110-
const regex = escaped.replace(/\*/gu, '.*');
111-
return RegExp(regex, 'u');
110+
const regex = escaped.replace(/\\\*/gu, '.*');
111+
return RegExp(`${regex}$`, 'u');
112112
}
113113

114114
/**

0 commit comments

Comments
 (0)