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

set stronger fakejwt and fix wrong advice message when no high risk #119

Merged
merged 2 commits into from
May 21, 2024
Merged
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
9 changes: 3 additions & 6 deletions cmd/scan/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func (a SortByPathAndSeverity) Less(i, j int) bool {
}

func NewScanVulnerabilityReports(report *report.ScanReport) []*ScanVulnerabilityReport {
vulns := make([]*ScanVulnerabilityReport, 0, len(report.GetVulnerabilityReports()))
for _, vr := range report.GetVulnerabilityReports() {
reports := report.GetFailedVulnerabilityReports()
vulns := make([]*ScanVulnerabilityReport, 0, len(reports))
for _, vr := range reports {
vulns = append(vulns, &ScanVulnerabilityReport{
OperationMethod: report.Operation.Method,
OperationPath: report.Operation.Path,
Expand Down Expand Up @@ -132,10 +133,6 @@ func DisplayReportTable(reporter *report.Reporter) {

vulnerabilityReports := NewFullScanVulnerabilityReports(reporter.GetReports())
for _, vulnReport := range vulnerabilityReports {
if vulnReport.Vuln.HasBeenSkipped() || vulnReport.Vuln.HasPassed() {
continue
}

row := []string{
fmt.Sprintf("%s %s", vulnReport.OperationMethod, vulnReport.OperationPath),
vulnReport.Vuln.SeverityLevelString(),
Expand Down
24 changes: 24 additions & 0 deletions cmd/scan/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ func TestNewScanVulnerabilityReports(t *testing.T) {
{
Issue: report.Issue{
Name: "Vuln1",
CVSS: report.CVSS{
Score: 5.0,
},
},
Status: report.VulnerabilityReportStatusFail,
},
{
Issue: report.Issue{
Name: "Vuln2",
CVSS: report.CVSS{
Score: 5.0,
},
},
Status: report.VulnerabilityReportStatusFail,
},
},

Expand All @@ -46,12 +54,20 @@ func TestNewFullScanVulnerabilityReports(t *testing.T) {
{
Issue: report.Issue{
Name: "Vuln1",
CVSS: report.CVSS{
Score: 5.0,
},
},
Status: report.VulnerabilityReportStatusFail,
},
{
Issue: report.Issue{
Name: "Vuln2",
CVSS: report.CVSS{
Score: 5.0,
},
},
Status: report.VulnerabilityReportStatusFail,
},
},

Expand All @@ -62,12 +78,20 @@ func TestNewFullScanVulnerabilityReports(t *testing.T) {
{
Issue: report.Issue{
Name: "Vuln3",
CVSS: report.CVSS{
Score: 5.0,
},
},
Status: report.VulnerabilityReportStatusFail,
},
{
Issue: report.Issue{
Name: "Vuln4",
CVSS: report.CVSS{
Score: 5.0,
},
},
Status: report.VulnerabilityReportStatusFail,
},
},

Expand Down
2 changes: 1 addition & 1 deletion jwt/const.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package jwt

const FakeJWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
const FakeJWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.ufhxDTmrs4T5MSsvT6lsb3OpdWi5q8O31VX7TgrVamA"
11 changes: 10 additions & 1 deletion report/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ func (rr *Reporter) GetVulnerabilityReports() []*VulnerabilityReport {
return vrs
}

func (rr *Reporter) GetFailedVulnerabilityReports() []*VulnerabilityReport {
var vrs []*VulnerabilityReport
for _, r := range rr.GetReports() {
vrs = append(vrs, r.GetFailedVulnerabilityReports()...)
}

return vrs
}

func (rr *Reporter) HasHighRiskOrHigherSeverityVulnerability() bool {
for _, r := range rr.GetVulnerabilityReports() {
for _, r := range rr.GetFailedVulnerabilityReports() {
if r.IsHighRiskSeverity() || r.IsCriticalRiskSeverity() {
return true
}
Expand Down
94 changes: 94 additions & 0 deletions report/reporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package report_test

import (
"net/http"
"testing"

"github.com/cerberauth/vulnapi/internal/request"
"github.com/cerberauth/vulnapi/report"
"github.com/stretchr/testify/assert"
)

func TestReporter_NoHasHighRiskOrHigherSeverityVulnerability_WhenNoReport(t *testing.T) {
reporter := report.NewReporter()
assert.False(t, reporter.HasHighRiskOrHigherSeverityVulnerability())
}

func TestReporter_NoHasVulnerability_WhenNoFailedReport(t *testing.T) {
reporter := report.NewReporter()
operation, _ := request.NewOperation(request.DefaultClient, http.MethodPost, "http://localhost:8080/", nil, nil, nil)
sr := report.NewScanReport("id", "test", operation)
issue := report.Issue{
Name: "test",
}
vulnerabilityReport := report.NewVulnerabilityReport(issue).Pass()
sr.AddVulnerabilityReport(vulnerabilityReport)
reporter.AddReport(sr)

assert.False(t, reporter.HasVulnerability())
}

func TestReporter_HasVulnerability_WhenFailedReport(t *testing.T) {
reporter := report.NewReporter()
operation, _ := request.NewOperation(request.DefaultClient, http.MethodPost, "http://localhost:8080/", nil, nil, nil)
sr := report.NewScanReport("id", "test", operation)
issue := report.Issue{
Name: "test",
}
vulnerabilityReport := report.NewVulnerabilityReport(issue).Fail()
sr.AddVulnerabilityReport(vulnerabilityReport)
reporter.AddReport(sr)

assert.True(t, reporter.HasVulnerability())
}

func TestReporters_HasHighRiskOrHigherSeverityVulnerability_WhenLowRiskReport(t *testing.T) {
reporter := report.NewReporter()
operation, _ := request.NewOperation(request.DefaultClient, http.MethodPost, "http://localhost:8080/", nil, nil, nil)
sr := report.NewScanReport("id", "test", operation)
issue := report.Issue{
Name: "test",
CVSS: report.CVSS{
Score: 0.1,
},
}
vulnerabilityReport := report.NewVulnerabilityReport(issue).Fail()
sr.AddVulnerabilityReport(vulnerabilityReport)
reporter.AddReport(sr)

assert.False(t, reporter.HasHighRiskOrHigherSeverityVulnerability())
}

func TestReporters_HasHighRiskOrHigherSeverityVulnerability_WhenHighRiskReport(t *testing.T) {
reporter := report.NewReporter()
operation, _ := request.NewOperation(request.DefaultClient, http.MethodPost, "http://localhost:8080/", nil, nil, nil)
sr := report.NewScanReport("id", "test", operation)
issue := report.Issue{
Name: "test",
CVSS: report.CVSS{
Score: 8,
},
}
vulnerabilityReport := report.NewVulnerabilityReport(issue).Fail()
sr.AddVulnerabilityReport(vulnerabilityReport)
reporter.AddReport(sr)

assert.True(t, reporter.HasHighRiskOrHigherSeverityVulnerability())
}

func TestReporters_HasHighRiskOrHigherSeverityVulnerability_WhenCriticalRiskReport(t *testing.T) {
reporter := report.NewReporter()
operation, _ := request.NewOperation(request.DefaultClient, http.MethodPost, "http://localhost:8080/", nil, nil, nil)
sr := report.NewScanReport("id", "test", operation)
issue := report.Issue{
Name: "test",
CVSS: report.CVSS{
Score: 9.8,
},
}
vulnerabilityReport := report.NewVulnerabilityReport(issue).Fail()
sr.AddVulnerabilityReport(vulnerabilityReport)
reporter.AddReport(sr)

assert.True(t, reporter.HasHighRiskOrHigherSeverityVulnerability())
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ func ScanHandler(operation *request.Operation, securityScheme auth.SecuritySchem

r := report.NewScanReport(AcceptsUnauthenticatedOperationScanID, AcceptsUnauthenticatedOperationScanName, operation)
if _, ok := securityScheme.(*auth.NoAuthSecurityScheme); ok {
vulnReport.Skip()
r.AddVulnerabilityReport(vulnReport).End()
return r, nil
return r.AddVulnerabilityReport(vulnReport.Skip()).End(), nil
}

noAuthSecurityScheme := auth.SecurityScheme(auth.NewNoAuthSecurityScheme())
vsa, err := scan.ScanURL(operation, &noAuthSecurityScheme)
if err != nil {
return r, err
}
r.AddScanAttempt(vsa).End()

vulnReport.WithBooleanStatus(scan.IsUnauthorizedStatusCodeOrSimilar(vsa.Response))
r.AddVulnerabilityReport(vulnReport)
r.AddVulnerabilityReport(vulnReport).AddScanAttempt(vsa).End()

return r, nil
}
8 changes: 2 additions & 6 deletions scan/broken_authentication/jwt/blank_secret/blank_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ func ScanHandler(operation *request.Operation, securityScheme auth.SecuritySchem
r := report.NewScanReport(BlankSecretVulnerabilityScanID, BlankSecretVulnerabilityScanName, operation)

if !ShouldBeScanned(securityScheme) {
vulnReport.Skip()
r.AddVulnerabilityReport(vulnReport).End()
return r, nil
return r.AddVulnerabilityReport(vulnReport.Skip()).End(), nil
}

var valueWriter *jwt.JWTWriter
Expand All @@ -68,10 +66,8 @@ func ScanHandler(operation *request.Operation, securityScheme auth.SecuritySchem
if err != nil {
return r, err
}
r.AddScanAttempt(vsa).End()

vulnReport.WithBooleanStatus(scan.IsUnauthorizedStatusCodeOrSimilar(vsa.Response))
r.AddVulnerabilityReport(vulnReport)
r.AddScanAttempt(vsa).AddVulnerabilityReport(vulnReport).End()

return r, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ func TestBlankSecretScanHandler_Passed_WhenNoJWTAndUnauthorizedResponse(t *testi
assert.True(t, report.Vulns[0].HasPassed())
}

func TestBlankSecretScanHandler_Passed_WhenNoJWTAndOKResponse(t *testing.T) {
client := request.DefaultClient
httpmock.ActivateNonDefault(client.Client)
defer httpmock.DeactivateAndReset()

securityScheme, _ := auth.NewAuthorizationJWTBearerSecurityScheme("token", nil)
operation, _ := request.NewOperation(client, http.MethodGet, "http://localhost:8080/", nil, nil, nil)
httpmock.RegisterResponder(operation.Method, operation.Request.URL.String(), httpmock.NewBytesResponder(http.StatusOK, nil))

report, err := blanksecret.ScanHandler(operation, securityScheme)

require.NoError(t, err)
assert.Equal(t, 1, httpmock.GetTotalCallCount())
assert.True(t, report.Vulns[0].HasFailed())
}

func TestBlankSecretScanHandler_Passed_WhenUnauthorizedResponse(t *testing.T) {
client := request.DefaultClient
httpmock.ActivateNonDefault(client.Client)
Expand Down
Loading