Skip to content

Commit

Permalink
feat: add allow lists
Browse files Browse the repository at this point in the history
* refactor: move list tests to correct location

* feat: allow lists

Resolves #7
Resolves #8

Co-authored-by: FutureDuck <iwonder@duckduckgo.com>
  • Loading branch information
lfilho and FutureDuck committed Jul 26, 2020
1 parent d40a791 commit 283d75f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import List from '../../../src/lib/model/list.js';
import List from '../../../../src/lib/model/list.js';
import {
A_URL,
ANOTHER_URL,
URL_IN_NO_LISTS,
SOME_URLS,
} from '../../../src/shared/__url_fixtures.js';
} from '../../../../src/shared/__url_fixtures.js';

let list;
const ANOTHER_URL = URL_IN_NO_LISTS;

beforeEach(() => {
const RANDOM_TYPE = List.types.DENY_LIST;
Expand Down
19 changes: 16 additions & 3 deletions __tests__/src/lib/request_matcher.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import RequestMatcher from '../../../src/lib/request_matcher.js';
import { BAD_URL, GOOD_URL } from '../../../src/shared/__url_fixtures.js';
import {
BAD_URL,
GOOD_URL,
URL_IN_NO_LISTS,
} from '../../../src/shared/__url_fixtures.js';

beforeAll(() => RequestMatcher.denyList.add(BAD_URL));
afterAll(() => RequestMatcher.denyList.clear());
beforeEach(() => {
RequestMatcher.allowList.clear();
RequestMatcher.denyList.clear();
});

describe('Request Matcher', () => {
it('should deny urls in the deny list', () => {
RequestMatcher.denyList.add(BAD_URL);
const result = RequestMatcher.isDenied(BAD_URL);
expect(result).toBe(true);
});

it('should not deny urls if they are not in the deny list', () => {
const result = RequestMatcher.isDenied(URL_IN_NO_LISTS);
expect(result).toBe(false);
});

it('should allow urls if they are in the allow list', () => {
RequestMatcher.allowList.add(GOOD_URL);
const result = RequestMatcher.isDenied(GOOD_URL);
expect(result).toBe(false);
});
Expand Down
5 changes: 2 additions & 3 deletions src/lib/list_populator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import List from './model/list.js';
import { BAD_URLS } from '../shared/__url_fixtures.js';
import { BAD_URLS, GOOD_URLS } from '../shared/__url_fixtures.js';

// TODO for now, just populating it with the examples from fixtures
// TODO real deal: fetch from local files shipped with the extension, augment it with live sources...
Expand All @@ -11,10 +11,9 @@ export default class ListPopulator {
static async populateList(list) {
const listTypeToUrlMapper = {
[List.types.DENY_LIST]: BAD_URLS,
[List.types.ALLOW_LIST]: GOOD_URLS,
};
const urls = listTypeToUrlMapper[list.type];
list.bulkAdd(urls);

//TODO https://github.com/lfilho/ddg-test-project/issues/8
}
}
7 changes: 7 additions & 0 deletions src/lib/model/allow_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import List from './list.js';

export default class AllowList extends List {
constructor() {
super(List.types.ALLOW_LIST);
}
}
19 changes: 18 additions & 1 deletion src/lib/request_matcher.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import AllowList from './model/allow_list.js';
import DenyList from './model/deny_list.js';
import ListPopulator from './list_populator.js';

let denyList;
// Singletons.
// Meaning since the extension will run on background,
// we will only load those lists once, saving memory
let denyList, allowList;

export default class RequestMatcher {
static get allowList() {
if (!allowList) {
allowList = new AllowList();
ListPopulator.populateList(allowList);
}
return allowList;
}

static get denyList() {
if (!denyList) {
denyList = new DenyList();
Expand All @@ -13,6 +25,11 @@ export default class RequestMatcher {
}

static isDenied(url) {
// Allow list implies a user's deliberate action to allow that url
// Hence the precedence below
if (this.allowList.has(url)) {
return false;
}
return this.denyList.has(url);
}
}
6 changes: 3 additions & 3 deletions src/shared/__url_fixtures.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const A_URL = 'https://example.com';
const GOOD_URL = 'https://duckduckgo.com';
const BAD_URL = 'https://evil-tracker.com';
const ANOTHER_URL = GOOD_URL;
const SOME_URLS = [A_URL, ANOTHER_URL, BAD_URL];
const URL_IN_NO_LISTS = 'https://duck.co';
const SOME_URLS = [A_URL, URL_IN_NO_LISTS, BAD_URL];
const TEST_PAGE_GOOD_URL =
'https://itty.bitty.site/#good_page/data:text/html;charset=utf-8;bxze64,XQAAAALrAQAAAAAAAAAeGInmWR9D5qtrM4PFJv4W1okR98bzFbE2QlIiIqEKRNhWozLGaVxy2UBVi6vb5PjLiS+KmhnoBI2zbVEi38FFqGt0V2dZ/n48NtEOjSTkOFXBuNLAKC6rlcwmvnHnUnMAWA2l/QImsEbNvvf1bv40vbtBzNp9F3TGp/HpcdlmwUSp59tjbjdUlRkOVnMxBaTPI6tnqjg9UBREwBH6Y4c6xLg53hJodPJyK8AysLdOEqC5OPFdGrHq7n6ViwKq90juHDM+UhFD8ug4iSu0Yo74yBMAo7Rtj+Jd5h9AkkjDCs/m4RMIP7KQKT4AldOuVvxaNDd4LcfbH/7lFxzMpv2FPyYxeR5ZmDMwE6422v7jh2OnV4nTcu42kWkhVBP7U06PxG1bBjmd5+5p11z/jK399tAktg==';
const TEST_PAGE_BAD_URL =
Expand All @@ -14,7 +14,7 @@ export {
A_URL,
GOOD_URL,
BAD_URL,
ANOTHER_URL,
URL_IN_NO_LISTS,
SOME_URLS,
TEST_PAGE_BAD_URL,
TEST_PAGE_GOOD_URL,
Expand Down

0 comments on commit 283d75f

Please sign in to comment.