return new File(dir, "adhocrules.pb");
}
+ public File cves() {
+ return new File(dir, "cves.pb");
+ }
+
public File fileFor(Domain domain, int componentRef) {
return new File(dir, domain.filePrefix + componentRef + domain.fileSuffix);
}
return Protobuf.readStream(file, ScannerReport.AdHocRule.parser());
}
+ public CloseableIterator<ScannerReport.Cve> readCves() {
+ File file = fileStructure.cves();
+ if (!fileExists(file)) {
+ return emptyCloseableIterator();
+ }
+ return Protobuf.readStream(file, ScannerReport.Cve.parser());
+ }
+
public CloseableIterator<ScannerReport.Measure> readComponentMeasures(int componentRef) {
File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef);
if (fileExists(file)) {
*/
package org.sonar.scanner.protocol.output;
+import com.google.protobuf.AbstractMessageLite;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public void appendComponentIssue(int componentRef, ScannerReport.Issue issue) {
File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
- try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
- issue.writeDelimitedTo(out);
- } catch (Exception e) {
- throw ContextException.of("Unable to write issue", e).addContext("file", file);
- }
+ appendDelimitedTo(file, issue, "issue");
}
public File writeComponentChangedLines(int componentRef, ScannerReport.ChangedLines changedLines) {
public void appendComponentExternalIssue(int componentRef, ScannerReport.ExternalIssue issue) {
File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef);
- try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
- issue.writeDelimitedTo(out);
- } catch (Exception e) {
- throw ContextException.of("Unable to write external issue", e).addContext("file", file);
- }
+ appendDelimitedTo(file, issue, "external issue");
}
public void appendAdHocRule(ScannerReport.AdHocRule adHocRule) {
File file = fileStructure.adHocRules();
- try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
- adHocRule.writeDelimitedTo(out);
- } catch (Exception e) {
- throw ContextException.of("Unable to write ad hoc rule", e).addContext("file", file);
- }
+ appendDelimitedTo(file, adHocRule, "ad hoc rule");
+ }
+
+ public void appendCve(ScannerReport.Cve cve) {
+ File file = fileStructure.cves();
+ appendDelimitedTo(file, cve, "cve");
}
public void appendComponentMeasure(int componentRef, ScannerReport.Measure measure) {
File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef);
+ appendDelimitedTo(file, measure, "measure");
+ }
+
+ private static void appendDelimitedTo(File file, AbstractMessageLite<?, ?> msg, String msgName) {
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
- measure.writeDelimitedTo(out);
+ msg.writeDelimitedTo(out);
} catch (Exception e) {
- throw ContextException.of("Unable to write measure", e).addContext("file", file);
+ throw ContextException.of("Unable to write " + msgName, e).addContext("file", file);
}
}
private JEditorPane activeRuleEditor;
private JScrollPane adHocRuleTab;
private JEditorPane adHocRuleEditor;
+ private JScrollPane cveTab;
+ private JEditorPane cveEditor;
private JScrollPane qualityProfileTab;
private JEditorPane qualityProfileEditor;
private JScrollPane pluginTab;
loadComponents();
updateActiveRules();
updateAdHocRules();
+ updateCves();
updateQualityProfiles();
updatePlugins();
updateMetadata();
}
}
+ private void updateCves() {
+ cveEditor.setText("");
+
+ StringBuilder builder = new StringBuilder();
+ try (CloseableIterator<ScannerReport.Cve> cveCloseableIterator = reader.readCves()) {
+ while (cveCloseableIterator.hasNext()) {
+ builder.append(cveCloseableIterator.next().toString()).append("\n");
+ }
+ cveEditor.setText(builder.toString());
+ }
+ }
+
private void updateQualityProfiles() {
qualityProfileEditor.setText("");
adHocRuleEditor = new JEditorPane();
adHocRuleTab.setViewportView(adHocRuleEditor);
+ cveTab = new JScrollPane();
+ tabbedPane.addTab("CVEs", null, cveTab, null);
+
+ cveEditor = new JEditorPane();
+ cveTab.setViewportView(cveEditor);
+
qualityProfileTab = new JScrollPane();
tabbedPane.addTab("Quality Profiles", null, qualityProfileTab, null);
repeated Impact defaultImpacts = 8;
}
+message Cve {
+ string cve_id = 1;
+ string description = 2;
+ float cvss_score = 3;
+ float epss_score = 4;
+ float epss_percentile = 5;
+ int64 published_date = 6;
+ int64 last_modified_date = 7;
+ repeated string cwe = 8;
+}
+
enum IssueType {
UNSET = 0;
CODE_SMELL = 1;
import com.google.common.collect.Iterators;
import java.io.File;
+import java.time.Instant;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
}
}
+ @Test
+ public void write_cve() {
+
+ // write data
+ ScannerReport.Cve cve = ScannerReport.Cve.newBuilder()
+ .setCveId("CVE-2023-20863")
+ .setDescription("In spring framework versions prior to 5.2.24 release+ ,5.3.27+ and 6.0.8+ , it is possible for a user to provide a specially crafted SpEL expression that may cause a denial-of-service (DoS) condition.")
+ .setCvssScore(6.5f)
+ .setEpssScore(0.00306f)
+ .setEpssPercentile(0.70277f)
+ .setPublishedDate(Instant.parse("2023-04-13T20:15:00Z").toEpochMilli())
+ .setLastModifiedDate(Instant.parse("2024-02-04T02:22:24.474Z").toEpochMilli())
+ .addCwe("CWE-400")
+ .build();
+ underTest.appendCve(cve);
+
+ File file = underTest.getFileStructure().cves();
+ assertThat(file).exists().isFile();
+ try (CloseableIterator<ScannerReport.Cve> read = Protobuf.readStream(file, ScannerReport.Cve.parser())) {
+ assertThat(Iterators.size(read)).isOne();
+ }
+ }
+
@Test
public void write_changed_lines() {
assertThat(underTest.hasComponentData(FileStructure.Domain.CHANGED_LINES, 1)).isFalse();