Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 47 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,59 @@ const simpleEntropy = (str) => {
}, 0)
}

const file = process.argv[2]
if (!file) {
console.error('You need to provide a file as an argument')
process.exit(1)
}
const whitelist = fs.readFileSync(path.resolve(__dirname, './whitelist'), 'utf8').split('\n')

const fullpath = path.isAbsolute(file) ? file : path.join(process.cwd(), file)
if (path.basename(fullpath).startsWith('.env')) {
console.log('Ignoring file', fullpath)
process.exit()
const isWordASecret = (word) => {
if (whitelist.includes(word)) {
return false
}
const entropy = simpleEntropy(word)
if (entropy > 4) {
return true
}
}
try {
const source = fs.readFileSync(fullpath, 'utf8')
const lines = source.split('\n')
if (lines[0] && lines[0].includes('findsecrets-ignore-file')) {

if (require.main !== module) {
module.exports = {
isWordASecret,
}
} else {
const file = process.argv[2]
if (!file) {
console.error('You need to provide a file as an argument')
process.exit(1)
}

const fullpath = path.isAbsolute(file) ? file : path.join(process.cwd(), file)
if (path.basename(fullpath).startsWith('.env')) {
console.log('Ignoring file', fullpath)
process.exit()
}
const errors = lines.reduce((errors, line, lineNumber) => {
if (line.includes('findsecrets-ignore-line')) {
return errors
try {
const source = fs.readFileSync(fullpath, 'utf8')
const lines = source.split('\n')
if (lines[0] && lines[0].includes('findsecrets-ignore-file')) {
process.exit()
}
const words = line.split(/\W+/)
words.forEach(word => {
const entropy = simpleEntropy(word)
if (entropy > 4) {
errors.push(` at line ${lineNumber + 1} ${word.substring(0, word.length / 2 + 1)}...`)
const errors = lines.reduce((errors, line, lineNumber) => {
if (line.includes('findsecrets-ignore-line')) {
return errors
}
}, false)
return errors
}, [])
if (errors.length > 0) {
console.error('Found secrets in', fullpath)
errors.forEach(error => console.error(error))
const words = line.split(/\W+/)
words.forEach(word => {
if (isWordASecret(word)) {
errors.push(` at line ${lineNumber + 1} ${word.substring(0, word.length / 2 + 1)}...`)
}
}, false)
return errors
}, [])
if (errors.length > 0) {
console.error('Found secrets in', fullpath)
errors.forEach(error => console.error(error))
process.exit(1)
}
} catch (err) {
console.error('Error', err.stack || err.message || String(err))
process.exit(1)
}
} catch (err) {
console.error('Error', err.stack || err.message || String(err))
process.exit(1)
}
11 changes: 11 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const app = require('./')

describe('isWordASecret', () => {
it('should flag these', () => {
expect(app.isWordASecret('ZVyyCKt7i2JMtlaJgnYExjRyBlI1KOHbxiDcseWQ9at5uHFvQl')).toBe(true)
})

it('should not flag these', () => {
expect(app.isWordASecret('dangerouslySetInnerHTML')).toBe(false)
})
})
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Prevent pushing secrets to the repository",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"bin": {
"findsecrets": "./index.js"
Expand All @@ -13,5 +13,7 @@
"author": "Alberto Gimeno <[email protected]>",
"license": "MIT",
"dependencies": {},
"devDependencies": {}
"devDependencies": {
"jest": "^23.6.0"
}
}
1 change: 1 addition & 0 deletions whitelist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dangerouslySetInnerHTML