| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.xml.hints; |
| 19 | |
|
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.List; |
| 22 | |
import org.owasp.dependencycheck.dependency.Confidence; |
| 23 | |
import org.owasp.dependencycheck.xml.suppression.PropertyType; |
| 24 | |
import org.xml.sax.Attributes; |
| 25 | |
import org.xml.sax.SAXException; |
| 26 | |
import org.xml.sax.helpers.DefaultHandler; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | 5 | public class HintHandler extends DefaultHandler { |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
private static final String HINT = "hint"; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
private static final String GIVEN = "given"; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
private static final String ADD = "add"; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
private static final String EVIDENCE = "evidence"; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private static final String FILE_NAME = "fileName"; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
private static final String VENDOR_DUPLICATING_RULE = "vendorDuplicatingHint"; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private static final String DUPLICATE = "duplicate"; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private static final String VENDOR = "vendor"; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
private static final String PRODUCT = "product"; |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
private static final String VERSION = "version"; |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
private static final String CONFIDENCE = "confidence"; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private static final String VALUE = "value"; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
private static final String NAME = "name"; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
private static final String SOURCE = "source"; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
private static final String TYPE = "type"; |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
private static final String CASE_SENSITIVE = "caseSensitive"; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
private static final String REGEX = "regex"; |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
private static final String CONTAINS = "contains"; |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | 5 | private final List<HintRule> hintRules = new ArrayList<HintRule>(); |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public List<HintRule> getHintRules() { |
| 121 | 5 | return hintRules; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | 5 | private final List<VendorDuplicatingHintRule> vendorDuplicatingHintRules = new ArrayList<VendorDuplicatingHintRule>(); |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public List<VendorDuplicatingHintRule> getVendorDuplicatingHintRules() { |
| 135 | 4 | return vendorDuplicatingHintRules; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
private HintRule rule; |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | 5 | private boolean inAddNode = false; |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
@Override |
| 158 | |
public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException { |
| 159 | 205 | if (HINT.equals(qName)) { |
| 160 | 30 | rule = new HintRule(); |
| 161 | 175 | } else if (ADD.equals(qName)) { |
| 162 | 30 | inAddNode = true; |
| 163 | 145 | } else if (GIVEN.equals(qName)) { |
| 164 | 30 | inAddNode = false; |
| 165 | 115 | } else if (EVIDENCE.equals(qName)) { |
| 166 | 82 | final String hintType = attr.getValue(TYPE); |
| 167 | 82 | if (VENDOR.equals(hintType)) { |
| 168 | 39 | if (inAddNode) { |
| 169 | 64 | rule.addAddVendor(attr.getValue(SOURCE), |
| 170 | 32 | attr.getValue(NAME), |
| 171 | 32 | attr.getValue(VALUE), |
| 172 | 32 | Confidence.valueOf(attr.getValue(CONFIDENCE))); |
| 173 | |
} else { |
| 174 | 14 | rule.addGivenVendor(attr.getValue(SOURCE), |
| 175 | 7 | attr.getValue(NAME), |
| 176 | 7 | attr.getValue(VALUE), |
| 177 | 7 | Confidence.valueOf(attr.getValue(CONFIDENCE))); |
| 178 | |
} |
| 179 | 43 | } else if (PRODUCT.equals(hintType)) { |
| 180 | 33 | if (inAddNode) { |
| 181 | 28 | rule.addAddProduct(attr.getValue(SOURCE), |
| 182 | 14 | attr.getValue(NAME), |
| 183 | 14 | attr.getValue(VALUE), |
| 184 | 14 | Confidence.valueOf(attr.getValue(CONFIDENCE))); |
| 185 | |
} else { |
| 186 | 38 | rule.addGivenProduct(attr.getValue(SOURCE), |
| 187 | 19 | attr.getValue(NAME), |
| 188 | 19 | attr.getValue(VALUE), |
| 189 | 19 | Confidence.valueOf(attr.getValue(CONFIDENCE))); |
| 190 | |
} |
| 191 | 10 | } else if (VERSION.equals(hintType)) { |
| 192 | 10 | if (inAddNode) { |
| 193 | 20 | rule.addAddVersion(attr.getValue(SOURCE), |
| 194 | 10 | attr.getValue(NAME), |
| 195 | 10 | attr.getValue(VALUE), |
| 196 | 10 | Confidence.valueOf(attr.getValue(CONFIDENCE))); |
| 197 | |
} |
| 198 | |
} |
| 199 | 82 | } else if (FILE_NAME.equals(qName)) { |
| 200 | 18 | final PropertyType pt = new PropertyType(); |
| 201 | 18 | pt.setValue(attr.getValue(CONTAINS)); |
| 202 | 18 | if (attr.getLength() > 0) { |
| 203 | 18 | final String regex = attr.getValue(REGEX); |
| 204 | 18 | if (regex != null) { |
| 205 | 18 | pt.setRegex(Boolean.parseBoolean(regex)); |
| 206 | |
} |
| 207 | 18 | final String caseSensitive = attr.getValue(CASE_SENSITIVE); |
| 208 | 18 | if (caseSensitive != null) { |
| 209 | 18 | pt.setCaseSensitive(Boolean.parseBoolean(caseSensitive)); |
| 210 | |
} |
| 211 | |
} |
| 212 | 18 | rule.addFilename(pt); |
| 213 | 18 | } else if (VENDOR_DUPLICATING_RULE.equals(qName)) { |
| 214 | 10 | vendorDuplicatingHintRules.add(new VendorDuplicatingHintRule(attr.getValue(VALUE), attr.getValue(DUPLICATE))); |
| 215 | |
} |
| 216 | 205 | } |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
@Override |
| 228 | |
public void endElement(String uri, String localName, String qName) throws SAXException { |
| 229 | 205 | if (HINT.equals(qName) && rule != null) { |
| 230 | 30 | hintRules.add(rule); |
| 231 | 30 | rule = null; |
| 232 | |
} |
| 233 | 205 | } |
| 234 | |
} |