diff --git a/.vscode/extensions.json b/.vscode/extensions.json index a427a76..9533f80 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -2,9 +2,8 @@ "recommendations": [ "dbaeumer.vscode-eslint", "visualstudioexptteam.vscodeintellicode", - "ms-vscode.vscode-typescript-next", "orta.vscode-jest", - "christian-kohler.npm-intellisense", - "hbenl.vscode-test-explorer" + "hbenl.vscode-test-explorer", + "ms-vscode.vscode-typescript-next" ] } \ No newline at end of file diff --git a/.vscode/server.code-workspace b/.vscode/server.code-workspace new file mode 100644 index 0000000..bab1b7f --- /dev/null +++ b/.vscode/server.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": ".." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/jest.config.js b/jest.config.js index 54bf7e5..8a605aa 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,4 +2,5 @@ module.exports = { transform: { "^.+\\.(t|j)sx?$": ["@swc/jest"], }, + silent: true }; \ No newline at end of file diff --git a/src/models/probe.ts b/src/models/probe.ts index 3fbfc75..65e8037 100644 --- a/src/models/probe.ts +++ b/src/models/probe.ts @@ -25,12 +25,12 @@ export type SupabaseProbeResult = { resultId: string; } -export type ProbeResult = { +export type ProbeResult = { context: { timestampStart: number; timestampStop: number; probeUid: string; probeName: string; }; - result: any; + result: T; } \ No newline at end of file diff --git a/src/models/results/nmap.ts b/src/models/results/nmap.ts new file mode 100644 index 0000000..db3668b --- /dev/null +++ b/src/models/results/nmap.ts @@ -0,0 +1,19 @@ +export type NmapResults = NmapResult[] +export type NmapResult = { + id: string; + sourceIdentifier: string; + published: string; + lastModified: string; + vulnStatus: string; + descriptions: Description[]; + metrics: Metrics; + references: any[]; +} + +type Metrics = { +} + +type Description = { + lang: string; + value: string; +} \ No newline at end of file diff --git a/src/services/reports/parser.ts b/src/services/reports/parser.ts index 4f676bb..67ee901 100644 --- a/src/services/reports/parser.ts +++ b/src/services/reports/parser.ts @@ -1,4 +1,5 @@ import { ProbeResult } from "../../models/probe" +import { NmapResult, NmapResults } from "../../models/results/nmap" import { PROBE_NAMES } from "../../utils" type Parser = (result: ProbeResult) => any @@ -7,8 +8,23 @@ const noParser = (result: ProbeResult) => { return result } -const nmapParser = (result: ProbeResult) => { - return result +export const nmapParser = (probeResult: ProbeResult): ProbeResult => { + const descriptionIgnorePatterns = [ + /\*\* REJECT \*\* DO NOT USE THIS CANDIDATE NUMBER/ + ] + + const finalResults = [] + for (const res of probeResult.result) { + if (descriptionIgnorePatterns.some((pattern) => res.descriptions.map((d) => d.value).some((description) => pattern.test(description)))) { + console.log(`Ignoring ${res.id}`) + continue + } + finalResults.push(res) + } + return { + ...probeResult, + result: finalResults + } } const dummyParser = (result: ProbeResult) => { diff --git a/test/parsers/nmapParser.test.ts b/test/parsers/nmapParser.test.ts new file mode 100644 index 0000000..88a0739 --- /dev/null +++ b/test/parsers/nmapParser.test.ts @@ -0,0 +1,40 @@ +import { ProbeResult } from "../../src/models/probe" +import { NmapResult, NmapResults } from "../../src/models/results/nmap" +import { nmapParser } from "../../src/services/reports/parser" + +describe('Nmap Parser tests', () => { + it('should remove the REJECT CVEs', () => { + const res = { + result: [ + { + descriptions: [ + { lang: 'en', value: '** REJECT ** DO NOT USE THIS CANDIDATE NUMBER bla bla bla' } + ] + } as NmapResult + ] + } as ProbeResult + expect(nmapParser(res).result).toHaveLength(0) + + const res2 = { + result: [ + { + descriptions: [ + { lang: 'en', value: '** REJECT ** DO NOT USE THIS CANDIDATE NUMBER bla bla bla' } + ] + } as NmapResult, + { + descriptions: [ + { lang: 'en', value: 'A good description' } + ] + } as NmapResult, + { + descriptions: [ + { lang: 'en', value: '** REJECT ** DO NOT USE THIS CANDIDATE NUMBER bla bla bla' } + ] + } as NmapResult + ] + } as ProbeResult + expect(nmapParser(res2).result).toHaveLength(1) + expect(nmapParser(res2).result[0].descriptions[0].value).toBe('A good description') + }) +}) \ No newline at end of file diff --git a/test/service/scanService.test.ts b/test/service/scanService.test.ts index a6fed43..0b8f33d 100644 --- a/test/service/scanService.test.ts +++ b/test/service/scanService.test.ts @@ -1,30 +1,15 @@ -import IScanService from "../../src/services/scan/interfaces/scanServiceInterface" -import ScanService from "../../src/services/scan/scanService"; -import { ScanStatus } from "../../src/services/scan/types/startData"; import { createScanRequest, CreateScanRequest } from "../../src/services/requests/validators/scanRequest"; -import AwsSqsQueue from "../../src/storage/messagequeue/awsSqsQueue"; -import SupabaseStorage from "../../src/storage/scans/supabaseStorage"; +import { ScanStatus } from '../../src/models/scan' +import { requestScan } from "../../src/services/requests/scanService"; +import { saveScanStartData, saveProbesStartData } from "../../src/storage/scan.storage"; +import { publishProbeRequest } from "../../src/storage/awsSqsQueue"; -jest.mock('../../src/storage/scans/supabaseStorage.ts') -jest.mock('../../src/storage/messagequeue/awsSqsQueue.ts') -describe('Scan Service Tests', () => { - let scanService: IScanService; - let supabaseStorage: SupabaseStorage; - let awsSqsQueue: AwsSqsQueue; +jest.mock('../../src/storage/scan.storage') +jest.mock('../../src/storage/awsSqsQueue') - let spySaveScanStartData: jest.SpyInstance; - let spySaveProbeStartData: jest.SpyInstance; - let spyPublishProbeRequest: jest.SpyInstance; - beforeEach(() => { - supabaseStorage = new SupabaseStorage(); - awsSqsQueue = new AwsSqsQueue(); - scanService = new ScanService(supabaseStorage, awsSqsQueue); - spySaveScanStartData = jest.spyOn(supabaseStorage, 'saveScanStartData'); - spySaveProbeStartData = jest.spyOn(supabaseStorage, 'saveProbesStartData'); - spyPublishProbeRequest = jest.spyOn(awsSqsQueue, 'publishProbeRequest'); - }) +describe('Scan Service Tests', () => { it('should request a scan', async () => { const scanRequest: CreateScanRequest = { @@ -34,23 +19,23 @@ describe('Scan Service Tests', () => { { name: 'probe-nmap', settings: {} } ] } - const response = await scanService.requestScan(scanRequest); + const response = await requestScan(scanRequest); expect(response.scanId).toBeDefined() - expect(spySaveScanStartData).toHaveBeenCalledWith({ + expect(saveScanStartData).toHaveBeenCalledWith({ id: response.scanId, status: ScanStatus.PENDING, notification: false, target: scanRequest.target, periodicity: '* * * * *' }); - expect(spySaveProbeStartData).toHaveBeenCalledWith([ + expect(saveProbesStartData).toHaveBeenCalledWith([ { id: expect.any(String), status: ScanStatus.PENDING, scanId: response.scanId } ]); - expect(spyPublishProbeRequest).toHaveBeenCalledWith([{ + expect(publishProbeRequest).toHaveBeenCalledWith([{ context: { id: expect.any(String), name: 'probe-nmap',