Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HTML report #130

Closed
wants to merge 3 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
13 changes: 7 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
jsonFormat = "json"
yamlFormat = "yaml"
sarifFormat = "sarif"
htmlFormat = "html"
configFileFlag = "config"

logLevelFlagName = "log-level"
Expand Down Expand Up @@ -114,8 +115,8 @@ func Execute() {
rootCmd.PersistentFlags().StringVar(&configFilePath, configFileFlag, "", "config file path")
cobra.CheckErr(rootCmd.MarkPersistentFlagFilename(configFileFlag, "yaml", "yml", "json"))
rootCmd.PersistentFlags().StringVar(&logLevelVar, logLevelFlagName, "info", "log level (trace, debug, info, warn, error, fatal)")
rootCmd.PersistentFlags().StringSliceVar(&reportPathVar, reportPathFlagName, []string{}, "path to generate report files. The output format will be determined by the file extension (.json, .yaml, .sarif)")
rootCmd.PersistentFlags().StringVar(&stdoutFormatVar, stdoutFormatFlagName, "yaml", "stdout output format, available formats are: json, yaml, sarif")
rootCmd.PersistentFlags().StringSliceVar(&reportPathVar, reportPathFlagName, []string{}, "path to generate report files. The output format will be determined by the file extension (.json, .yaml, .sarif, .html)")
rootCmd.PersistentFlags().StringVar(&stdoutFormatVar, stdoutFormatFlagName, "yaml", "stdout output format, available formats are: json, yaml, sarif, html")
rootCmd.PersistentFlags().StringArrayVar(&customRegexRuleVar, customRegexRuleFlagName, []string{}, "custom regexes to apply to the scan, must be valid Go regex")

rootCmd.PersistentFlags().StringSliceVar(&includeRuleVar, includeRuleFlagName, []string{}, "include rules by name or tag to apply to the scan (adds to list, starts from empty)")
Expand Down Expand Up @@ -144,15 +145,15 @@ func Execute() {
}

func validateFormat(stdout string, reportPath []string) {
if !(strings.EqualFold(stdout, yamlFormat) || strings.EqualFold(stdout, jsonFormat) || strings.EqualFold(stdout, sarifFormat)) {
log.Fatal().Msgf(`invalid output format: %s, available formats are: json, yaml and sarif`, stdout)
if !(strings.EqualFold(stdout, yamlFormat) || strings.EqualFold(stdout, jsonFormat) || strings.EqualFold(stdout, sarifFormat) || strings.EqualFold(stdout, htmlFormat)) {
log.Fatal().Msgf(`invalid output format: %s, available formats are: json, yaml, sarif, html`, stdout)
}
for _, path := range reportPath {

fileExtension := filepath.Ext(path)
format := strings.TrimPrefix(fileExtension, ".")
if !(strings.EqualFold(format, yamlFormat) || strings.EqualFold(format, jsonFormat) || strings.EqualFold(format, sarifFormat)) {
log.Fatal().Msgf(`invalid report extension: %s, available extensions are: json, yaml and sarif`, format)
if !(strings.EqualFold(format, yamlFormat) || strings.EqualFold(format, jsonFormat) || strings.EqualFold(format, sarifFormat) || strings.EqualFold(format, htmlFormat)) {
log.Fatal().Msgf(`invalid report extension: %s, available extensions are: json, yaml, sarif, html`, format)
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions reporting/html.go
Copy link
Contributor

Choose a reason for hiding this comment

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

image

We need to break the line (overflow-wrap: anywhere)
Also, I think putting it inside a code block will help.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package reporting

import (
"bytes"
_ "embed"
"html/template"
"log"
)

var (
//go:embed html/report.tmpl
htmlTemplate string
//go:embed html/report.css
cssTemplate string
//go:embed html/github.svg
githubSVG string
//go:embed html/checkmarx_logo.html
checkmarxLogo string
)

func writeHtml(report Report) string {
tmpl := template.Must(template.New("report").Funcs(getFuncMap()).Parse(htmlTemplate))
Copy link
Contributor

Choose a reason for hiding this comment

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

It is new for me.

When the template.New("report").Funcs(funcMap).Parse(htmlTemplate) returns (*template.Template, error), the template.Must that accept *template.Template, error will get the multi-var return value?
Good to know!

var buffer bytes.Buffer
err := tmpl.Execute(&buffer, report)
if err != nil {
log.Fatalf("failed to create HTML report with error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think maybe use log.Errorf, because Fatalf is calling os.Exit, and maybe there are more outputs formats that still can be written.

}
return buffer.String()
}

func getFuncMap() template.FuncMap {
return template.FuncMap{
"includeCSS": func() template.CSS { return template.CSS(cssTemplate) },
"includeSVG": func() template.HTML { return template.HTML(githubSVG) },
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"includeSVG": func() template.HTML { return template.HTML(githubSVG) },
"includeGithubSVG": func() template.HTML { return template.HTML(githubSVG) },

Check failure

Code scanning / gosec

The used method does not auto-escape HTML. This can potentially lead to 'Cross-site Scripting' vulnerabilities, in case the attacker controls the input.

The used method does not auto-escape HTML. This can potentially lead to 'Cross-site Scripting' vulnerabilities, in case the attacker controls the input.
"includeLogo": func() template.HTML { return template.HTML(checkmarxLogo) },

Check failure

Code scanning / gosec

The used method does not auto-escape HTML. This can potentially lead to 'Cross-site Scripting' vulnerabilities, in case the attacker controls the input.

The used method does not auto-escape HTML. This can potentially lead to 'Cross-site Scripting' vulnerabilities, in case the attacker controls the input.
}
}
Comment on lines +31 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

It can be declared as var instead of function:

Suggested change
func getFuncMap() template.FuncMap {
return template.FuncMap{
"includeCSS": func() template.CSS { return template.CSS(cssTemplate) },
"includeSVG": func() template.HTML { return template.HTML(githubSVG) },
"includeLogo": func() template.HTML { return template.HTML(checkmarxLogo) },
}
}
var funcMap = template.FuncMap{
"includeCSS": func() template.CSS { return template.CSS(cssTemplate) },
"includeSVG": func() template.HTML { return template.HTML(githubSVG) },
"includeLogo": func() template.HTML { return template.HTML(checkmarxLogo) },
}

1 change: 1 addition & 0 deletions reporting/html/checkmarx_logo.html

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions reporting/html/github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
171 changes: 171 additions & 0 deletions reporting/html/report.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}

body {
font-family: sans-serif;
}

.container {
display: flex;
align-items: center;
flex-direction: column;
margin: 5px;
border: 1px solid #bebebe;
}

.run-info {
display: flex;
flex-wrap: wrap;
border: 1px solid #bebebe;
margin-top: 10px;
width: 50vw;
}

.run-info > span {
flex-basis: 50%;
text-align: center;
}

.counters {
display: flex;
flex-direction: row;
margin: 22px 0;
}

.report-header-footer {
display: flex;
flex-direction: row;
justify-content: space-between;
border-bottom: 1px solid #bebebe;
width: 100%;
padding: 15px 21px;
background-color: #503e9e;
height: 50px;
font-weight: bold;
font-size: 14px;
color: #fff;
cursor: default;
user-select: none;
}

.report-header-footer > a {
color: inherit;
text-decoration: inherit;
}

.report-header-footer > .title {
font-size: 18px;
}

.report-header-footer > .title > span {
color: #000;
}

.report-header-footer > .timestamp {
font-weight: normal;
font-style: italic;
opacity: 0.5;
}

.badge {
color: #fff;
border: 2px solid #e8e8e8;
border-radius: 50%;
cursor: default;
user-select: none;
padding: 3px;
font-size: 10px;
display: flex;
align-items: center;
justify-content: center;
width: 30px;
height: 30px;
position: absolute;
left: 60%;
top: 50%;
}

.separator {
border-top: 1px solid #979797;
opacity: 0.5;
width: 95%;
margin: 22px 0;
}

.secret-info {
border: 1px #969696 solid;
border-radius: 2px;
display: flex;
flex-direction: column;
margin: 6px 9px;
}

.secret-info-header {
display: flex;
flex-direction: row;
justify-content: space-between;
margin: 6px 9px;
}

.secret-info-details {
display: flex;
flex-direction: column;
margin: 6px 9px;
}

.secret-info-details > span > strong {
width: 5vw;
}

.social-networks {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
margin-bottom: 24px;
}

.social-networks > a {
margin: 0 15px;
}

.social-networks > a > div > svg {
width: 20px;
height: 20px;
}

.footer-text {
font-style: italic;
opacity: 0.5;
font-weight: normal;
width: 100%;
display: flex;
align-self: center;
justify-content: center;
}

a.checkmarx,
a.checkmarx:visited,
a.checkmarx:hover,
a.checkmarx:active {
cursor: pointer;
font-weight: bold;
text-decoration: underline;
color: #fff;
opacity: 0.8;
}

.hide {
display: none;
}

summary {
cursor: pointer;
user-select: none;
font-size: 18px;
font-weight: bold;
}
63 changes: 63 additions & 0 deletions reporting/html/report.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>2MS Scan Result</title>
</head>
<style>
{{ includeCSS }}
</style>
<body>
{{ includeLogo }}
<div class="container">
<div class="report-header-footer">
<span class="title">
2MS
<span>REPORT</span>
</span>
</div>

<div class="run-info">
<span style="flex-basis:100%" id="scan-paths"><strong>Total items scanned:</strong> {{ .TotalItemsScanned }}</span>
<span style="flex-basis:100%" id="scan-paths"><strong>Total serets found:</strong> {{ .TotalSecretsFound }}</span>
</div>
<hr class="separator">

{{ range $file, $secrets := .Results }}

<div class="secret-info">
<h3>{{ $file }}</h3>

<ul>
{{ range $index, $secret := $secrets }}

<div class="secret-info-details">
<span><strong>ID:</strong> {{ .ID }}</span>
<span><strong>Source:</strong> {{ .Source }}</span>
Copy link
Contributor

Choose a reason for hiding this comment

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

Most of the times, the Source will be a link, but not for Git, for example.

Do you think we can make it a link when it is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

looking into it

<span><strong>Description:</strong> {{ .Description }}</span>
<span><strong>Start Line:</strong> {{ .StartLine }}</span>
<span><strong>End Line:</strong> {{ .EndLine }}</span>
<span><strong>Start Column:</strong> {{ .StartColumn }}</span>
<span><strong>End Column:</strong> {{ .EndColumn }}</span>
<span><strong>Value:</strong> {{ .Value }}</span>
</div>

{{ end }}
</ul>
</div>

{{ end }}
<hr class="separator">
<div class="social-networks">
<a href="https://github.com/Checkmarx/2ms/" rel="noopener" target="_blank">
<div>{{ includeSVG }}</div>
</a>
</div>
<div class="report-header-footer">
<span class="footer-text">The 2MS project is powered by&nbsp;<a href="https://www.checkmarx.com/" class="checkmarx" rel="noopener" target="_blank">Checkmarx</a>, global leader of Application Security Testing</span>
</div>
</div>
</body>
</html>

3 changes: 3 additions & 0 deletions reporting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
jsonFormat = "json"
yamlFormat = "yaml"
sarifFormat = "sarif"
htmlFormat = "html"
)

type Report struct {
Expand Down Expand Up @@ -72,6 +73,8 @@ func (r *Report) getOutput(format string, cfg *config.Config) string {
output = writeYaml(*r)
case sarifFormat:
output = writeSarif(*r, cfg)
case htmlFormat:
output = writeHtml(*r)
}
return output
}