|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * overly permissive CORS configurations, as well as |
| 4 | + * extension points for adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +import javascript |
| 8 | + |
| 9 | +/** Module containing sources, sinks, and sanitizers for overly permissive CORS configurations. */ |
| 10 | +module CorsPermissiveConfiguration { |
| 11 | + private newtype TFlowState = |
| 12 | + TTaint() or |
| 13 | + TPermissive() |
| 14 | + |
| 15 | + /** A flow state to associate with a tracked value. */ |
| 16 | + class FlowState extends TFlowState { |
| 17 | + /** Gets a string representation of this flow state. */ |
| 18 | + string toString() { |
| 19 | + this = TTaint() and result = "taint" |
| 20 | + or |
| 21 | + this = TPermissive() and result = "permissive" |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /** Predicates for working with flow states. */ |
| 26 | + module FlowState { |
| 27 | + /** A tainted value. */ |
| 28 | + FlowState taint() { result = TTaint() } |
| 29 | + |
| 30 | + /** A permissive value (true, null, or "*"). */ |
| 31 | + FlowState permissive() { result = TPermissive() } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * A data flow source for permissive CORS configuration. |
| 36 | + */ |
| 37 | + abstract class Source extends DataFlow::Node { } |
| 38 | + |
| 39 | + /** |
| 40 | + * A data flow sink for permissive CORS configuration. |
| 41 | + */ |
| 42 | + abstract class Sink extends DataFlow::Node { } |
| 43 | + |
| 44 | + /** |
| 45 | + * A sanitizer for permissive CORS configuration. |
| 46 | + */ |
| 47 | + abstract class Sanitizer extends DataFlow::Node { } |
| 48 | + |
| 49 | + /** |
| 50 | + * An active threat-model source, considered as a flow source. |
| 51 | + */ |
| 52 | + private class ActiveThreatModelSourceAsSource extends Source instanceof ActiveThreatModelSource { |
| 53 | + ActiveThreatModelSourceAsSource() { not this instanceof ClientSideRemoteFlowSource } |
| 54 | + } |
| 55 | + |
| 56 | + /** An overly permissive value for `origin` configuration. */ |
| 57 | + class PermissiveValue extends Source { |
| 58 | + PermissiveValue() { |
| 59 | + this.mayHaveBooleanValue(true) or |
| 60 | + this.asExpr() instanceof NullLiteral or |
| 61 | + this.mayHaveStringValue("*") |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * The value of cors origin when initializing the application. |
| 67 | + */ |
| 68 | + class CorsOriginSink extends Sink, DataFlow::ValueNode { |
| 69 | + CorsOriginSink() { this = ModelOutput::getASinkNode("cors-origin").asSink() } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * A sanitizer for CORS configurations where credentials are explicitly disabled. |
| 74 | + * When credentials are false, using "*" for origin is a legitimate pattern. |
| 75 | + */ |
| 76 | + private class CredentialsDisabledSanitizer extends Sanitizer { |
| 77 | + CredentialsDisabledSanitizer() { |
| 78 | + exists(DataFlow::SourceNode config, DataFlow::CallNode call | |
| 79 | + call.getArgument(0).getALocalSource() = config and |
| 80 | + this = config.getAPropertyWrite("origin").getRhs() and |
| 81 | + config.getAPropertyWrite("credentials").getRhs().mayHaveBooleanValue(false) |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments