-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce new typosquatting marshall (#297)
Signed-off-by: Liran Tal <liran.tal@gmail.com>
- Loading branch information
Showing
3 changed files
with
72 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
const BaseMarshall = require('./baseMarshall') | ||
const { marshallCategories } = require('./constants') | ||
|
||
const path = require('path') | ||
const levenshtein = require('fast-levenshtein') | ||
const topPackagesRawJSON = require(path.join(__dirname, '../../data/top-packages.json')) | ||
|
||
const MARSHALL_NAME = 'typosquatting' | ||
|
||
class Marshall extends BaseMarshall { | ||
constructor(options) { | ||
super(options) | ||
this.name = MARSHALL_NAME | ||
this.categoryId = marshallCategories.PackageHealth.id | ||
} | ||
|
||
title() { | ||
return 'Checking for typosquatting' | ||
} | ||
|
||
validate(pkg) { | ||
let levenshteinDistance = null | ||
let similarPackages = [] | ||
return new Promise((resolve, reject) => { | ||
for (const popularPackageNameInRepository of topPackagesRawJSON) { | ||
levenshteinDistance = levenshtein.get(pkg.packageName, popularPackageNameInRepository) | ||
|
||
if (levenshteinDistance < 3) { | ||
similarPackages.push(popularPackageNameInRepository) | ||
} | ||
} | ||
|
||
if (similarPackages.length > 0) { | ||
return reject( | ||
new Error( | ||
`Package name could be a typosquatting attempt for popular package(s): ${similarPackages.join( | ||
', ' | ||
)}` | ||
) | ||
) | ||
} | ||
|
||
return resolve([]) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = Marshall |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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