|
| 1 | +/** |
| 2 | + * Provides default sources, sinks, sanitizers, and flow steps for |
| 3 | + * detecting insecure mass assignment, as well as extension points for adding your own. |
| 4 | + */ |
| 5 | + |
| 6 | +private import codeql.ruby.AST |
| 7 | +private import codeql.ruby.controlflow.CfgNodes |
| 8 | +private import codeql.ruby.DataFlow |
| 9 | +private import codeql.ruby.TaintTracking |
| 10 | +private import codeql.ruby.dataflow.RemoteFlowSources |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides default sources, sinks, sanitizers, and flow steps for |
| 14 | + * detecting insecure mass assignment, as well as extension points for adding your own. |
| 15 | + */ |
| 16 | +module MassAssignment { |
| 17 | + /** |
| 18 | + * A data flow source for user input used for mass assignment. |
| 19 | + */ |
| 20 | + abstract class Source extends DataFlow::Node { } |
| 21 | + |
| 22 | + /** |
| 23 | + * A data flow sink for user input used for mass assignment. |
| 24 | + */ |
| 25 | + abstract class Sink extends DataFlow::Node { } |
| 26 | + |
| 27 | + /** |
| 28 | + * A sanitizer for insecure mass assignment. |
| 29 | + */ |
| 30 | + abstract class Sanitizer extends DataFlow::Node { } |
| 31 | + |
| 32 | + /** |
| 33 | + * A call that permits arbitrary parameters to be used for mass assignment. |
| 34 | + */ |
| 35 | + abstract class MassPermit extends DataFlow::Node { |
| 36 | + /** Gets the argument for the parameters to be permitted. */ |
| 37 | + abstract DataFlow::Node getParamsArgument(); |
| 38 | + |
| 39 | + /** Gets the result node of the permitted parameters. */ |
| 40 | + abstract DataFlow::Node getPermittedParamsResult(); |
| 41 | + } |
| 42 | + |
| 43 | + private class RemoteSource extends Source instanceof RemoteFlowSource { } |
| 44 | + |
| 45 | + /** A call to `permit!`, which permits each key of its receiver. */ |
| 46 | + private class PermitBangCall extends MassPermit instanceof DataFlow::CallNode { |
| 47 | + PermitBangCall() { this.(DataFlow::CallNode).getMethodName() = "permit!" } |
| 48 | + |
| 49 | + override DataFlow::Node getParamsArgument() { result = this.(DataFlow::CallNode).getReceiver() } |
| 50 | + |
| 51 | + override DataFlow::Node getPermittedParamsResult() { |
| 52 | + result = this |
| 53 | + or |
| 54 | + result.(DataFlow::PostUpdateNode).getPreUpdateNode() = this.getParamsArgument() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** Holds if `h` is an empty hash or contains an empty hash at one if its (possibly nested) values. */ |
| 59 | + private predicate hasEmptyHash(ExprCfgNode e) { |
| 60 | + e instanceof ExprNodes::HashLiteralCfgNode and |
| 61 | + not exists(e.(ExprNodes::HashLiteralCfgNode).getAKeyValuePair()) |
| 62 | + or |
| 63 | + hasEmptyHash(e.(ExprNodes::HashLiteralCfgNode).getAKeyValuePair().getValue()) |
| 64 | + or |
| 65 | + hasEmptyHash(e.(ExprNodes::PairCfgNode).getValue()) |
| 66 | + or |
| 67 | + hasEmptyHash(e.(ExprNodes::ArrayLiteralCfgNode).getAnArgument()) |
| 68 | + } |
| 69 | + |
| 70 | + /** A call to `permit` that fully specifies the permitted parameters. */ |
| 71 | + private class PermitCallSanitizer extends Sanitizer, DataFlow::CallNode { |
| 72 | + PermitCallSanitizer() { |
| 73 | + this.getMethodName() = "permit" and |
| 74 | + not hasEmptyHash(this.getArgument(_).getExprNode()) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** A call to `permit` that uses an empty hash, which allows arbitrary keys to be specified. */ |
| 79 | + private class PermitCallMassPermit extends MassPermit instanceof DataFlow::CallNode { |
| 80 | + PermitCallMassPermit() { |
| 81 | + this.(DataFlow::CallNode).getMethodName() = "permit" and |
| 82 | + hasEmptyHash(this.(DataFlow::CallNode).getArgument(_).getExprNode()) |
| 83 | + } |
| 84 | + |
| 85 | + override DataFlow::Node getParamsArgument() { result = this.(DataFlow::CallNode).getReceiver() } |
| 86 | + |
| 87 | + override DataFlow::Node getPermittedParamsResult() { result = this } |
| 88 | + } |
| 89 | + |
| 90 | + /** A call to `to_unsafe_h`, which allows arbitrary parameter. */ |
| 91 | + private class ToUnsafeHashCall extends MassPermit instanceof DataFlow::CallNode { |
| 92 | + ToUnsafeHashCall() { this.(DataFlow::CallNode).getMethodName() = "to_unsafe_h" } |
| 93 | + |
| 94 | + override DataFlow::Node getParamsArgument() { result = this.(DataFlow::CallNode).getReceiver() } |
| 95 | + |
| 96 | + override DataFlow::Node getPermittedParamsResult() { result = this } |
| 97 | + } |
| 98 | +} |
0 commit comments