|
| 1 | +/** Provides XML definitions related to the `org.apache.commons` package. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.dataflow.RangeUtils |
| 5 | +private import semmle.code.java.security.XmlParsers |
| 6 | + |
| 7 | +/** |
| 8 | + * The classes `org.apache.commons.digester3.Digester`, `org.apache.commons.digester.Digester` or `org.apache.tomcat.util.digester.Digester`. |
| 9 | + */ |
| 10 | +private class Digester extends RefType { |
| 11 | + Digester() { |
| 12 | + this.hasQualifiedName([ |
| 13 | + "org.apache.commons.digester3", "org.apache.commons.digester", |
| 14 | + "org.apache.tomcat.util.digester" |
| 15 | + ], "Digester") |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/** A call to `Digester.parse`. */ |
| 20 | +private class DigesterParse extends XmlParserCall { |
| 21 | + DigesterParse() { |
| 22 | + exists(Method m | |
| 23 | + this.getMethod() = m and |
| 24 | + m.getDeclaringType() instanceof Digester and |
| 25 | + m.hasName("parse") |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + override Expr getSink() { result = this.getArgument(0) } |
| 30 | + |
| 31 | + override predicate isSafe() { SafeDigesterFlow::flowToExpr(this.getQualifier()) } |
| 32 | +} |
| 33 | + |
| 34 | +/** A `ParserConfig` that is specific to `Digester`. */ |
| 35 | +private class DigesterConfig extends ParserConfig { |
| 36 | + DigesterConfig() { |
| 37 | + exists(Method m | |
| 38 | + m = this.getMethod() and |
| 39 | + m.getDeclaringType() instanceof Digester and |
| 40 | + m.hasName("setFeature") |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * A safely configured `Digester`. |
| 47 | + */ |
| 48 | +private class SafeDigester extends VarAccess { |
| 49 | + SafeDigester() { |
| 50 | + exists(Variable v | v = this.getVariable() | |
| 51 | + exists(DigesterConfig config | config.getQualifier() = v.getAnAccess() | |
| 52 | + config.enables(singleSafeConfig()) |
| 53 | + ) |
| 54 | + or |
| 55 | + exists(DigesterConfig config | config.getQualifier() = v.getAnAccess() | |
| 56 | + config |
| 57 | + .disables(any(ConstantStringExpr s | |
| 58 | + s.getStringValue() = "http://xml.org/sax/features/external-general-entities" |
| 59 | + )) |
| 60 | + ) and |
| 61 | + exists(DigesterConfig config | config.getQualifier() = v.getAnAccess() | |
| 62 | + config |
| 63 | + .disables(any(ConstantStringExpr s | |
| 64 | + s.getStringValue() = "http://xml.org/sax/features/external-parameter-entities" |
| 65 | + )) |
| 66 | + ) and |
| 67 | + exists(DigesterConfig config | config.getQualifier() = v.getAnAccess() | |
| 68 | + config |
| 69 | + .disables(any(ConstantStringExpr s | |
| 70 | + s.getStringValue() = |
| 71 | + "http://apache.org/xml/features/nonvalidating/load-external-dtd" |
| 72 | + )) |
| 73 | + ) |
| 74 | + ) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +private module SafeDigesterFlowConfig implements DataFlow::ConfigSig { |
| 79 | + predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeDigester } |
| 80 | + |
| 81 | + predicate isSink(DataFlow::Node sink) { |
| 82 | + exists(MethodAccess ma | |
| 83 | + sink.asExpr() = ma.getQualifier() and ma.getMethod().getDeclaringType() instanceof Digester |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + int fieldFlowBranchLimit() { result = 0 } |
| 88 | +} |
| 89 | + |
| 90 | +private module SafeDigesterFlow = DataFlow::Global<SafeDigesterFlowConfig>; |
0 commit comments