From b44ed7cc7f27e87befb4615f7acb9c56a4f22e4c Mon Sep 17 00:00:00 2001 From: Fred Tingaud Date: Thu, 20 Feb 2025 12:04:31 +0100 Subject: [PATCH] Fix tests and severity display --- .../__snapshots__/RulePage.test.tsx.snap | 204 ++++++++++++++---- .../metadata/S1000/cfamily-metadata.json | 7 + .../metadata/S1000/default-metadata.json | 7 + .../metadata/S3457/cfamily-metadata.json | 7 + .../metadata/S3457/csharp-metadata.json | 7 + .../metadata/S3457/default-metadata.json | 7 + .../metadata/S3457/java-metadata.json | 10 +- .../metadata/S3457/python-metadata.json | 7 + .../metadata/S3649/default-metadata.json | 7 + .../metadata/S3649/java-metadata.json | 7 + .../metadata/S987/cfamily-metadata.json | 7 + .../metadata/S987/default-metadata.json | 7 + .../rules/S1000/cfamily/metadata.json | 6 + .../resources/rules/S3457/metadata.json | 6 + .../resources/rules/S3649/metadata.json | 6 + .../rules/S987/cfamily/metadata.json | 6 + .../deployment/__tests__/searchIndex.test.ts | 2 +- frontend/src/deployment/metadata.ts | 4 +- frontend/src/deployment/searchIndex.ts | 10 +- frontend/src/types/IndexStore.ts | 2 +- frontend/src/types/Severities.ts | 6 + 21 files changed, 283 insertions(+), 49 deletions(-) diff --git a/frontend/src/__tests__/__snapshots__/RulePage.test.tsx.snap b/frontend/src/__tests__/__snapshots__/RulePage.test.tsx.snap index fbf9efc5c1..23bda429f7 100644 --- a/frontend/src/__tests__/__snapshots__/RulePage.test.tsx.snap +++ b/frontend/src/__tests__/__snapshots__/RulePage.test.tsx.snap @@ -847,6 +847,61 @@ var res = string.Format("{0} {1}", array); // Compliant we don't know the size o , + + "code" + + : { + + + "impacts" + + : { + + + "MAINTAINABILITY" + + : + + "MEDIUM" + + + }, + + + "attribute" + + : + + "CLEAR" + + + }, + + + "severity" + + : + + "MEDIUM" + + , + @@ -939,19 +994,6 @@ var res = string.Format("{0} {1}", array); // Compliant we don't know the size o , - - "defaultSeverity" - - : - - "Major" - - , - @@ -1887,6 +1929,48 @@ exports[`renders cfamily version of S1000 1`] = ` , + + "code" + + : { + + + "impacts" + + : { + + + "MAINTAINABILITY" + + : + + "MEDIUM" + + + }, + + + "attribute" + + : + + "CLEAR" + + + }, + @@ -1987,19 +2071,6 @@ exports[`renders cfamily version of S1000 1`] = ` : [] }, - - "defaultSeverity" - - : - - "Major" - - , - @@ -2026,6 +2097,19 @@ exports[`renders cfamily version of S1000 1`] = ` , + + "severity" + + : + + "MEDIUM" + + , + @@ -3561,6 +3645,61 @@ var res = string.Format("{0} {1}", array); // Compliant we don't know the size o , + + "code" + + : { + + + "impacts" + + : { + + + "MAINTAINABILITY" + + : + + "MEDIUM" + + + }, + + + "attribute" + + : + + "CLEAR" + + + }, + + + "severity" + + : + + "MEDIUM" + + , + @@ -3653,19 +3792,6 @@ var res = string.Format("{0} {1}", array); // Compliant we don't know the size o , - - "defaultSeverity" - - : - - "Major" - - , - diff --git a/frontend/src/deployment/__tests__/resources/metadata/S1000/cfamily-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S1000/cfamily-metadata.json index 5bbae09b90..e33d6eeed8 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S1000/cfamily-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S1000/cfamily-metadata.json @@ -3,6 +3,12 @@ "type": "CODE_SMELL", "status": "ready", "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, "remediation": { "func": "Constant/Issue", "constantCost": "1h" @@ -21,6 +27,7 @@ }, "ruleSpecification": "RSPEC-1000", "sqKey": "UnnamedNamespaceInHeader", + "severity": "MEDIUM", "scope": "Main", "securityStandards": { "CERT": [ diff --git a/frontend/src/deployment/__tests__/resources/metadata/S1000/default-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S1000/default-metadata.json index 32f369d11a..7ea2ebcda4 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S1000/default-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S1000/default-metadata.json @@ -6,6 +6,12 @@ "func": "Constant/Issue", "constantCost": "1h" }, + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, "tags": [ "cert", "misra-c++2008", @@ -20,6 +26,7 @@ }, "ruleSpecification": "RSPEC-1000", "sqKey": "UnnamedNamespaceInHeader", + "severity": "MEDIUM", "scope": "Main", "securityStandards": { "CERT": [ diff --git a/frontend/src/deployment/__tests__/resources/metadata/S3457/cfamily-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S3457/cfamily-metadata.json index ffc7aca78e..cb746b1484 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S3457/cfamily-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S3457/cfamily-metadata.json @@ -2,6 +2,12 @@ "title": "Printf-style format strings should be used correctly", "type": "CODE_SMELL", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, "remediation": { "func": "Constant\/Issue", "constantCost": "10min" @@ -31,6 +37,7 @@ "FIO47-C." ] }, + "severity": "MEDIUM", "languagesSupport": [ { "name": "cfamily", diff --git a/frontend/src/deployment/__tests__/resources/metadata/S3457/csharp-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S3457/csharp-metadata.json index 112a036b36..4ef7c2b1fe 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S3457/csharp-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S3457/csharp-metadata.json @@ -2,6 +2,13 @@ "title": "Composite format strings should be used correctly", "type": "CODE_SMELL", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, + "severity": "MEDIUM", "remediation": { "func": "Constant\/Issue", "constantCost": "10min" diff --git a/frontend/src/deployment/__tests__/resources/metadata/S3457/default-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S3457/default-metadata.json index 034998d3f1..423ebd0395 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S3457/default-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S3457/default-metadata.json @@ -2,6 +2,13 @@ "title": "Printf-style format strings should be used correctly", "type": "CODE_SMELL", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, + "severity": "MEDIUM", "remediation": { "func": "Constant/Issue", "constantCost": "10min" diff --git a/frontend/src/deployment/__tests__/resources/metadata/S3457/java-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S3457/java-metadata.json index a9100127ce..9c07fe5ce9 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S3457/java-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S3457/java-metadata.json @@ -26,7 +26,15 @@ "CERT": [ "FIO47-C." ] - }, "ruleSpecification": "RSPEC-3457", + }, + "code": { + "attribute": "CLEAR", + "impacts": { + "MAINTAINABILITY": "MEDIUM" + } + }, + "severity": "MEDIUM", + "ruleSpecification": "RSPEC-3457", "sqKey": "S3457", "scope": "All", "languagesSupport": [ diff --git a/frontend/src/deployment/__tests__/resources/metadata/S3457/python-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S3457/python-metadata.json index e2d68a9bec..6be0550319 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S3457/python-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S3457/python-metadata.json @@ -12,6 +12,13 @@ "allKeys": [ "S3457" ], + "code": { + "attribute": "CLEAR", + "impacts": { + "MAINTAINABILITY": "MEDIUM" + } + }, + "severity": "MEDIUM", "ruleSpecification": "RSPEC-3457", "sqKey": "S3457", "scope": "All", diff --git a/frontend/src/deployment/__tests__/resources/metadata/S3649/default-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S3649/default-metadata.json index 5b473c0ff8..95c4187ba9 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S3649/default-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S3649/default-metadata.json @@ -2,6 +2,13 @@ "title": "Database queries should not be vulnerable to injection attacks", "type": "VULNERABILITY", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, + "severity": "MEDIUM", "remediation": { "func": "Constant\/Issue", "constantCost": "30min" diff --git a/frontend/src/deployment/__tests__/resources/metadata/S3649/java-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S3649/java-metadata.json index 5b473c0ff8..95c4187ba9 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S3649/java-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S3649/java-metadata.json @@ -2,6 +2,13 @@ "title": "Database queries should not be vulnerable to injection attacks", "type": "VULNERABILITY", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, + "severity": "MEDIUM", "remediation": { "func": "Constant\/Issue", "constantCost": "30min" diff --git a/frontend/src/deployment/__tests__/resources/metadata/S987/cfamily-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S987/cfamily-metadata.json index 9aac21771a..ed087d6184 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S987/cfamily-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S987/cfamily-metadata.json @@ -2,6 +2,13 @@ "title": "\"\" should not be used", "type": "BUG", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, + "severity": "MEDIUM", "remediation": { "func": "Constant\/Issue", "constantCost": "4h" diff --git a/frontend/src/deployment/__tests__/resources/metadata/S987/default-metadata.json b/frontend/src/deployment/__tests__/resources/metadata/S987/default-metadata.json index c34f843f15..21d180790a 100644 --- a/frontend/src/deployment/__tests__/resources/metadata/S987/default-metadata.json +++ b/frontend/src/deployment/__tests__/resources/metadata/S987/default-metadata.json @@ -2,6 +2,13 @@ "title": "\"\" should not be used", "type": "BUG", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, + "severity": "MEDIUM", "remediation": { "func": "Constant/Issue", "constantCost": "4h" diff --git a/frontend/src/deployment/__tests__/resources/rules/S1000/cfamily/metadata.json b/frontend/src/deployment/__tests__/resources/rules/S1000/cfamily/metadata.json index 24efefbbbc..c1806345f7 100644 --- a/frontend/src/deployment/__tests__/resources/rules/S1000/cfamily/metadata.json +++ b/frontend/src/deployment/__tests__/resources/rules/S1000/cfamily/metadata.json @@ -2,6 +2,12 @@ "title": "Header files should not contain unnamed namespaces", "type": "CODE_SMELL", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, "remediation": { "func": "Constant\/Issue", "constantCost": "1h" diff --git a/frontend/src/deployment/__tests__/resources/rules/S3457/metadata.json b/frontend/src/deployment/__tests__/resources/rules/S3457/metadata.json index 6ab1c82dee..2a04d15fbb 100644 --- a/frontend/src/deployment/__tests__/resources/rules/S3457/metadata.json +++ b/frontend/src/deployment/__tests__/resources/rules/S3457/metadata.json @@ -2,6 +2,12 @@ "title": "Composite format strings should be used correctly", "type": "CODE_SMELL", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, "remediation": { "func": "Constant\/Issue", "constantCost": "10min" diff --git a/frontend/src/deployment/__tests__/resources/rules/S3649/metadata.json b/frontend/src/deployment/__tests__/resources/rules/S3649/metadata.json index efbca2da21..7036a92d0d 100644 --- a/frontend/src/deployment/__tests__/resources/rules/S3649/metadata.json +++ b/frontend/src/deployment/__tests__/resources/rules/S3649/metadata.json @@ -2,6 +2,12 @@ "title": "Database queries should not be vulnerable to injection attacks", "type": "VULNERABILITY", "status": "ready", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, "remediation": { "func": "Constant\/Issue", "constantCost": "30min" diff --git a/frontend/src/deployment/__tests__/resources/rules/S987/cfamily/metadata.json b/frontend/src/deployment/__tests__/resources/rules/S987/cfamily/metadata.json index 486817b95a..033eea2958 100644 --- a/frontend/src/deployment/__tests__/resources/rules/S987/cfamily/metadata.json +++ b/frontend/src/deployment/__tests__/resources/rules/S987/cfamily/metadata.json @@ -6,6 +6,12 @@ "func": "Constant\/Issue", "constantCost": "4h" }, + "code": { + "attribute": "CLEAR", + "impacts": { + "MAINTAINABILITY": "MEDIUM" + } + }, "tags": [ "based-on-misra", "lock-in" diff --git a/frontend/src/deployment/__tests__/searchIndex.test.ts b/frontend/src/deployment/__tests__/searchIndex.test.ts index 73bfb59d40..8f90dfb4e7 100644 --- a/frontend/src/deployment/__tests__/searchIndex.test.ts +++ b/frontend/src/deployment/__tests__/searchIndex.test.ts @@ -26,7 +26,7 @@ describe('index store generation', () => { { "name": "python", "status": "deprecated", } ], tags: ['cert', 'clumsy', 'confusing'], - severities: ['Major', 'Minor'], + severities: ['MEDIUM'], qualityProfiles: ['Sonar way'], }); }); diff --git a/frontend/src/deployment/metadata.ts b/frontend/src/deployment/metadata.ts index 6203db3686..d4bf8f6048 100644 --- a/frontend/src/deployment/metadata.ts +++ b/frontend/src/deployment/metadata.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import path from 'path'; -import { maxSeverity } from '../types/Severities'; +import { Severity, maxSeverity } from '../types/Severities'; import { LanguageSupport } from '../types/RuleMetadata'; import { getRulesDirectories, listSupportedLanguages } from './utils'; @@ -138,7 +138,7 @@ function getRuleMetadata(srcDir: string, language?: string) { const genericJson = fs.existsSync(genericFile) ? JSON.parse(fs.readFileSync(genericFile, 'utf8')) : {}; const merged = { ...genericJson, ...languageSpecificJson }; if (merged.hasOwnProperty('code') && merged.code.hasOwnProperty('impacts')) { - merged['severity'] = maxSeverity(merged.code.impacts); + merged['severity'] = Severity[maxSeverity(merged.code.impacts)]; } return merged; } diff --git a/frontend/src/deployment/searchIndex.ts b/frontend/src/deployment/searchIndex.ts index 716814ef26..f0eb30be33 100644 --- a/frontend/src/deployment/searchIndex.ts +++ b/frontend/src/deployment/searchIndex.ts @@ -5,7 +5,7 @@ import path from 'path'; import { stripHtml } from 'string-strip-html'; import lunr, { Token } from 'lunr'; -import { Severity, maxSeverity } from '../types/Severities'; +import { Severity, compareSeverities, maxSeverity } from '../types/Severities'; import { IndexedRule, IndexStore, Type, IndexAggregates } from '../types/IndexStore'; import { logger as rootLogger } from './deploymentLogger'; import { LanguageSupport } from '../types/RuleMetadata'; @@ -22,7 +22,7 @@ export interface IndexedRuleWithDescription extends IndexedRule { function buildOneRuleRecord(allLanguages: string[], rulesPath: string, ruleDir: string) { const types = new Set(); - const severities = new Set(); + const severities = new Set(); const allKeys = new Set([ruleDir]); const titles = new Set(); const tags = new Set(); @@ -54,9 +54,9 @@ function buildOneRuleRecord(allLanguages: string[], rulesPath: string, ruleDir: titles.add(metadata.title); types.add(metadata.type); if (!metadata.hasOwnProperty('code')) { - severities.add(Severity.INFO); + severities.add(Severity[Severity.INFO]); } else { - severities.add(maxSeverity(metadata.code.impacts)); + severities.add(Severity[maxSeverity(metadata.code.impacts)]); } supportedLanguages.push({ name: lang, status: metadata.status }); if (metadata.tags) { @@ -110,7 +110,7 @@ function buildOneRuleIndexedRecord(rulesPath: string, ruleDir: string) id: ruleDir, supportedLanguages: Array.from(record.supportedLanguages).sort((a, b) => a.name.localeCompare(b.name)), types: Array.from(record.types).sort((a, b) => a.localeCompare(b)), - severities: Array.from(record.severities).sort((a, b) => b - a), + severities: Array.from(record.severities).sort((a, b) => compareSeverities(b, a)), all_keys: Array.from(record.allKeys).sort((a, b) => a.localeCompare(b)), titles: Array.from(record.titles).sort((a, b) => a.localeCompare(b)), tags: Array.from(record.tags).sort((a, b) => a.localeCompare(b)), diff --git a/frontend/src/types/IndexStore.ts b/frontend/src/types/IndexStore.ts index 1dc649200f..c2f657f4de 100644 --- a/frontend/src/types/IndexStore.ts +++ b/frontend/src/types/IndexStore.ts @@ -7,7 +7,7 @@ export interface IndexedRule { supportedLanguages: LanguageSupport[]; // FIXME: type, defaultSeverity should never be null but the index generation has a bug types: Type[]; - severities: Severity[]; + severities: string[]; all_keys: string[]; titles: string[]; tags: string[]; diff --git a/frontend/src/types/Severities.ts b/frontend/src/types/Severities.ts index 6238f2cfdf..2e01a3f215 100644 --- a/frontend/src/types/Severities.ts +++ b/frontend/src/types/Severities.ts @@ -17,4 +17,10 @@ export function maxSeverity(impacts: any): Severity { } } return maxSeverity; +} + +export function compareSeverities(a: string, b:string): number { + const seva: Severity = Severity[a as keyof typeof Severity]; + const sevb: Severity = Severity[b as keyof typeof Severity]; + return seva - sevb; } \ No newline at end of file