-
Notifications
You must be signed in to change notification settings - Fork 5
Parse CVE information from OX CSAF website #79
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| // @ts-nocheck | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import crypto from 'crypto'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| const CSAF_ROOT = 'https://documentation.open-xchange.com/dovecot/security/advisories/csaf/'; | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| const projectRoot = path.resolve(__dirname, '..'); | ||
| const legacySecurityPath = path.join(projectRoot, 'src/lib/data/legacy-security.json'); | ||
| const outputSecurity = path.join(projectRoot, 'src/lib/data/security.json'); | ||
|
|
||
| const DUMMY_ENTRY = [ | ||
| { | ||
| id: 'CVE-0000-00000', | ||
| severity: 'moderate', | ||
| date: '01 Jan 2024', | ||
| description: | ||
| 'Dev mode placeholder — CSAF data not fetched. Run npm run dev-production to fetch real data.', | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| link: '' | ||
| } | ||
| ]; | ||
|
|
||
| // --- Date helpers --- | ||
|
|
||
| function normalizeDate(isoDate) { | ||
| const d = new Date(isoDate); | ||
| if (isNaN(d)) return isoDate; | ||
| const months = [ | ||
| 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', | ||
| 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' | ||
| ]; | ||
| return `${String(d.getUTCDate()).padStart(2, '0')} ${months[d.getUTCMonth()]} ${d.getUTCFullYear()}`; | ||
| } | ||
|
|
||
| // --- Severity helpers --- | ||
|
|
||
| function normalizeSeverity(baseSeverity) { | ||
| return { HIGH: 'high', CRITICAL: 'high', LOW: 'low' }[(baseSeverity || '').toUpperCase()] || 'moderate'; | ||
| } | ||
|
|
||
| // --- CE product filtering helpers --- | ||
|
|
||
| /** | ||
| * Collects the product IDs that belong to the "OX Dovecot CE" branch. | ||
| * Returns null when the product_tree has no such branch (pre-split era, | ||
| * where "OX Dovecot Pro core" covered both Pro and CE — show everything). | ||
| */ | ||
| function collectCeProductIds(productTree) { | ||
| for (const vendorBranch of productTree?.branches || []) { | ||
| for (const productBranch of vendorBranch.branches || []) { | ||
| if (productBranch.name !== 'OX Dovecot CE') continue; | ||
| const ceIds = new Set(); | ||
| for (const versionBranch of productBranch.branches || []) { | ||
| if (versionBranch.product?.product_id) { | ||
| ceIds.add(versionBranch.product.product_id); | ||
| } | ||
| } | ||
| return ceIds; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| // Post-split: show only if a CE product ID appears in known/last_affected. | ||
| function affectsCe(vulnerability, ceIds) { | ||
| if (!ceIds) return true; // pre-split or no product_tree: Pro core == CE | ||
| const status = vulnerability.product_status || {}; | ||
| const candidates = [...(status.known_affected || []), ...(status.last_affected || [])]; | ||
| return candidates.some((id) => ceIds.has(id)); | ||
| } | ||
|
|
||
| // Post-split: prefer the score entry whose products list includes a CE product ID. | ||
| function pickSeverity(vulnerability, ceIds) { | ||
| const scores = vulnerability.scores || []; | ||
| if (!scores.length) return 'moderate'; | ||
|
|
||
| if (ceIds) { | ||
| const ceScore = scores.find((s) => (s.products || []).some((p) => ceIds.has(p))); | ||
| if (ceScore) return normalizeSeverity(ceScore.cvss_v3?.baseSeverity); | ||
| } | ||
|
|
||
| return normalizeSeverity(scores[0]?.cvss_v3?.baseSeverity); | ||
| } | ||
|
|
||
| // --- CSAF file parsing --- | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| /** | ||
| * Parses a single CSAF document into zero or more normalized CVE records. | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| * Only records that affect Community Edition are returned. | ||
| */ | ||
| function parseCsafFile(csaf) { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| const ceIds = collectCeProductIds(csaf.product_tree); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| const date = normalizeDate(csaf.document.tracking.current_release_date); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| const htmlRef = (csaf.document.references || []).find( | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| (r) => r.summary === 'HTML representation' | ||
| ); | ||
| const link = htmlRef?.url || ''; | ||
|
|
||
| const entries = []; | ||
| for (const vuln of csaf.vulnerabilities || []) { | ||
| if (!affectsCe(vuln, ceIds)) continue; | ||
|
|
||
| const description = | ||
| (vuln.notes || []).find((n) => n.category === 'description')?.text || vuln.title || ''; | ||
| const severity = pickSeverity(vuln, ceIds); | ||
|
|
||
| entries.push({ | ||
| id: vuln.cve, | ||
| severity, | ||
| date, | ||
| description, | ||
| link | ||
| }); | ||
| } | ||
| return entries; | ||
| } | ||
|
|
||
| // --- Fetch + verify --- | ||
|
|
||
| /** | ||
| * Fetches a CSAF JSON file from the given URL, verifies its SHA-512 checksum, | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| * and returns the parsed JSON object. | ||
| * Throws on network errors or checksum mismatches. | ||
| */ | ||
| async function fetchAndVerify(url) { | ||
| const [jsonRes, shaRes] = await Promise.all([fetch(url), fetch(`${url}.sha512`)]); | ||
|
|
||
| if (!jsonRes.ok) throw new Error(`Failed to fetch ${url}: HTTP ${jsonRes.status}`); | ||
| if (!shaRes.ok) | ||
| throw new Error(`Failed to fetch checksum for ${url}: HTTP ${shaRes.status}`); | ||
|
|
||
| const jsonText = await jsonRes.text(); | ||
| const shaText = (await shaRes.text()).trim(); | ||
|
|
||
| // GNU coreutils ("<hash> <filename>"), BSD ("SHA512 (file) = <hash>"), or bare hash. | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| const expectedHash = shaText.match(/[0-9a-f]{128}/)?.[0] ?? ''; | ||
|
|
||
| const actualHash = crypto.createHash('sha512').update(jsonText).digest('hex'); | ||
|
|
||
| if (actualHash !== expectedHash) { | ||
| throw new Error( | ||
| `SHA-512 checksum mismatch for ${url}\n` + | ||
| ` expected: ${expectedHash}\n` + | ||
| ` actual: ${actualHash}` | ||
| ); | ||
| } | ||
|
|
||
| return JSON.parse(jsonText); | ||
| } | ||
|
|
||
| // --- Main pipeline --- | ||
|
|
||
| /** | ||
| * Fetches all CSAF advisories, merges with legacy local data, | ||
| * and writes the result to security.json. | ||
| */ | ||
| export async function fetchAndMerge(logger) { | ||
| logger?.info('[fetch-csaf] Fetching CSAF index...'); | ||
|
|
||
| const indexRes = await fetch(`${CSAF_ROOT}index.txt`); | ||
| if (!indexRes.ok) | ||
| throw new Error(`Failed to fetch CSAF index: HTTP ${indexRes.status}`); | ||
|
|
||
| const indexText = await indexRes.text(); | ||
| const advisoryPaths = indexText | ||
| .split('\n') | ||
| .map((l) => l.trim()) | ||
| .filter(Boolean); | ||
|
|
||
| logger?.info(`[fetch-csaf] Found ${advisoryPaths.length} advisories in index.`); | ||
|
|
||
| const csafEntries = []; | ||
| for (const relPath of advisoryPaths) { | ||
| const url = `${CSAF_ROOT}${relPath}`; | ||
| logger?.info(`[fetch-csaf] Fetching ${relPath}...`); | ||
| const csaf = await fetchAndVerify(url); | ||
| const entries = parseCsafFile(csaf); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| logger?.info(`[fetch-csaf] → ${entries.length} CE CVE(s) extracted.`); | ||
| csafEntries.push(...entries); | ||
| } | ||
|
|
||
| const legacy = JSON.parse(fs.readFileSync(legacySecurityPath, 'utf8')); | ||
|
|
||
| // Deduplicate by CVE ID (CSAF takes precedence over legacy if both somehow exist) | ||
| const seen = new Set(); | ||
| const merged = []; | ||
| for (const entry of [...csafEntries, ...legacy]) { | ||
| if (!seen.has(entry.id)) { | ||
| seen.add(entry.id); | ||
| merged.push(entry); | ||
| } | ||
| } | ||
|
|
||
| merged.sort((a, b) => new Date(b.date) - new Date(a.date)); | ||
|
|
||
| fs.writeFileSync(outputSecurity, JSON.stringify(merged, null, 2)); | ||
| logger?.info( | ||
| `[fetch-csaf] Wrote ${outputSecurity} — ` + | ||
| `${merged.length} total entries (${csafEntries.length} from CSAF, ${legacy.length} legacy).` | ||
| ); | ||
| } | ||
|
|
||
| // --- Vite plugin --- | ||
|
|
||
| export function fetchCsafPlugin() { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| let mode = 'production'; | ||
| let viteLogger; | ||
|
|
||
| return { | ||
| name: 'fetch-csaf', | ||
|
|
||
| configResolved(config) { | ||
| mode = config.mode; | ||
| viteLogger = config.logger; | ||
| }, | ||
|
|
||
| async buildStart() { | ||
| if (mode === 'development') { | ||
| fs.writeFileSync(outputSecurity, JSON.stringify(DUMMY_ENTRY, null, 2)); | ||
| viteLogger?.info( | ||
| '[fetch-csaf] Dev mode: writing dummy security entry (no CSAF fetch).' | ||
| ); | ||
| } else { | ||
| await fetchAndMerge(viteLogger); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // Allow running as a standalone script: node scripts/fetch-csaf.js | ||
| const isMain = | ||
| process.argv[1] && fs.realpathSync(process.argv[1]) === fs.realpathSync(__filename); | ||
| if (isMain) { | ||
| fetchAndMerge(console).catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.