Split addition tests, expand non-url checks (#885)

* Split addition tests, expand non-url checks

* Spelling

* Additional valid url check

* Meta changes
This commit is contained in:
Jaco
2022-01-02 09:20:34 +01:00
committed by GitHub
parent 080afd8fa5
commit 60bd4b6d64
8 changed files with 88 additions and 97 deletions
+82
View File
@@ -0,0 +1,82 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0
import fs from 'fs';
import { decodeAddress } from '@polkadot/util-crypto';
const addresses = JSON.parse(fs.readFileSync('address.json', 'utf-8')) as Record<string, string[]>;
const allowed = JSON.parse(fs.readFileSync('known.json', 'utf-8')) as Record<string, string[]>;
const all = JSON.parse(fs.readFileSync('all.json', 'utf8')) as { deny: string[] };
describe('added addresses', (): void => {
it('has no malformed addresses', (): void => {
const invalids = Object
.entries(addresses)
.map(([url, addrs]): [string, string[]] => {
return [url, addrs.filter((a) => {
try {
return decodeAddress(a).length !== 32;
} catch (error) {
console.error(url, (error as Error).message);
return true;
}
})];
})
.filter(([, addrs]) => addrs.length);
if (invalids.length) {
throw new Error(`Invalid ss58 checksum addresses found: ${invalids.map(([url, addrs]) => `\n\t${url}: ${addrs.join(', ')}`).join('')}`);
}
});
it('has no entries on the known addresses list', (): void => {
const added = Object
.values(addresses)
.reduce<string[]>((all, addrs) => all.concat(addrs), []);
const dupes = Object
.entries(allowed)
.reduce<[string, string][]>((all, [site, addrs]) => all.concat(addrs.map((a) => [site, a])), [])
.filter(([, a]) => added.includes(a));
expect(dupes).toEqual([]);
});
});
describe('added urls', (): void => {
it('has no malformed domain-only entries', (): void => {
const invalids = all.deny.filter((u) =>
u.includes('/') || // don't allow paths
u.includes('?') || // don't allow query params
u.includes(' ') || // no spaces
!u.includes('.') // need at least a domain
);
expect(invalids).toEqual([]);
});
it('has no urls starting with www. (domain-only inclusions)', (): void => {
const invalids = all.deny.filter((u) =>
u.startsWith('www.')
);
expect(invalids).toEqual([]);
});
it('has no duplicate entries', (): void => {
const checks: string[] = [];
const dupes = all.deny.reduce<string[]>((dupes, url) => {
if (!checks.includes(url)) {
checks.push(url);
} else {
dupes.push(url);
}
return dupes;
}, []);
expect(dupes).toEqual([]);
});
});
-76
View File
@@ -1,16 +1,8 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0
import fs from 'fs';
import { decodeAddress } from '@polkadot/util-crypto';
import { checkAddress, checkIfDenied } from '.';
const addresses = JSON.parse(fs.readFileSync('address.json', 'utf-8')) as Record<string, string[]>;
const allowed = JSON.parse(fs.readFileSync('known.json', 'utf-8')) as Record<string, string[]>;
const all = JSON.parse(fs.readFileSync('all.json', 'utf8')) as { deny: string[] };
describe('checkIfDenied', (): void => {
it('returns false when host is not listed', async (): Promise<void> => {
expect(
@@ -74,71 +66,3 @@ describe('checkAddress', (): void => {
).toEqual('polkadots.network');
});
});
describe('check additions', (): void => {
it('has no malformed addresses', (): void => {
const invalids = Object
.entries(addresses)
.map(([url, addrs]): [string, string[]] => {
return [url, addrs.filter((a) => {
try {
return decodeAddress(a).length !== 32;
} catch (error) {
console.error(url, (error as Error).message);
return true;
}
})];
})
.filter(([, addrs]) => addrs.length);
if (invalids.length) {
throw new Error(`Invalid ss58 checksum addresses found: ${invalids.map(([url, addrs]) => `\n\t${url}: ${addrs.join(', ')}`).join('')}`);
}
});
it('has no entries on the known addresses list', (): void => {
const added = Object
.values(addresses)
.reduce<string[]>((all, addrs) => all.concat(addrs), []);
const dupes = Object
.entries(allowed)
.reduce<[string, string][]>((all, [site, addrs]) => all.concat(addrs.map((a) => [site, a])), [])
.filter(([, a]) => added.includes(a));
expect(dupes).toEqual([]);
});
it('has no malformed domain-only entries', (): void => {
const invalids = all.deny.filter((u) =>
u.includes('/') ||
u.includes('?')
);
expect(invalids).toEqual([]);
});
it('has no urls starting with www. (domain-only inclusions)', (): void => {
const invalids = all.deny.filter((u) =>
u.startsWith('www.')
);
expect(invalids).toEqual([]);
});
it('has no duplicate entries', (): void => {
const checks: string[] = [];
const dupes = all.deny.reduce<string[]>((dupes, url) => {
if (!checks.includes(url)) {
checks.push(url);
} else {
dupes.push(url);
}
return dupes;
}, []);
expect(dupes).toEqual([]);
});
});