| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| VulnerableSoftware |
|
| 3.2777777777777777;3.278 |
| 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) 2012 Jeremy Long. All Rights Reserved. | |
| 17 | */ | |
| 18 | package org.owasp.dependencycheck.dependency; | |
| 19 | ||
| 20 | import java.io.Serializable; | |
| 21 | import java.io.UnsupportedEncodingException; | |
| 22 | import java.net.URLDecoder; | |
| 23 | import java.util.logging.Level; | |
| 24 | import java.util.logging.Logger; | |
| 25 | import org.owasp.dependencycheck.data.cpe.IndexEntry; | |
| 26 | ||
| 27 | /** | |
| 28 | * A record containing information about vulnerable software. This is referenced from a vulnerability. | |
| 29 | * | |
| 30 | * @author Jeremy Long <jeremy.long@owasp.org> | |
| 31 | */ | |
| 32 | 0 | public class VulnerableSoftware extends IndexEntry implements Serializable, Comparable<VulnerableSoftware> { |
| 33 | ||
| 34 | /** | |
| 35 | * The serial version UID. | |
| 36 | */ | |
| 37 | private static final long serialVersionUID = 307319490326651052L; | |
| 38 | ||
| 39 | /** | |
| 40 | * Parse a CPE entry from the cpe string representation. | |
| 41 | * | |
| 42 | * @param cpe a cpe entry (e.g. cpe:/a:vendor:software:version) | |
| 43 | */ | |
| 44 | public void setCpe(String cpe) { | |
| 45 | try { | |
| 46 | 0 | parseName(cpe); |
| 47 | 0 | } catch (UnsupportedEncodingException ex) { |
| 48 | 0 | final String msg = String.format("Character encoding is unsupported for CPE '%s'.", cpe); |
| 49 | 0 | Logger.getLogger(VulnerableSoftware.class.getName()).log(Level.WARNING, msg); |
| 50 | 0 | Logger.getLogger(VulnerableSoftware.class.getName()).log(Level.FINE, null, ex); |
| 51 | 0 | setName(cpe); |
| 52 | 0 | } |
| 53 | 0 | } |
| 54 | ||
| 55 | /** | |
| 56 | * <p> | |
| 57 | * Parses a name attribute value, from the cpe.xml, into its corresponding parts: vendor, product, version, | |
| 58 | * revision.</p> | |
| 59 | * <p> | |
| 60 | * Example:</p> | |
| 61 | * <code> cpe:/a:apache:struts:1.1:rc2</code> | |
| 62 | * | |
| 63 | * <p> | |
| 64 | * Results in:</p> <ul> <li>Vendor: apache</li> <li>Product: struts</li> | |
| 65 | * <li>Version: 1.1</li> <li>Revision: rc2</li> </ul> | |
| 66 | * | |
| 67 | * @param cpeName the cpe name | |
| 68 | * @throws UnsupportedEncodingException should never be thrown... | |
| 69 | */ | |
| 70 | @Override | |
| 71 | public void parseName(String cpeName) throws UnsupportedEncodingException { | |
| 72 | 0 | this.name = cpeName; |
| 73 | 0 | if (cpeName != null && cpeName.length() > 7) { |
| 74 | 0 | final String[] data = cpeName.substring(7).split(":"); |
| 75 | 0 | if (data.length >= 1) { |
| 76 | 0 | this.setVendor(URLDecoder.decode(data[0].replace("+", "%2B"), "UTF-8")); |
| 77 | } | |
| 78 | 0 | if (data.length >= 2) { |
| 79 | 0 | this.setProduct(URLDecoder.decode(data[1].replace("+", "%2B"), "UTF-8")); |
| 80 | } | |
| 81 | 0 | if (data.length >= 3) { |
| 82 | 0 | version = URLDecoder.decode(data[2].replace("+", "%2B"), "UTF-8"); |
| 83 | } | |
| 84 | 0 | if (data.length >= 4) { |
| 85 | 0 | revision = URLDecoder.decode(data[3].replace("+", "%2B"), "UTF-8"); |
| 86 | } | |
| 87 | 0 | if (data.length >= 5) { |
| 88 | 0 | edition = URLDecoder.decode(data[4].replace("+", "%2B"), "UTF-8"); |
| 89 | } | |
| 90 | } | |
| 91 | 0 | } |
| 92 | /** | |
| 93 | * If present, indicates that previous version are vulnerable. | |
| 94 | */ | |
| 95 | private String previousVersion; | |
| 96 | ||
| 97 | /** | |
| 98 | * Indicates if previous versions of this software are vulnerable. | |
| 99 | * | |
| 100 | * @return if previous versions of this software are vulnerable | |
| 101 | */ | |
| 102 | public boolean hasPreviousVersion() { | |
| 103 | 0 | return previousVersion != null; |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Get the value of previousVersion. | |
| 108 | * | |
| 109 | * @return the value of previousVersion | |
| 110 | */ | |
| 111 | public String getPreviousVersion() { | |
| 112 | return previousVersion; | |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * Set the value of previousVersion. | |
| 117 | * | |
| 118 | * @param previousVersion new value of previousVersion | |
| 119 | */ | |
| 120 | public void setPreviousVersion(String previousVersion) { | |
| 121 | this.previousVersion = previousVersion; | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * Standard equals implementation to compare this VulnerableSoftware to another object. | |
| 126 | * | |
| 127 | * @param obj the object to compare | |
| 128 | * @return whether or not the objects are equal | |
| 129 | */ | |
| 130 | @Override | |
| 131 | public boolean equals(Object obj) { | |
| 132 | 0 | if (obj == null) { |
| 133 | 0 | return false; |
| 134 | } | |
| 135 | 0 | if (getClass() != obj.getClass()) { |
| 136 | 0 | return false; |
| 137 | } | |
| 138 | 0 | final VulnerableSoftware other = (VulnerableSoftware) obj; |
| 139 | 0 | if ((this.getName() == null) ? (other.getName() != null) : !this.getName().equals(other.getName())) { |
| 140 | 0 | return false; |
| 141 | } | |
| 142 | 0 | return true; |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * Standard implementation of hashCode. | |
| 147 | * | |
| 148 | * @return the hashCode for the object | |
| 149 | */ | |
| 150 | @Override | |
| 151 | public int hashCode() { | |
| 152 | 0 | int hash = 7; |
| 153 | 0 | hash = 83 * hash + (this.getName() != null ? this.getName().hashCode() : 0); |
| 154 | 0 | return hash; |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Standard toString() implementation display the name and whether or not previous versions are also affected. | |
| 159 | * | |
| 160 | * @return a string representation of the object | |
| 161 | */ | |
| 162 | @Override | |
| 163 | public String toString() { | |
| 164 | 0 | return "VulnerableSoftware{ name=" + name + ", previousVersion=" + previousVersion + '}'; |
| 165 | } | |
| 166 | ||
| 167 | /** | |
| 168 | * Implementation of the comparable interface. | |
| 169 | * | |
| 170 | * @param vs the VulnerableSoftware to compare | |
| 171 | * @return an integer indicating the ordering of the two objects | |
| 172 | */ | |
| 173 | @Override | |
| 174 | public int compareTo(VulnerableSoftware vs) { | |
| 175 | 0 | int result = 0; |
| 176 | 0 | final String[] left = this.getName().split(":"); |
| 177 | 0 | final String[] right = vs.getName().split(":"); |
| 178 | 0 | final int max = (left.length <= right.length) ? left.length : right.length; |
| 179 | 0 | if (max > 0) { |
| 180 | 0 | for (int i = 0; result == 0 && i < max; i++) { |
| 181 | 0 | final String[] subLeft = left[i].split("\\."); |
| 182 | 0 | final String[] subRight = right[i].split("\\."); |
| 183 | 0 | final int subMax = (subLeft.length <= subRight.length) ? subLeft.length : subRight.length; |
| 184 | 0 | if (subMax > 0) { |
| 185 | 0 | for (int x = 0; result == 0 && x < subMax; x++) { |
| 186 | 0 | if (isPositiveInteger(subLeft[x]) && isPositiveInteger(subRight[x])) { |
| 187 | try { | |
| 188 | 0 | result = Long.valueOf(subLeft[x]).compareTo(Long.valueOf(subRight[x])); |
| 189 | // final long iLeft = Long.parseLong(subLeft[x]); | |
| 190 | // final long iRight = Long.parseLong(subRight[x]); | |
| 191 | // if (iLeft != iRight) { | |
| 192 | // if (iLeft > iRight) { | |
| 193 | // result = 2; | |
| 194 | // } else { | |
| 195 | // result = -2; | |
| 196 | // } | |
| 197 | // } | |
| 198 | 0 | } catch (NumberFormatException ex) { |
| 199 | //ignore the exception - they obviously aren't numbers | |
| 200 | 0 | if (!subLeft[x].equalsIgnoreCase(subRight[x])) { |
| 201 | 0 | result = subLeft[x].compareToIgnoreCase(subRight[x]); |
| 202 | } | |
| 203 | 0 | } |
| 204 | } else { | |
| 205 | 0 | result = subLeft[x].compareToIgnoreCase(subRight[x]); |
| 206 | } | |
| 207 | } | |
| 208 | 0 | if (result == 0) { |
| 209 | 0 | if (subLeft.length > subRight.length) { |
| 210 | 0 | result = 2; |
| 211 | } | |
| 212 | 0 | if (subRight.length > subLeft.length) { |
| 213 | 0 | result = -2; |
| 214 | } | |
| 215 | } | |
| 216 | } else { | |
| 217 | 0 | result = left[i].compareToIgnoreCase(right[i]); |
| 218 | } | |
| 219 | } | |
| 220 | 0 | if (result == 0) { |
| 221 | 0 | if (left.length > right.length) { |
| 222 | 0 | result = 2; |
| 223 | } | |
| 224 | 0 | if (right.length > left.length) { |
| 225 | 0 | result = -2; |
| 226 | } | |
| 227 | } | |
| 228 | } else { | |
| 229 | 0 | result = this.getName().compareToIgnoreCase(vs.getName()); |
| 230 | } | |
| 231 | 0 | return result; |
| 232 | } | |
| 233 | ||
| 234 | /** | |
| 235 | * Determines if the string passed in is a positive integer. | |
| 236 | * | |
| 237 | * @param str the string to test | |
| 238 | * @return true if the string only contains 0-9, otherwise false. | |
| 239 | */ | |
| 240 | private static boolean isPositiveInteger(final String str) { | |
| 241 | 0 | if (str == null || str.isEmpty()) { |
| 242 | 0 | return false; |
| 243 | } | |
| 244 | 0 | for (int i = 0; i < str.length(); i++) { |
| 245 | 0 | final char c = str.charAt(i); |
| 246 | 0 | if (c < '0' || c > '9') { |
| 247 | 0 | return false; |
| 248 | } | |
| 249 | } | |
| 250 | 0 | return true; |
| 251 | } | |
| 252 | /** | |
| 253 | * The name of the cpe. | |
| 254 | */ | |
| 255 | private String name; | |
| 256 | ||
| 257 | /** | |
| 258 | * Get the value of name. | |
| 259 | * | |
| 260 | * @return the value of name | |
| 261 | */ | |
| 262 | public String getName() { | |
| 263 | return name; | |
| 264 | } | |
| 265 | ||
| 266 | /** | |
| 267 | * Set the value of name. | |
| 268 | * | |
| 269 | * @param name new value of name | |
| 270 | */ | |
| 271 | public void setName(String name) { | |
| 272 | this.name = name; | |
| 273 | } | |
| 274 | /** | |
| 275 | * The product version number. | |
| 276 | */ | |
| 277 | private String version; | |
| 278 | ||
| 279 | /** | |
| 280 | * Get the value of version. | |
| 281 | * | |
| 282 | * @return the value of version | |
| 283 | */ | |
| 284 | public String getVersion() { | |
| 285 | return version; | |
| 286 | } | |
| 287 | ||
| 288 | /** | |
| 289 | * Set the value of version. | |
| 290 | * | |
| 291 | * @param version new value of version | |
| 292 | */ | |
| 293 | public void setVersion(String version) { | |
| 294 | this.version = version; | |
| 295 | } | |
| 296 | /** | |
| 297 | * The product revision version. | |
| 298 | */ | |
| 299 | private String revision; | |
| 300 | ||
| 301 | /** | |
| 302 | * Get the value of revision. | |
| 303 | * | |
| 304 | * @return the value of revision | |
| 305 | */ | |
| 306 | public String getRevision() { | |
| 307 | return revision; | |
| 308 | } | |
| 309 | ||
| 310 | /** | |
| 311 | * Set the value of revision. | |
| 312 | * | |
| 313 | * @param revision new value of revision | |
| 314 | */ | |
| 315 | public void setRevision(String revision) { | |
| 316 | this.revision = revision; | |
| 317 | } | |
| 318 | /** | |
| 319 | * The product edition. | |
| 320 | */ | |
| 321 | private String edition; | |
| 322 | ||
| 323 | /** | |
| 324 | * Get the value of edition. | |
| 325 | * | |
| 326 | * @return the value of edition | |
| 327 | */ | |
| 328 | public String getEdition() { | |
| 329 | return edition; | |
| 330 | } | |
| 331 | ||
| 332 | /** | |
| 333 | * Set the value of edition. | |
| 334 | * | |
| 335 | * @param edition new value of edition | |
| 336 | */ | |
| 337 | public void setEdition(String edition) { | |
| 338 | this.edition = edition; | |
| 339 | } | |
| 340 | } |