|
| 1 | +import "./triage.scss"; |
| 2 | +import "@material/web/textfield/filled-text-field.js"; |
| 3 | +import "@material/web/button/filled-button.js"; |
| 4 | +import "@material/web/progress/circular-progress.js"; |
| 5 | + |
| 6 | +document.addEventListener("DOMContentLoaded", () => { |
| 7 | + const vulnIdInput = document.getElementById("vuln-id-input"); |
| 8 | + const loadBtn = document.getElementById("load-btn"); |
| 9 | + const columns = document.querySelectorAll(".triage-column"); |
| 10 | + |
| 11 | + // Map selection values to their respective endpoints/paths |
| 12 | + const sourceConfigMap = { |
| 13 | + // External APIs |
| 14 | + "cve-org": { |
| 15 | + proxySource: "cve", |
| 16 | + }, |
| 17 | + "nvd-api": { |
| 18 | + proxySource: "nvd", |
| 19 | + }, |
| 20 | + // Test Instance |
| 21 | + "test-nvd": { |
| 22 | + proxySource: "test-nvd", |
| 23 | + }, |
| 24 | + "test-cve5": { |
| 25 | + proxySource: "test-cve5", |
| 26 | + }, |
| 27 | + "test-osv": { |
| 28 | + proxySource: "test-osv", |
| 29 | + }, |
| 30 | + "test-nvd-metrics": { |
| 31 | + proxySource: "test-nvd-metrics", |
| 32 | + }, |
| 33 | + "test-cve5-metrics": { |
| 34 | + proxySource: "test-cve5-metrics", |
| 35 | + }, |
| 36 | + // Prod Instance |
| 37 | + "prod-nvd": { |
| 38 | + proxySource: "prod-nvd", |
| 39 | + }, |
| 40 | + "prod-cve5": { |
| 41 | + proxySource: "prod-cve5", |
| 42 | + }, |
| 43 | + "prod-osv": { |
| 44 | + proxySource: "prod-osv", |
| 45 | + }, |
| 46 | + "prod-nvd-metrics": { |
| 47 | + proxySource: "prod-nvd-metrics", |
| 48 | + }, |
| 49 | + "prod-cve5-metrics": { |
| 50 | + proxySource: "prod-cve5-metrics", |
| 51 | + }, |
| 52 | + // API |
| 53 | + "api-test": { |
| 54 | + urlTemplate: "https://api.test.osv.dev/v1/vulns/{id}", |
| 55 | + }, |
| 56 | + "api-prod": { |
| 57 | + urlTemplate: "https://api.osv.dev/v1/vulns/{id}", |
| 58 | + }, |
| 59 | + }; |
| 60 | + |
| 61 | + function escapeHtml(text) { |
| 62 | + const div = document.createElement('div'); |
| 63 | + div.textContent = text; |
| 64 | + return div.innerHTML; |
| 65 | + } |
| 66 | + |
| 67 | + function syntaxHighlight(json) { // credit to https://codepen.io/absolutedevelopment/pen/EpwVzN |
| 68 | + if (typeof json !== 'string') { |
| 69 | + json = JSON.stringify(json, undefined, 2); |
| 70 | + } |
| 71 | + |
| 72 | + const escapedJson = escapeHtml(json); |
| 73 | + |
| 74 | + return escapedJson.replace( |
| 75 | + /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)/g, |
| 76 | + function (match) { |
| 77 | + let cls = 'json-number'; |
| 78 | + if (/^"/.test(match)) { |
| 79 | + if (/:$/.test(match)) { |
| 80 | + cls = 'json-key'; |
| 81 | + } else { |
| 82 | + cls = 'json-string'; |
| 83 | + } |
| 84 | + } else if (/true|false/.test(match)) { |
| 85 | + cls = 'json-boolean'; |
| 86 | + } else if (/null/.test(match)) { |
| 87 | + cls = 'json-null'; |
| 88 | + } |
| 89 | + return `<span class="${cls}">${match}</span>`; |
| 90 | + } |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + async function fetchData(sourceKey, vulnId) { |
| 95 | + const config = sourceConfigMap[sourceKey]; |
| 96 | + let url; |
| 97 | + |
| 98 | + const safeId = encodeURIComponent(vulnId); |
| 99 | + |
| 100 | + if (config.proxySource) { |
| 101 | + url = `/triage/proxy?source=${encodeURIComponent(config.proxySource)}&id=${safeId}`; |
| 102 | + } else if (config.urlTemplate) { |
| 103 | + url = config.urlTemplate.replace("{id}", safeId); |
| 104 | + } else { |
| 105 | + throw new Error("Invalid configuration"); |
| 106 | + } |
| 107 | + |
| 108 | + const response = await fetch(url); |
| 109 | + if (!response.ok) { |
| 110 | + throw new Error(response.status === 404 ? "Not Found" : `Error: ${response.statusText}`); |
| 111 | + } |
| 112 | + return response.json(); |
| 113 | + } |
| 114 | + |
| 115 | + function updateColumn(column) { |
| 116 | + const select = column.querySelector(".source-select"); |
| 117 | + const contentPre = column.querySelector(".json-content"); |
| 118 | + const spinner = column.querySelector(".loading-spinner"); |
| 119 | + const sourceKey = select.value; |
| 120 | + const vulnId = vulnIdInput.value.trim(); |
| 121 | + |
| 122 | + if (!sourceKey) { |
| 123 | + contentPre.textContent = "Select a source to view content"; |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if (!vulnId) { |
| 128 | + contentPre.textContent = "Please enter a Vulnerability ID"; |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + spinner.classList.remove("hidden"); |
| 133 | + contentPre.textContent = ""; |
| 134 | + |
| 135 | + fetchData(sourceKey, vulnId) |
| 136 | + .then((data) => { |
| 137 | + contentPre.innerHTML = syntaxHighlight(data); |
| 138 | + }) |
| 139 | + .catch((error) => { |
| 140 | + contentPre.textContent = error.message; |
| 141 | + }) |
| 142 | + .finally(() => { |
| 143 | + spinner.classList.add("hidden"); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + loadBtn.addEventListener("click", () => { |
| 148 | + columns.forEach((col) => updateColumn(col)); |
| 149 | + }); |
| 150 | + |
| 151 | + // Also handle Enter key on the input field |
| 152 | + vulnIdInput.addEventListener("keydown", (e) => { |
| 153 | + if (e.key === "Enter") { |
| 154 | + columns.forEach((col) => updateColumn(col)); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + // Individual column updates when dropdown changes |
| 159 | + columns.forEach((col) => { |
| 160 | + const select = col.querySelector(".source-select"); |
| 161 | + select.addEventListener("change", () => { |
| 162 | + if (vulnIdInput.value.trim()) { |
| 163 | + updateColumn(col); |
| 164 | + } |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments