Skip to content

Commit e888977

Browse files
evgenyfedorenkobrandonroberts
authored andcommittedNov 20, 2019
fix(store-devtools): escaping the safelist and blocklist strings (#2259)
Closes #2228
1 parent 2fdfe17 commit e888977

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed
 

‎modules/store-devtools/spec/extension.spec.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,15 @@ describe('DevtoolsExtension', () => {
374374
});
375375

376376
describe('with Action and actionsBlocklist', () => {
377-
const NORMAL_ACTION = 'NORMAL_ACTION';
378-
const BLOCKED_ACTION = 'BLOCKED_ACTION';
377+
const NORMAL_ACTION = '[Test] NORMAL_ACTION';
378+
const BLOCKED_ACTION_1 = '[Test] BLOCKED_ACTION #1';
379+
const BLOCKED_ACTION_2 = '[Test] BLOCKED_ACTION #2';
379380

380381
beforeEach(() => {
381382
devtoolsExtension = new DevtoolsExtension(
382383
reduxDevtoolsExtension,
383384
createConfig({
384-
actionsBlocklist: [BLOCKED_ACTION],
385+
actionsBlocklist: [BLOCKED_ACTION_1, BLOCKED_ACTION_2],
385386
}),
386387
<any>null
387388
);
@@ -402,22 +403,28 @@ describe('DevtoolsExtension', () => {
402403
state
403404
);
404405
devtoolsExtension.notify(
405-
new PerformAction({ type: BLOCKED_ACTION }, 1234567),
406+
new PerformAction({ type: BLOCKED_ACTION_1 }, 1234567),
407+
state
408+
);
409+
devtoolsExtension.notify(
410+
new PerformAction({ type: BLOCKED_ACTION_2 }, 1234567),
406411
state
407412
);
413+
408414
expect(extensionConnection.send).toHaveBeenCalledTimes(2);
409415
});
410416
});
411417

412418
describe('with Action and actionsSafelist', () => {
413-
const NORMAL_ACTION = 'NORMAL_ACTION';
414-
const SAFE_ACTION = 'SAFE_ACTION';
419+
const NORMAL_ACTION = '[Test] NORMAL_ACTION';
420+
const SAFE_ACTION_1 = '[Test] SAFE_ACTION #1';
421+
const SAFE_ACTION_2 = '[Test] SAFE_ACTION #2';
415422

416423
beforeEach(() => {
417424
devtoolsExtension = new DevtoolsExtension(
418425
reduxDevtoolsExtension,
419426
createConfig({
420-
actionsSafelist: [SAFE_ACTION],
427+
actionsSafelist: [SAFE_ACTION_1, SAFE_ACTION_2],
421428
}),
422429
<any>null
423430
);
@@ -438,10 +445,15 @@ describe('DevtoolsExtension', () => {
438445
state
439446
);
440447
devtoolsExtension.notify(
441-
new PerformAction({ type: SAFE_ACTION }, 1234567),
448+
new PerformAction({ type: SAFE_ACTION_1 }, 1234567),
442449
state
443450
);
444-
expect(extensionConnection.send).toHaveBeenCalledTimes(1);
451+
devtoolsExtension.notify(
452+
new PerformAction({ type: SAFE_ACTION_2 }, 1234567),
453+
state
454+
);
455+
456+
expect(extensionConnection.send).toHaveBeenCalledTimes(2);
445457
});
446458
});
447459

‎modules/store-devtools/src/utils.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,18 @@ export function isActionFiltered(
166166
) {
167167
const predicateMatch = predicate && !predicate(state, action.action);
168168
const safelistMatch =
169-
safelist && !action.action.type.match(safelist.join('|'));
169+
safelist &&
170+
!action.action.type.match(safelist.map(s => escapeRegExp(s)).join('|'));
170171
const blocklistMatch =
171-
blockedlist && action.action.type.match(blockedlist.join('|'));
172+
blockedlist &&
173+
action.action.type.match(blockedlist.map(s => escapeRegExp(s)).join('|'));
172174
return predicateMatch || safelistMatch || blocklistMatch;
173175
}
176+
177+
/**
178+
* Return string with escaped RegExp special characters
179+
* https://stackoverflow.com/a/6969486/1337347
180+
*/
181+
function escapeRegExp(s: string): string {
182+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
183+
}

0 commit comments

Comments
 (0)
Please sign in to comment.