mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(ui): clearer hold reasons, aligned details and chart states
Render the hold reason as a sentence, pluralise counts, give the details grid a fixed rhythm, use distinct registry colours, label the plotted series, and show a note for a single day of history.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { humanReason, plural } from './reason'
|
||||
|
||||
describe('plural', () => {
|
||||
it.each([
|
||||
[0, '0 findings'],
|
||||
[1, '1 finding'],
|
||||
[2, '2 findings'],
|
||||
])('%i', (n, want) => expect(plural(n, 'finding')).toBe(want))
|
||||
})
|
||||
|
||||
describe('humanReason', () => {
|
||||
it('rewrites a zero threshold as "any"', () => {
|
||||
expect(humanReason('Package has 1 CRITICAL vulnerabilities (threshold: 0)')).toBe('1 critical finding. Policy holds any critical finding.')
|
||||
})
|
||||
|
||||
it('states a count threshold and pluralises both numbers', () => {
|
||||
expect(humanReason('Package has 5 HIGH vulnerabilities (threshold: 3)')).toBe('5 high findings. Policy holds more than 3 high findings.')
|
||||
expect(humanReason('Package has 2 LOW vulnerabilities (threshold: 1)')).toBe('2 low findings. Policy holds more than 1 low finding.')
|
||||
})
|
||||
|
||||
it('rewrites a severity-floor reason', () => {
|
||||
expect(humanReason('Package has HIGH or CRITICAL vulnerabilities')).toBe('Has a high or critical finding. Policy holds a finding at that level.')
|
||||
expect(humanReason('Package has MODERATE, HIGH, or CRITICAL vulnerabilities')).toBe(
|
||||
'Has a moderate, high or critical finding. Policy holds a finding at that level.',
|
||||
)
|
||||
})
|
||||
|
||||
it('passes an unknown or empty reason through', () => {
|
||||
expect(humanReason('scan failed - fail closed')).toBe('scan failed - fail closed')
|
||||
expect(humanReason(undefined)).toBe('')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,33 @@
|
||||
import { formatSeverityName } from '@/lib/severity'
|
||||
|
||||
/** "1 finding", "2 findings". */
|
||||
export function plural(count: number, noun: string): string {
|
||||
return `${count} ${noun}${count === 1 ? '' : 's'}`
|
||||
}
|
||||
|
||||
const THRESHOLD_REASON = /^Package has (\d+) (CRITICAL|HIGH|MODERATE|MEDIUM|LOW) vulnerabilities \(threshold: (-?\d+)\)$/i
|
||||
const FLOOR_REASON = /^Package has ([A-Za-z, ]+?) vulnerabilities$/
|
||||
|
||||
/** Turns the server's hold reason into a sentence a reader can act on. Unknown reasons pass through. */
|
||||
export function humanReason(reason: string | undefined): string {
|
||||
if (!reason) return ''
|
||||
const t = THRESHOLD_REASON.exec(reason)
|
||||
if (t) {
|
||||
const count = Number(t[1])
|
||||
const level = t[2].toLowerCase()
|
||||
const limit = Number(t[3])
|
||||
const rule = limit <= 0 ? `Policy holds any ${level} finding.` : `Policy holds more than ${plural(limit, `${level} finding`)}.`
|
||||
return `${plural(count, `${level} finding`)}. ${rule}`
|
||||
}
|
||||
const f = FLOOR_REASON.exec(reason)
|
||||
if (f) {
|
||||
const levels = f[1]
|
||||
.split(/,\s*(?:or\s+)?|\s+or\s+/)
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean)
|
||||
.map((s) => formatSeverityName(s).toLowerCase())
|
||||
const named = levels.length > 1 ? `${levels.slice(0, -1).join(', ')} or ${levels[levels.length - 1]}` : levels[0]
|
||||
return `Has a ${named} finding. Policy holds a finding at that level.`
|
||||
}
|
||||
return reason
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { describePolicy, scannerState, type SystemInfo } from './system'
|
||||
import { describePolicy, scannerState, scannerVersion, type SystemInfo } from './system'
|
||||
|
||||
function security(over: Partial<SystemInfo['security']>): SystemInfo['security'] {
|
||||
return {
|
||||
@@ -48,3 +48,12 @@ describe('scannerState', () => {
|
||||
expect(scannerState({ name: 'a', enabled: true, healthy: true, version: '' })).toBe('healthy')
|
||||
})
|
||||
})
|
||||
|
||||
describe('scannerVersion', () => {
|
||||
it('shows the version, API for hosted databases, and nothing for a scanner that is off', () => {
|
||||
expect(scannerVersion({ name: 'trivy', enabled: true, healthy: true, version: '0.58.1' })).toBe('v0.58.1')
|
||||
expect(scannerVersion({ name: 'osv', enabled: true, healthy: true, version: '' })).toBe('API')
|
||||
expect(scannerVersion({ name: 'grype', enabled: true, healthy: false, version: '' })).toBe('Version not reported')
|
||||
expect(scannerVersion({ name: 'npm-audit', enabled: false, healthy: null, version: '' })).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -34,6 +34,16 @@ export function scannerState(scanner: ScannerInfo): ScannerState {
|
||||
return scanner.healthy === false ? 'down' : 'healthy'
|
||||
}
|
||||
|
||||
// These scanners query a hosted database, so they have no local binary version.
|
||||
const API_SCANNERS = new Set(['osv', 'github-advisory-database'])
|
||||
|
||||
/** Version line for a scanner: empty when it is off, "API" for hosted databases. */
|
||||
export function scannerVersion(scanner: ScannerInfo): string {
|
||||
if (!scanner.enabled) return ''
|
||||
if (scanner.version) return `v${scanner.version}`
|
||||
return API_SCANNERS.has(scanner.name) ? 'API' : 'Version not reported'
|
||||
}
|
||||
|
||||
const SEVERITY_NAMES = { critical: 'CRITICAL', high: 'HIGH', moderate: 'MODERATE', low: 'LOW' } as const
|
||||
const SEVERITY_ORDER = ['critical', 'high', 'moderate', 'low'] as const
|
||||
|
||||
|
||||
@@ -37,7 +37,8 @@ describe('verdictBanner', () => {
|
||||
)
|
||||
expect(banner.type).toBe('danger')
|
||||
expect(banner.title).toBe('Held: CVE-2021-44906 is CRITICAL')
|
||||
expect(banner.body).toContain('Blocks any CRITICAL.')
|
||||
expect(banner.body).toContain('1 critical finding. Policy holds any critical finding.')
|
||||
expect(banner.body).not.toContain('threshold')
|
||||
expect(banner.body).toContain('403')
|
||||
expect(banner.body).toContain('Fixed in v1.2.6')
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { PackageReport } from '@/lib/report'
|
||||
import { formatDate, withVersionPrefix } from '@/lib/format'
|
||||
import { humanReason, plural } from '@/lib/reason'
|
||||
import { formatSeverityName, severityRank } from '@/lib/severity'
|
||||
|
||||
export interface BannerContent {
|
||||
@@ -19,7 +20,7 @@ function heldBanner(report: PackageReport, policy: string): BannerContent {
|
||||
const live = report.vulnerabilities.filter((v) => !v.bypassed)
|
||||
const top = live.reduce((max, v) => Math.max(max, severityRank(v.severity)), 0)
|
||||
const blockers = live.filter((v) => severityRank(v.severity) === top)
|
||||
const reason = report.decision.reason
|
||||
const reason = humanReason(report.decision.reason)
|
||||
const clients = 'Clients that request this version get a 403.'
|
||||
if (blockers.length === 0) {
|
||||
return { type: 'danger', title: `Held: ${reason || 'the scan did not complete'}`, body: `${policy} ${clients}`.trim() }
|
||||
@@ -30,7 +31,7 @@ function heldBanner(report: PackageReport, policy: string): BannerContent {
|
||||
return {
|
||||
type: 'danger',
|
||||
title: `Held: ${listIds(blockers.map((b) => b.id))} ${verb} ${formatSeverityName(blockers[0].severity).toUpperCase()}`,
|
||||
body: `${policy} ${reason ? `${reason}. ` : ''}${clients}${fix}`.replace(/\s+/g, ' ').trim(),
|
||||
body: `${reason || policy} ${clients}${fix}`.replace(/\s+/g, ' ').trim(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +64,7 @@ export function verdictBanner(report: PackageReport, policy: string, failClosed:
|
||||
return {
|
||||
type: 'success',
|
||||
title: 'Cleared: no finding reaches the block policy',
|
||||
body: n === 0 ? 'The scanners reported nothing. Clients are served this version.' : `${n} finding${n === 1 ? '' : 's'} below the block policy. Clients are served this version.`,
|
||||
body: n === 0 ? 'The scanners reported nothing. Clients are served this version.' : `${plural(n, 'finding')} below the block policy. Clients are served this version.`,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user