Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Add report functionality for detect-secrets + tests #235

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 32 additions & 0 deletions linters/detect_secrets/detect_secrets_annotations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2019 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

const pathLib = require('path')

const { getAnnotationLevel } = require('../../annotations_levels')

/**
* @param {*} issue an issue from which the annotation will be build
* @param {String} filePath file path where the issue is located
* @return {Object} returns an annotation object as specified here:
* https://developer.github.com/v3/checks/runs/#annotations-object
*/
function getAnnotation (issue, filePath) {
const path = pathLib.normalize(filePath)
const startLine = issue.line_number
const endLine = startLine
const annotationLevel = getAnnotationLevel('HIGH', 'LOW')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the severity always be high and confidence always low for these results?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's something we should discuss in our issue tracker
#209
I just put the severity and confidence Bandit uses when reporting hardcoded credentials.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to look at https://github.com/lyft/bandit-high-entropy-string. This is a bandit plugin to do detect-secrets like testing. As part of the plugin, it defines severity and confidence.

const title = issue.type
const message = `The issue was found by the ${issue.type} plugin in detect-secrets.`

return {
path,
start_line: startLine,
end_line: endLine,
annotation_level: annotationLevel,
title,
message
}
}

module.exports.getAnnotation = getAnnotation
29 changes: 29 additions & 0 deletions linters/detect_secrets/detect_secrets_report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

const { getAnnotation } = require('./detect_secrets_annotations')
const { countIssueLevels } = require('../../annotations_levels')

/**
* Process detect-secrets output (generate annotations, count issue levels)
* @param {*} results detect-secrets json output
* @returns {{{Object[]}, Object, String}} Object[] array of annotation objects
* see https://developer.github.com/v3/checks/runs/#annotations-object
* Object contains the number of errors, warnings and notices found
* String consists of link|links to the documentation for the issues
*/
MVrachev marked this conversation as resolved.
Show resolved Hide resolved
module.exports = (results) => {
const filesWithIssues = Object.keys(results)
let annotations = []
for (let file of filesWithIssues) {
for (let issue of results[file]) {
annotations.push(getAnnotation(issue, file))
}
}
const issueCount = countIssueLevels(annotations)
let moreInfo = ''
if (annotations.length > 0) {
moreInfo = '[detect-secrets documentation](https://github.com/Yelp/detect-secrets#currently-supported-plugins)'
}
return { annotations, issueCount, moreInfo }
}
50 changes: 50 additions & 0 deletions test/detect_secrets.report.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

const generateReport = require('../linters/detect_secrets/detect_secrets_report')

const detectSecretsResults = require('./fixtures/reports/detect_secrets.vulnerable')

describe('TSLint report generation', () => {
let report = ''

beforeEach(() => {
report = generateReport(detectSecretsResults.results)
})

test('Creates correct report structure', () => {
expect(report).toHaveProperty('annotations')
expect(report).toHaveProperty('issueCount.errors')
expect(report).toHaveProperty('issueCount.warnings')
expect(report).toHaveProperty('issueCount.notices')
expect(report).toHaveProperty('moreInfo')
})

test('Creates correct annotations', () => {
expect(report.annotations).toHaveLength(4)
expect(report.annotations[0].end_line).toBe(13)
expect(report.annotations[1].start_line).toBe(68)
expect(report.annotations[2].title).toEqual('Basic Auth Credentials')
expect(report.annotations[3].path).toBe('test.yaml')

report.annotations.forEach(annotation => {
expect(annotation.annotation_level).toMatch(/notice|warning|failure/)
expect(annotation.message).not.toBe('')
})
})

test('Creates correct issue count', () => {
expect(report.issueCount.errors).toBe(4)
expect(report.issueCount.warnings).toBe(0)
expect(report.issueCount.notices).toBe(0)
})

test('Handles no vulnerable reports', () => {
const report = generateReport([], '')

expect(report.annotations).toHaveLength(0)
expect(report.issueCount.errors).toBe(0)
expect(report.issueCount.warnings).toBe(0)
expect(report.issueCount.notices).toBe(0)
})
})
59 changes: 59 additions & 0 deletions test/fixtures/reports/detect_secrets.vulnerable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"exclude": {
"files": null,
"lines": null
},
"generated_at": "2019-05-15T17:24:04Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
},
{
"base64_limit": 4.5,
"name": "Base64HighEntropyString"
},
{
"name": "BasicAuthDetector"
},
{
"hex_limit": 3,
"name": "HexHighEntropyString"
},
{
"name": "KeywordDetector"
},
{
"name": "PrivateKeyDetector"
},
{
"name": "SlackDetector"
}
],
"results": {
"test.js": [
{
"hashed_secret": "edbd2994e5980ce68a498791abc1fb9c40f6bb43",
"line_number": 13,
"type": "Secret Keyword"
},
{
"hashed_secret": "daefe0b4345a654580dcad25c7c11ff4c944a8c0",
"line_number": 68,
"type": "Private Key"
},
{
"hashed_secret": "99b5e14eaf6b7cd863796dab48ae736be2ac6b53",
"line_number": 77,
"type": "Basic Auth Credentials"
}
],
"test.yaml": [
{
"hashed_secret": "66e02bb499b2a3f5f2894ce1b7959962c1a5a245",
"line_number": 5,
"type": "Base64 High Entropy String"
}
]
},
"version": "0.12.2"
}