Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-1477 Wildcard/Regex Whitelisting #497

Closed
wants to merge 24 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7db8cdd
Add regex and wildcard functionality to whitelist and blacklist
benstrumeyer Feb 8, 2020
7fdc2d8
Escape inputted regex and add error handling
benstrumeyer Feb 10, 2020
f07e151
Add escape-string-regexp dependency
benstrumeyer Feb 10, 2020
3cff5eb
Refactor matchesWildcardOrRegex and remove escape-strings-regex depen…
benstrumeyer Feb 11, 2020
a03896f
Make regex variables const
benstrumeyer Feb 11, 2020
b025685
Merge branch 'develop' into regex-whitelisting
christophertino Feb 14, 2020
3930487
Prevent ReDoS attack. Validate url, wildcard or regex. Update error m…
benstrumeyer Feb 18, 2020
48e8e60
Merge branch 'regex-whitelisting' of github.com:ghostery/ghostery-ext…
benstrumeyer Feb 18, 2020
63ec3b6
Remove newline
benstrumeyer Feb 18, 2020
fc1f621
Add period to error text
benstrumeyer Feb 18, 2020
fcd5e9a
Merge branch 'develop' into regex-whitelisting
benstrumeyer Feb 18, 2020
b9186bc
GH-1947 Plus checkout UTM params (#499)
benstrumeyer Feb 21, 2020
a726e87
update translations
christophertino Feb 21, 2020
be06d00
Add regex and wildcard functionality to whitelist and blacklist
benstrumeyer Feb 8, 2020
863f225
Escape inputted regex and add error handling
benstrumeyer Feb 10, 2020
032cbc6
Add escape-string-regexp dependency
benstrumeyer Feb 10, 2020
8a6533c
Refactor matchesWildcardOrRegex and remove escape-strings-regex depen…
benstrumeyer Feb 11, 2020
5e6e3c2
Make regex variables const
benstrumeyer Feb 11, 2020
16b89a7
Prevent ReDoS attack. Validate url, wildcard or regex. Update error m…
benstrumeyer Feb 18, 2020
1c63fcb
Remove newline
benstrumeyer Feb 18, 2020
d35ef22
Add period to error text
benstrumeyer Feb 18, 2020
6fdf07b
Create unit and snapshot test for isValidUrlWildcard function
benstrumeyer Feb 19, 2020
ab3ce25
Add unit tests for background portion
benstrumeyer Feb 21, 2020
abe1de5
Fix merge conflicts
benstrumeyer Feb 21, 2020
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

Create unit and snapshot test for isValidUrlWildcard function
  • Loading branch information
benstrumeyer committed Feb 21, 2020
commit 6fdf07b92b93d5d60b36d803804f10d219efd82d
@@ -163,13 +163,15 @@ class TrustAndRestrict extends React.Component {
if (!safe(pageHost)) return false;

// Check for valid regex
let isValidRegex = true;
try {
// eslint-disable-next-line
new RegExp(pageHost);
} catch {
return false;
isValidRegex = false;
}
return false;

return isValidRegex;
}

/**
@@ -0,0 +1,91 @@
/**
* Rewards Test Component
*
* Ghostery Browser Extension
* https://www.ghostery.com/
*
* Copyright 2019 Ghostery, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import React from 'react';
import renderer from 'react-test-renderer';
import { shallow } from 'enzyme';
import { when } from 'jest-when';
import TrustAndRestrict from '../TrustAndRestrict';

describe('app/panel/components/Settings/TrustAndRestrict', () => {
describe('Snapshot test with react-test-renderer', () => {
test('Testing TrustAndRestrict is rendering', () => {
const wrapper = renderer.create(
<TrustAndRestrict />
).toJSON();
expect(wrapper).toMatchSnapshot();
});
});
});

describe('app/panel/components/Settings/', () => {
test('isValidUrlWildcardOrRegex should return true with url entered', () => {
const wrapper = shallow(<TrustAndRestrict />);
const input = 'ghostery.com';

const fn = jest.spyOn(wrapper.instance(), 'isValidUrlWildcardOrRegex');
when(fn)
.calledWith(input)
.mockReturnValue(true);
const returnValue = wrapper.instance().isValidUrlWildcardOrRegex(input);
expect(returnValue).toBe(true);
});

test('isValidUrlWildcardOrRegex should return true with wildcard URL entered', () => {
const wrapper = shallow(<TrustAndRestrict />);
const input = 'developer.*.org';

const fn = jest.spyOn(wrapper.instance(), 'isValidUrlWildcardOrRegex');
when(fn)
.calledWith(input)
.mockReturnValue(true);
const returnValue = wrapper.instance().isValidUrlWildcardOrRegex(input);
expect(returnValue).toBe(true);
});

test('isValidUrlWildcardOrRegex should return true with regex URL entered', () => {
const wrapper = shallow(<TrustAndRestrict />);
const input = '[ds]eveloper.mozilla.org';

const fn = jest.spyOn(wrapper.instance(), 'isValidUrlWildcardOrRegex');
when(fn)
.calledWith(input)
.mockReturnValue(true);
const returnValue = wrapper.instance().isValidUrlWildcardOrRegex(input);
expect(returnValue).toBe(true);
});

test('isValidUrlWildcardOrRegex should return false with unsafe regex entered', () => {
const wrapper = shallow(<TrustAndRestrict />);
const input = '/^(\w+\s?)*$/';

const fn = jest.spyOn(wrapper.instance(), 'isValidUrlWildcardOrRegex');
when(fn)
.calledWith(input)
.mockReturnValue(false);
const returnValue = wrapper.instance().isValidUrlWildcardOrRegex(input);
expect(returnValue).toBe(false);
});

test('isValidUrlWildcardOrRegex should return false with incorrect regex format entered', () => {
const wrapper = shallow(<TrustAndRestrict />);
const input = '[.ghostery.com';

const fn = jest.spyOn(wrapper.instance(), 'isValidUrlWildcardOrRegex');
when(fn)
.calledWith(input)
.mockReturnValue(false);
const returnValue = wrapper.instance().isValidUrlWildcardOrRegex(input);
expect(returnValue).toBe(false);
});
});
@@ -0,0 +1,119 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`app/panel/components/Settings/TrustAndRestrict Snapshot test with react-test-renderer Testing TrustAndRestrict is rendering 1`] = `
<div
className="s-trust-restrict-panel s-tabs-panel"
>
<div
className="row"
>
<div
className="columns"
>
<h3>
settings_trusted_restricted_sites
</h3>
</div>
</div>
<div
className="s-trust-restrict-menu"
>
<div
className="s-active-pane s-pane-title"
id="showTrustedSites"
onClick={[Function]}
>
<span>
settings_trusted_sites
</span>
</div>
<div
className="s-pane-title-next"
id="showRestrictedSites"
onClick={[Function]}
>
<span>
settings_restricted_sites
</span>
</div>
</div>
<div
className="s-sites-pane"
>
<div
className="row"
>
<div
className="columns"
>
<div
className="s-sites-input-box"
>
<input
onChange={[Function]}
onKeyDown={[Function]}
placeholder="settings_sites_placeholder"
type="text"
value=""
/>
<div
className="s-sites-input-icon"
onClick={[Function]}
/>
</div>
<div
className="s-site-description"
>
<span>
settings_trusted_sites_description
</span>
</div>
<div
className="s-invisible s-callout"
>

</div>
</div>
</div>
</div>
<div
className="s-hide s-sites-pane"
>
<div
className="row"
>
<div
className="columns"
>
<div
className="s-sites-input-box"
>
<input
onChange={[Function]}
onKeyDown={[Function]}
placeholder="settings_sites_placeholder"
type="text"
value=""
/>
<div
className="s-sites-input-icon"
onClick={[Function]}
/>
</div>
<div
className="s-site-description"
>
<span>
settings_restricted_sites_description
</span>
</div>
<div
className="s-invisible s-callout"
>

</div>
</div>
</div>
</div>
</div>
`;
@@ -92,6 +92,7 @@
"eslint-plugin-react": "^7.18.3",
"fs-extra": "^8.1.0",
"jest": "^25.1.0",
"jest-when": "^2.7.0",
"jsdoc": "^3.6.3",
"jsonfile": "^5.0.0",
"license-checker": "^25.0.1",
ProTip! Use n and p to navigate between commits in a pull request.