|
| 1 | +/** Provides classes and predicates to reason about Insecure Basic Authentication vulnerabilities. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.DataFlow |
| 5 | +import semmle.code.java.dataflow.TaintTracking |
| 6 | +import semmle.code.java.security.HttpsUrls |
| 7 | + |
| 8 | +/** |
| 9 | + * A source that represents HTTP URLs. |
| 10 | + * Extend this class to add your own Insecure Basic Authentication sources. |
| 11 | + */ |
| 12 | +abstract class InsecureBasicAuthSource extends DataFlow::Node { } |
| 13 | + |
| 14 | +/** A default source representing HTTP strings, URLs or URIs. */ |
| 15 | +private class DefaultInsecureBasicAuthSource extends InsecureBasicAuthSource { |
| 16 | + DefaultInsecureBasicAuthSource() { this.asExpr() instanceof HttpStringLiteral } |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * A sink that represents a method that sets Basic Authentication. |
| 21 | + * Extend this class to add your own Insecure Basic Authentication sinks. |
| 22 | + */ |
| 23 | +abstract class InsecureBasicAuthSink extends DataFlow::Node { } |
| 24 | + |
| 25 | +/** A default sink representing methods that set an Authorization header. */ |
| 26 | +private class DefaultInsecureBasicAuthSink extends InsecureBasicAuthSink { |
| 27 | + DefaultInsecureBasicAuthSink() { |
| 28 | + exists(MethodAccess ma | |
| 29 | + ma.getMethod().hasName("addHeader") or |
| 30 | + ma.getMethod().hasName("setHeader") or |
| 31 | + ma.getMethod().hasName("setRequestProperty") |
| 32 | + | |
| 33 | + this.asExpr() = ma.getQualifier() and |
| 34 | + ma.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "Authorization" and |
| 35 | + TaintTracking::localExprTaint(any(BasicAuthString b), ma.getArgument(1)) |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * String pattern of basic authentication. |
| 42 | + */ |
| 43 | +private class BasicAuthString extends StringLiteral { |
| 44 | + BasicAuthString() { exists(string s | this.getRepresentedString() = s | s.matches("Basic %")) } |
| 45 | +} |
0 commit comments