| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SuppressionHandler |
|
| 3.6;3.6 |
| 1 | /* | |
| 2 | * This file is part of dependency-check-core. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | * | |
| 16 | * Copyright (c) 2013 Jeremy Long. All Rights Reserved. | |
| 17 | */ | |
| 18 | package org.owasp.dependencycheck.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 | * A handler to load suppression rules. | |
| 28 | * | |
| 29 | * @author Jeremy Long <jeremy.long@owasp.org> | |
| 30 | */ | |
| 31 | 0 | public class SuppressionHandler extends DefaultHandler { |
| 32 | ||
| 33 | /** | |
| 34 | * The suppress node, indicates the start of a new rule. | |
| 35 | */ | |
| 36 | public static final String SUPPRESS = "suppress"; | |
| 37 | /** | |
| 38 | * The file path element name. | |
| 39 | */ | |
| 40 | public static final String FILE_PATH = "filePath"; | |
| 41 | /** | |
| 42 | * The sha1 hash element name. | |
| 43 | */ | |
| 44 | public static final String SHA1 = "sha1"; | |
| 45 | /** | |
| 46 | * The CVE element name. | |
| 47 | */ | |
| 48 | public static final String CVE = "cve"; | |
| 49 | /** | |
| 50 | * The CPE element name. | |
| 51 | */ | |
| 52 | public static final String CPE = "cpe"; | |
| 53 | /** | |
| 54 | * The CWE element name. | |
| 55 | */ | |
| 56 | public static final String CWE = "cwe"; | |
| 57 | /** | |
| 58 | * The cvssBelow element name. | |
| 59 | */ | |
| 60 | public static final String CVSS_BELOW = "cvssBelow"; | |
| 61 | /** | |
| 62 | * A list of suppression rules. | |
| 63 | */ | |
| 64 | 0 | private List<SuppressionRule> suppressionRules = new ArrayList<SuppressionRule>(); |
| 65 | ||
| 66 | /** | |
| 67 | * Get the value of suppressionRules. | |
| 68 | * | |
| 69 | * @return the value of suppressionRules | |
| 70 | */ | |
| 71 | public List<SuppressionRule> getSuppressionRules() { | |
| 72 | return suppressionRules; | |
| 73 | } | |
| 74 | /** | |
| 75 | * The current rule being read. | |
| 76 | */ | |
| 77 | private SuppressionRule rule; | |
| 78 | /** | |
| 79 | * The attributes of the node being read. | |
| 80 | */ | |
| 81 | private Attributes currentAttributes; | |
| 82 | /** | |
| 83 | * The current node text being extracted from the element. | |
| 84 | */ | |
| 85 | private StringBuffer currentText; | |
| 86 | ||
| 87 | /** | |
| 88 | * Handles the start element event. | |
| 89 | * | |
| 90 | * @param uri the uri of the element being processed | |
| 91 | * @param localName the local name of the element being processed | |
| 92 | * @param qName the qName of the element being processed | |
| 93 | * @param attributes the attributes of the element being processed | |
| 94 | * @throws SAXException thrown if there is an exception processing | |
| 95 | */ | |
| 96 | @Override | |
| 97 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { | |
| 98 | 0 | currentAttributes = null; |
| 99 | 0 | currentText = new StringBuffer(); |
| 100 | ||
| 101 | 0 | if (SUPPRESS.equals(qName)) { |
| 102 | 0 | rule = new SuppressionRule(); |
| 103 | 0 | } else if (FILE_PATH.equals(qName)) { |
| 104 | 0 | currentAttributes = attributes; |
| 105 | } | |
| 106 | 0 | } |
| 107 | ||
| 108 | /** | |
| 109 | * Handles the end element event. | |
| 110 | * | |
| 111 | * @param uri the URI of the element | |
| 112 | * @param localName the local name of the element | |
| 113 | * @param qName the qName of the element | |
| 114 | * @throws SAXException thrown if there is an exception processing | |
| 115 | */ | |
| 116 | @Override | |
| 117 | public void endElement(String uri, String localName, String qName) throws SAXException { | |
| 118 | 0 | if (SUPPRESS.equals(qName)) { |
| 119 | 0 | suppressionRules.add(rule); |
| 120 | 0 | rule = null; |
| 121 | 0 | } else if (FILE_PATH.equals(qName)) { |
| 122 | 0 | final PropertyType pt = processPropertyType(); |
| 123 | 0 | rule.setFilePath(pt); |
| 124 | 0 | } else if (SHA1.equals(qName)) { |
| 125 | 0 | rule.setSha1(currentText.toString()); |
| 126 | 0 | } else if (CPE.equals(qName)) { |
| 127 | 0 | final PropertyType pt = processPropertyType(); |
| 128 | 0 | rule.addCpe(pt); |
| 129 | 0 | } else if (CWE.equals(qName)) { |
| 130 | 0 | rule.addCwe(currentText.toString()); |
| 131 | 0 | } else if (CVE.equals(qName)) { |
| 132 | 0 | rule.addCve(currentText.toString()); |
| 133 | 0 | } else if (CVSS_BELOW.equals(qName)) { |
| 134 | 0 | final float cvss = Float.parseFloat(currentText.toString()); |
| 135 | 0 | rule.addCvssBelow(cvss); |
| 136 | } | |
| 137 | 0 | } |
| 138 | ||
| 139 | /** | |
| 140 | * Collects the body text of the node being processed. | |
| 141 | * | |
| 142 | * @param ch the char array of text | |
| 143 | * @param start the start position to copy text from in the char array | |
| 144 | * @param length the number of characters to copy from the char array | |
| 145 | * @throws SAXException thrown if there is a parsing exception | |
| 146 | */ | |
| 147 | @Override | |
| 148 | public void characters(char[] ch, int start, int length) throws SAXException { | |
| 149 | 0 | currentText.append(ch, start, length); |
| 150 | 0 | } |
| 151 | ||
| 152 | /** | |
| 153 | * Processes field members that have been collected during the characters and startElement method to construct a | |
| 154 | * PropertyType object. | |
| 155 | * | |
| 156 | * @return a PropertyType object | |
| 157 | */ | |
| 158 | private PropertyType processPropertyType() { | |
| 159 | 0 | final PropertyType pt = new PropertyType(); |
| 160 | 0 | pt.setValue(currentText.toString()); |
| 161 | 0 | if (currentAttributes != null && currentAttributes.getLength() > 0) { |
| 162 | 0 | final String regex = currentAttributes.getValue("regex"); |
| 163 | 0 | if (regex != null) { |
| 164 | 0 | pt.setRegex(Boolean.parseBoolean(regex)); |
| 165 | } | |
| 166 | 0 | final String caseSensitive = currentAttributes.getValue("caseSensitive"); |
| 167 | 0 | if (regex != null) { |
| 168 | 0 | pt.setCaseSensitive(Boolean.parseBoolean(caseSensitive)); |
| 169 | } | |
| 170 | } | |
| 171 | 0 | return pt; |
| 172 | } | |
| 173 | } |