Skip to content

Commit 63f90c3

Browse files
committed
fix: prevent rogue base suppression files
1 parent 94c5084 commit 63f90c3

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

core/src/main/java/org/owasp/dependencycheck/analyzer/AbstractSuppressionAnalyzer.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.nio.file.Path;
2727
import java.nio.file.StandardCopyOption;
2828
import java.util.ArrayList;
29+
import java.util.Iterator;
2930
import java.util.List;
3031
import java.util.Set;
3132
import java.util.regex.Pattern;
@@ -188,23 +189,43 @@ private void loadSuppressionBaseData(final Engine engine) throws SuppressionPars
188189
}
189190

190191
/**
191-
* Loads all the base suppression rules packaged with the application.
192+
* Loads the base suppression rules packaged with the application.
192193
*
193194
* @param parser The suppression parser to use
194195
* @param engine a reference the dependency-check engine
195196
* @throws SuppressionParseException thrown if the XML cannot be parsed.
196197
*/
197198
private void loadPackagedSuppressionBaseData(final SuppressionParser parser, final Engine engine) throws SuppressionParseException {
198-
final List<SuppressionRule> ruleList;
199-
try (InputStream in = FileUtils.getResourceAsStream(BASE_SUPPRESSION_FILE)) {
200-
if (in == null) {
201-
throw new SuppressionParseException("Suppression rules `" + BASE_SUPPRESSION_FILE + "` could not be found");
199+
List<SuppressionRule> ruleList = null;
200+
Iterator<URL> urls = null;
201+
try {
202+
urls = FileUtils.getResources(BASE_SUPPRESSION_FILE);
203+
} catch (IOException e) {
204+
LOGGER.warn("Base suppression rules `{}}` could not be loaded; {}", BASE_SUPPRESSION_FILE, e.getMessage());
205+
return;
206+
}
207+
URL loc = AbstractSuppressionAnalyzer.class.getProtectionDomain().getCodeSource().getLocation();
208+
String jarPath = loc.getFile();
209+
URL validUrl = null;
210+
while (urls.hasNext()) {
211+
URL url = urls.next();
212+
String path = url.toString();
213+
if (path.equals("jar:" + jarPath + "!/dependencycheck-base-suppression.xml")) {
214+
validUrl = url;
215+
break;
202216
}
203-
ruleList = parser.parseSuppressionRules(in);
204-
} catch (SAXException | IOException ex) {
205-
throw new SuppressionParseException("Unable to parse the base suppression data file", ex);
206217
}
207-
if (!ruleList.isEmpty()) {
218+
if (validUrl != null) {
219+
try (InputStream in = validUrl.openStream()) {
220+
if (in == null) {
221+
throw new SuppressionParseException("Suppression rules `" + BASE_SUPPRESSION_FILE + "` could not be found");
222+
}
223+
ruleList = parser.parseSuppressionRules(in);
224+
} catch (SAXException | IOException ex) {
225+
throw new SuppressionParseException("Unable to parse the base suppression data file", ex);
226+
}
227+
}
228+
if (ruleList != null && !ruleList.isEmpty()) {
208229
if (engine.hasObject(SUPPRESSION_OBJECT_KEY)) {
209230
@SuppressWarnings("unchecked")
210231
final List<SuppressionRule> rules = (List<SuppressionRule>) engine.getObject(SUPPRESSION_OBJECT_KEY);

utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
2929
import java.util.Comparator;
30+
import java.util.Iterator;
3031
import java.util.UUID;
3132
import java.util.stream.Stream;
3233

@@ -173,6 +174,20 @@ public static InputStream getResourceAsStream(@NotNull String resource) throws F
173174
return inputStream;
174175
}
175176

177+
178+
/** Gets the {@link java.util.Iterator<java.net.URL>} for this resource
179+
*
180+
* @param resource path
181+
* @return iterator of each resource URL
182+
* @throws IOException if I/O error occurs
183+
*/
184+
public static Iterator<URL> getResources(@NotNull String resource) throws IOException {
185+
final ClassLoader classLoader = FileUtils.class.getClassLoader();
186+
return classLoader != null
187+
? classLoader.getResources(resource).asIterator()
188+
: ClassLoader.getSystemResources(resource).asIterator();
189+
}
190+
176191
/**
177192
* Returns a File object for the given resource. The resource is attempted
178193
* to be loaded from the class loader.

0 commit comments

Comments
 (0)