| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.xml.suppression; |
| 19 | |
|
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.List; |
| 22 | |
import org.xml.sax.Attributes; |
| 23 | |
import org.xml.sax.SAXException; |
| 24 | |
import org.xml.sax.helpers.DefaultHandler; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 14 | public class SuppressionHandler extends DefaultHandler { |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public static final String SUPPRESS = "suppress"; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public static final String FILE_PATH = "filePath"; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public static final String SHA1 = "sha1"; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public static final String CVE = "cve"; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public static final String CPE = "cpe"; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public static final String CWE = "cwe"; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static final String GAV = "gav"; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public static final String CVSS_BELOW = "cvssBelow"; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 14 | private final List<SuppressionRule> suppressionRules = new ArrayList<SuppressionRule>(); |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public List<SuppressionRule> getSuppressionRules() { |
| 76 | 11 | return suppressionRules; |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
private SuppressionRule rule; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
private Attributes currentAttributes; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
private StringBuilder currentText; |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
@Override |
| 101 | |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 102 | 2187 | currentAttributes = attributes; |
| 103 | 2187 | currentText = new StringBuilder(); |
| 104 | 2187 | if (SUPPRESS.equals(qName)) { |
| 105 | 482 | rule = new SuppressionRule(); |
| 106 | 482 | final String base = currentAttributes.getValue("base"); |
| 107 | 482 | if (base != null) { |
| 108 | 482 | rule.setBase(Boolean.parseBoolean(base)); |
| 109 | |
} else { |
| 110 | 0 | rule.setBase(false); |
| 111 | |
} |
| 112 | |
} |
| 113 | 2187 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
@Override |
| 124 | |
public void endElement(String uri, String localName, String qName) throws SAXException { |
| 125 | 2187 | if (SUPPRESS.equals(qName)) { |
| 126 | 482 | suppressionRules.add(rule); |
| 127 | 482 | rule = null; |
| 128 | 1705 | } else if (FILE_PATH.equals(qName)) { |
| 129 | 89 | final PropertyType pt = processPropertyType(); |
| 130 | 89 | rule.setFilePath(pt); |
| 131 | 89 | } else if (SHA1.equals(qName)) { |
| 132 | 4 | rule.setSha1(currentText.toString()); |
| 133 | 1612 | } else if (GAV.equals(qName)) { |
| 134 | 385 | final PropertyType pt = processPropertyType(); |
| 135 | 385 | rule.setGav(pt); |
| 136 | 385 | } else if (CPE.equals(qName)) { |
| 137 | 701 | final PropertyType pt = processPropertyType(); |
| 138 | 701 | rule.addCpe(pt); |
| 139 | 701 | } else if (CWE.equals(qName)) { |
| 140 | 0 | rule.addCwe(currentText.toString()); |
| 141 | 526 | } else if (CVE.equals(qName)) { |
| 142 | 29 | rule.addCve(currentText.toString()); |
| 143 | 497 | } else if (CVSS_BELOW.equals(qName)) { |
| 144 | 4 | final float cvss = Float.parseFloat(currentText.toString()); |
| 145 | 4 | rule.addCvssBelow(cvss); |
| 146 | |
} |
| 147 | 2187 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
@Override |
| 158 | |
public void characters(char[] ch, int start, int length) throws SAXException { |
| 159 | 4922 | currentText.append(ch, start, length); |
| 160 | 4922 | } |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
private PropertyType processPropertyType() { |
| 169 | 1175 | final PropertyType pt = new PropertyType(); |
| 170 | 1175 | pt.setValue(currentText.toString()); |
| 171 | 1175 | if (currentAttributes != null && currentAttributes.getLength() > 0) { |
| 172 | 1175 | final String regex = currentAttributes.getValue("regex"); |
| 173 | 1175 | if (regex != null) { |
| 174 | 1175 | pt.setRegex(Boolean.parseBoolean(regex)); |
| 175 | |
} |
| 176 | 1175 | final String caseSensitive = currentAttributes.getValue("caseSensitive"); |
| 177 | 1175 | if (caseSensitive != null) { |
| 178 | 1175 | pt.setCaseSensitive(Boolean.parseBoolean(caseSensitive)); |
| 179 | |
} |
| 180 | |
} |
| 181 | 1175 | return pt; |
| 182 | |
} |
| 183 | |
} |