2020import java .io .BufferedReader ;
2121import java .io .IOException ;
2222import java .io .InputStreamReader ;
23- import java .io .UnsupportedEncodingException ;
24- import java .net .URLEncoder ;
2523import java .nio .charset .StandardCharsets ;
2624import java .util .ArrayList ;
2725import java .util .Arrays ;
@@ -111,24 +109,7 @@ public class CPEAnalyzer extends AbstractAnalyzer {
111109 * alpha characters.
112110 */
113111 private static final String CLEANSE_NONALPHA_RX = "[^A-Za-z]*" ;
114- /**
115- * UTF-8 character set name.
116- */
117- private static final String UTF8 = StandardCharsets .UTF_8 .name ();
118- /**
119- * The URL to search the NVD CVE data at NIST. This is used by calling:
120- * <pre>String.format(NVD_SEARCH_URL, vendor, product, version);</pre>
121- */
122- public static final String NVD_SEARCH_URL = "https://nvd.nist.gov/vuln/search/results?form_type=Advanced&"
123- + "results_type=overview&search_type=all&cpe_vendor=cpe%%3A%%2F%%3A%1$s&cpe_product=cpe%%3A%%2F%%3A%1$s%%3A%2$s&"
124- + "cpe_version=cpe%%3A%%2F%%3A%1$s%%3A%2$s%%3A%3$s" ;
125112
126- /**
127- * The URL to search the NVD CVE data at NIST. This is used by calling:
128- * <pre>String.format(NVD_SEARCH_URL, vendor, product);</pre>
129- */
130- public static final String NVD_SEARCH_BROAD_URL = "https://nvd.nist.gov/vuln/search/results?form_type=Advanced&"
131- + "results_type=overview&search_type=all&cpe_vendor=cpe%%3A%%2F%%3A%1$s&cpe_product=cpe%%3A%%2F%%3A%1$s%%3A%2$s" ;
132113 /**
133114 * The CPE in memory index.
134115 */
@@ -806,12 +787,11 @@ protected void analyzeDependency(Dependency dependency, Engine engine) throws An
806787 * analysis
807788 * @return <code>true</code> if an identifier was added to the dependency;
808789 * otherwise <code>false</code>
809- * @throws UnsupportedEncodingException is thrown if UTF-8 is not supported
810790 * @throws AnalysisException thrown if the suppression rules failed
811791 */
812792 @ SuppressWarnings ("StringSplitter" )
813793 protected boolean determineIdentifiers (Dependency dependency , String vendor , String product ,
814- Confidence currentConfidence ) throws UnsupportedEncodingException , AnalysisException {
794+ Confidence currentConfidence ) throws AnalysisException {
815795
816796 final CpeBuilder cpeBuilder = new CpeBuilder ();
817797
@@ -864,8 +844,7 @@ protected boolean determineIdentifiers(Dependency dependency, String vendor, Str
864844 dbVerUpdate = DependencyVersionUtil .parseVersion (vs .getVersion () + '.' + vs .getUpdate (), true );
865845 }
866846 if (dbVer == null ) { //special case, no version specified - everything is vulnerable
867- final String url = String .format (NVD_SEARCH_BROAD_URL , URLEncoder .encode (vs .getVendor (), UTF8 ),
868- URLEncoder .encode (vs .getProduct (), UTF8 ));
847+ final String url = CpeIdentifier .nvdProductSearchUrlFor (vs );
869848 final IdentifierMatch match = new IdentifierMatch (vs , url , IdentifierConfidence .BROAD_MATCH , conf );
870849 collected .add (match );
871850 } else if (evVer .equals (dbVer )) {
@@ -875,8 +854,7 @@ protected boolean determineIdentifiers(Dependency dependency, String vendor, Str
875854 bestGuessConf = conf ;
876855 bestGuess = dbVer ;
877856 bestGuessUpdate = evBaseVerUpdate ;
878- bestGuessURL = String .format (NVD_SEARCH_URL , URLEncoder .encode (vs .getVendor (), UTF8 ),
879- URLEncoder .encode (vs .getProduct (), UTF8 ), URLEncoder .encode (vs .getVersion (), UTF8 ));
857+ bestGuessURL = CpeIdentifier .nvdSearchUrlFor (vs );
880858 } else if (dbVerUpdate != null && evVer .getVersionParts ().size () <= dbVerUpdate .getVersionParts ().size ()
881859 && evVer .matchesAtLeastThreeLevels (dbVerUpdate )) {
882860 if (bestGuessConf == null || bestGuessConf .compareTo (conf ) > 0 ) {
@@ -983,14 +961,12 @@ protected boolean determineIdentifiers(Dependency dependency, String vendor, Str
983961 * @param updateVersion the update version
984962 * @param conf the current confidence
985963 * @param collected a reference to the collected identifiers
986- * @throws UnsupportedEncodingException thrown if UTF-8 is not supported
987964 */
988965 private void addExactMatch (Cpe vs , String updateVersion , Confidence conf ,
989- final Set <IdentifierMatch > collected ) throws UnsupportedEncodingException {
966+ final Set <IdentifierMatch > collected ) {
990967
991968 final CpeBuilder cpeBuilder = new CpeBuilder ();
992- final String url = String .format (NVD_SEARCH_URL , URLEncoder .encode (vs .getVendor (), UTF8 ),
993- URLEncoder .encode (vs .getProduct (), UTF8 ), URLEncoder .encode (vs .getVersion (), UTF8 ));
969+ final String url = CpeIdentifier .nvdSearchUrlFor (vs );
994970 Cpe useCpe ;
995971 if (updateVersion != null && "*" .equals (vs .getUpdate ())) {
996972 try {
@@ -1022,13 +998,11 @@ private void addExactMatch(Cpe vs, String updateVersion, Confidence conf,
1022998 * @param collected a reference to the identifiers matched
1023999 * @throws AnalysisException thrown if aliens attacked and valid input could
10241000 * not be used to construct a CPE
1025- * @throws UnsupportedEncodingException thrown if run on a system that
1026- * doesn't support UTF-8
10271001 */
10281002 private void considerDependencyVersion (Dependency dependency ,
10291003 String vendor , String product , Confidence confidence ,
10301004 final Set <IdentifierMatch > collected )
1031- throws AnalysisException , UnsupportedEncodingException {
1005+ throws AnalysisException {
10321006
10331007 if (dependency .getVersion () != null && !dependency .getVersion ().isEmpty ()) {
10341008 final CpeBuilder cpeBuilder = new CpeBuilder ();
@@ -1051,8 +1025,7 @@ private void considerDependencyVersion(Dependency dependency,
10511025 addVersionAndUpdate (depVersion , cpeBuilder );
10521026 try {
10531027 final Cpe depCpe = cpeBuilder .build ();
1054- final String url = String .format (NVD_SEARCH_URL , URLEncoder .encode (vendor , UTF8 ),
1055- URLEncoder .encode (product , UTF8 ), URLEncoder .encode (depCpe .getVersion (), UTF8 ));
1028+ final String url = CpeIdentifier .nvdSearchUrlFor (vendor , product , depCpe .getVersion ());
10561029 final IdentifierMatch match = new IdentifierMatch (depCpe , url , IdentifierConfidence .EXACT_MATCH , confidence );
10571030 collected .add (match );
10581031 } catch (CpeValidationException ex ) {
0 commit comments