Add a homoglyph detector for gradle-wrapper.jar files

This commit is contained in:
Jonathan Leitschuh 2020-01-13 12:39:40 -05:00
parent c230e9d098
commit ae0da6528c
No known key found for this signature in database
GPG key ID: 3501A7427721B061
8 changed files with 4102 additions and 8 deletions

View file

@ -4,7 +4,8 @@ import * as find from '../src/find'
test('finds test data wrapper jars', async () => {
const repoRoot = path.resolve('.')
const wrapperJars = await find.findWrapperJars(repoRoot)
expect(wrapperJars.length).toBe(2)
expect(wrapperJars.length).toBe(3)
expect(wrapperJars).toContain('__tests__/data/valid/gradle-wrapper.jar')
expect(wrapperJars).toContain('__tests__/data/invalid/gradle-wrapper.jar')
expect(wrapperJars).toContain('__tests__/data/invalid/gradlе-wrapper.jar') // homoglyph
})

View file

@ -4,11 +4,11 @@ import * as validate from '../src/validate'
test('validates wrapper jars', async () => {
const invalidWrapperJars = await validate.findInvalidWrapperJars(
path.resolve('.'),
2,
3,
false,
[]
)
expect(invalidWrapperJars.length).toBe(1)
expect(invalidWrapperJars.length).toBe(2)
expect(invalidWrapperJars[0]).toEqual(
new validate.InvalidWrapperJar(
'__tests__/data/invalid/gradle-wrapper.jar',
@ -19,8 +19,8 @@ test('validates wrapper jars', async () => {
test('fails if not enough wrapper jars are found', async () => {
await expect(
validate.findInvalidWrapperJars(path.resolve('.'), 3, false, [])
validate.findInvalidWrapperJars(path.resolve('.'), 4, false, [])
).rejects.toThrowError(
'Expected to find at least 3 Gradle Wrapper JARs but got only 2'
'Expected to find at least 4 Gradle Wrapper JARs but got only 3'
)
})

2038
dist/index.js vendored

File diff suppressed because it is too large Load diff

View file

@ -7,5 +7,6 @@ module.exports = {
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
verbose: true,
setupFilesAfterEnv: ['./jest.setup.js']
}

1
jest.setup.js Normal file
View file

@ -0,0 +1 @@
jest.setTimeout(10000) // in milliseconds

View file

@ -2,12 +2,17 @@ import * as util from 'util'
import * as path from 'path'
import * as fs from 'fs'
import * as homoglyphs from './homoglyphs'
const readdir = util.promisify(fs.readdir)
export async function findWrapperJars(baseDir: string): Promise<string[]> {
const targetWords = ['gradle-wrapper.jar']
const files = await recursivelyListFiles(baseDir)
return files
.filter(file => file.endsWith('gradle-wrapper.jar'))
.filter(
file => homoglyphs.search(path.basename(file), targetWords).length > 0
)
.map(wrapperJar => path.relative(baseDir, wrapperJar))
}

2050
src/homoglyphs.ts Normal file

File diff suppressed because it is too large Load diff