forked from MetaMask/eth-phishing-detect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validating add-hosts script (MetaMask#9602)
* 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
Showing
4 changed files
with
115 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |