Skip to content

Commit

Permalink
Add validating add-hosts script (MetaMask#9602)
Browse files Browse the repository at this point in the history
* blacklist.js: break out implementation to add-hosts.js

* add generic add-hosts.js script; add corresponding npm scripts

* Add redundancy checks to add-hosts
  • Loading branch information
legobeat authored Dec 7, 2022
1 parent 42d05d4 commit 1591cfe
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 18 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ console.log(value)

## Contributions

To keep a tidy file, use the following CLI to add new phishing domains:
To keep a tidy file, use the following CLI to make changes to the list:

### Adding hosts to blocklist

```
yarn add:blocklist crypto-phishing-site.tld
```

### Adding hosts to allowlist

```
yarn add:blacklist crypto-phishing-site.tld
yarn add:allowlist crypto-phishing-site.tld
```
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"test": "node test | tap-summary",
"buildSample": "browserify example/index.js -o example/bundle.js",
"ci": "node test | tap-summary --no-progress",
"add:blacklist": "node src/blacklist.js"
"add:blacklist": "node src/blacklist.js",
"add:blocklist": "node src/add-hosts.js src/config.json blocklist",
"add:allowlist": "node src/add-hosts.js src/config.json allowlist"
},
"author": "kumavis",
"license": "DBAD",
Expand Down
95 changes: 95 additions & 0 deletions src/add-hosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env node
const fs = require('fs');
const config = require('./config.json');
const PhishingDetector = require('./detector')

const SECTION_KEYS = {
blocklist: 'blacklist',
fuzzylist: 'fuzzylist',
allowlist: 'allowlist',
};

const addHosts = (section, domains, dest) => {
const cfg = {
...config,
[section]: config[section].concat(domains),
};

const output = JSON.stringify(cfg, null, 2) + '\n';

fs.writeFile(dest, output, (err) => {
if (err) {
return console.log(err);
}
});
}

const validateHostRedundancy = (detector, section, h) => {
switch (section) {
case 'blocklist': {
const r = detector.check(h);
if (r.result) {
throw new Error(`'${h}' already covered by '${r.match}' in '${r.type}'.`);
}
return true;
}
case 'allowlist': {
const r = detector.check(h);
if (!r.result) {
console.error(`'${h}' does not require allowlisting`);
return false;
}
return true;
}
case 'fuzzylist': {
if (config.fuzzylist.includes(h)) {
console.error(`'${h}' already in fuzzylist`);
return false;
}
return true;
}
default:
throw new Error(`unrecognized section '${section}'`);
}
}

module.exports = {
addHosts,
validateHostRedundancy,
};

/////////////////////
//////// MAIN ///////
/////////////////////

const exitWithUsage = (exitCode) => {
console.error(`Usage: ${
process.argv.slice(0,2).join(' ')
} src/config.json ${
Object.keys(SECTION_KEYS).join('|')
} hostname...`);
process.exit(exitCode);
};

if (require.main === module) {
if (process.argv.length < 4) {
exitWithUsage(1);
}

const [destFile, section, ...hosts] = process.argv.slice(2);

if (!Object.keys(SECTION_KEYS).includes(section) || hosts.length < 1) {
exitWithUsage(1);
}

const detector = new PhishingDetector(config);

try {
hosts.filter(h => validateHostRedundancy(detector, section, h));
} catch (err) {
console.error(err);
process.exit(1);
}

addHosts(SECTION_KEYS[section], hosts, destFile);
}
22 changes: 7 additions & 15 deletions src/blacklist.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
const fs = require("fs");
const config = require("./config");
#!/usr/bin/env node
const { spawnSync } = require('child_process');

const domains = process.argv.slice(2);
domains.forEach(domain => {
config.blacklist.unshift(domain);
});
console.warn(`*WARNING* blacklist.js/add:blacklist are deprecated and will be removed in a future version.
Use add-hosts.js/add:blocklist instead`);

// Nicely pad the file
let output = JSON.stringify(config, null, 2);
output += "\n";

fs.writeFile("./src/config.json", output, (err) => {
if (err) {
return console.log(err);
}
});
const r = spawnSync('./src/add-hosts.js', ['./src/config.json', 'blocklist', ...process.argv.slice(2)]);
process.stderr.write(r.stderr.toString('utf-8'));
process.stdout.write(r.stdout.toString('utf-8'));

0 comments on commit 1591cfe

Please sign in to comment.