|
| 1 | +import javascript |
| 2 | +import semmle.javascript.security.dataflow.RequestForgeryCustomizations |
| 3 | +import semmle.javascript.security.dataflow.UrlConcatenation |
| 4 | + |
| 5 | +class Configuration extends TaintTracking::Configuration { |
| 6 | + Configuration() { this = "SSRF" } |
| 7 | + |
| 8 | + override predicate isSource(DataFlow::Node source) { source instanceof RequestForgery::Source } |
| 9 | + |
| 10 | + override predicate isSink(DataFlow::Node sink) { sink instanceof RequestForgery::Sink } |
| 11 | + |
| 12 | + override predicate isSanitizer(DataFlow::Node node) { |
| 13 | + super.isSanitizer(node) or |
| 14 | + node instanceof RequestForgery::Sanitizer |
| 15 | + } |
| 16 | + |
| 17 | + private predicate hasSanitizingSubstring(DataFlow::Node nd) { |
| 18 | + nd.getStringValue().regexpMatch(".*[?#].*") |
| 19 | + or |
| 20 | + hasSanitizingSubstring(StringConcatenation::getAnOperand(nd)) |
| 21 | + or |
| 22 | + hasSanitizingSubstring(nd.getAPredecessor()) |
| 23 | + } |
| 24 | + |
| 25 | + private predicate strictSanitizingPrefixEdge(DataFlow::Node source, DataFlow::Node sink) { |
| 26 | + exists(DataFlow::Node operator, int n | |
| 27 | + StringConcatenation::taintStep(source, sink, operator, n) and |
| 28 | + hasSanitizingSubstring(StringConcatenation::getOperand(operator, [0 .. n - 1])) |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + override predicate isSanitizerEdge(DataFlow::Node source, DataFlow::Node sink) { |
| 33 | + strictSanitizingPrefixEdge(source, sink) |
| 34 | + } |
| 35 | + |
| 36 | + override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode nd) { |
| 37 | + nd instanceof IntegerCheck or |
| 38 | + nd instanceof ValidatorCheck or |
| 39 | + nd instanceof TernaryOperatorSanitizerGuard |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * This sanitizers models the next example: |
| 45 | + * let valid = req.params.id ? Number.isInteger(req.params.id) : false |
| 46 | + * if (valid) { sink(req.params.id) } |
| 47 | + * |
| 48 | + * This sanitizer models this way of using ternary operators, |
| 49 | + * when the sanitizer guard is used as any of the branches |
| 50 | + * instead of being used as the condition. |
| 51 | + * |
| 52 | + * This sanitizer sanitize the corresponding if statement branch. |
| 53 | + */ |
| 54 | +class TernaryOperatorSanitizer extends RequestForgery::Sanitizer { |
| 55 | + TernaryOperatorSanitizer() { |
| 56 | + exists( |
| 57 | + TaintTracking::SanitizerGuardNode guard, IfStmt ifStmt, DataFlow::Node taintedInput, |
| 58 | + boolean outcome, Stmt r, DataFlow::Node falseNode |
| 59 | + | |
| 60 | + ifStmt.getCondition().flow().getAPredecessor+() = guard and |
| 61 | + ifStmt.getCondition().flow().getAPredecessor+() = falseNode and |
| 62 | + falseNode.asExpr().(BooleanLiteral).mayHaveBooleanValue(false) and |
| 63 | + not ifStmt.getCondition() instanceof LogicalBinaryExpr and |
| 64 | + guard.sanitizes(outcome, taintedInput.asExpr()) and |
| 65 | + ( |
| 66 | + outcome = true and r = ifStmt.getThen() and not ifStmt.getCondition() instanceof LogNotExpr |
| 67 | + or |
| 68 | + outcome = false and r = ifStmt.getElse() and not ifStmt.getCondition() instanceof LogNotExpr |
| 69 | + or |
| 70 | + outcome = false and r = ifStmt.getThen() and ifStmt.getCondition() instanceof LogNotExpr |
| 71 | + or |
| 72 | + outcome = true and r = ifStmt.getElse() and ifStmt.getCondition() instanceof LogNotExpr |
| 73 | + ) and |
| 74 | + r.getFirstControlFlowNode() |
| 75 | + .getBasicBlock() |
| 76 | + .(ReachableBasicBlock) |
| 77 | + .dominates(this.getBasicBlock()) |
| 78 | + ) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * This sanitizer guard is another way of modeling the example from above |
| 84 | + * In this case: |
| 85 | + * let valid = req.params.id ? Number.isInteger(req.params.id) : false |
| 86 | + * if (!valid) { return } |
| 87 | + * sink(req.params.id) |
| 88 | + * |
| 89 | + * The previous sanitizer is not enough, |
| 90 | + * because we are sanitizing the entire if statement branch |
| 91 | + * but we need to sanitize the use of this variable from now on. |
| 92 | + * |
| 93 | + * Thats why we model this sanitizer guard which says that |
| 94 | + * the result of the ternary operator execution is a sanitizer guard. |
| 95 | + */ |
| 96 | +class TernaryOperatorSanitizerGuard extends TaintTracking::SanitizerGuardNode { |
| 97 | + TaintTracking::SanitizerGuardNode originalGuard; |
| 98 | + |
| 99 | + TernaryOperatorSanitizerGuard() { |
| 100 | + this.getAPredecessor+().asExpr().(BooleanLiteral).mayHaveBooleanValue(false) and |
| 101 | + this.getAPredecessor+() = originalGuard and |
| 102 | + not this.asExpr() instanceof LogicalBinaryExpr |
| 103 | + } |
| 104 | + |
| 105 | + override predicate sanitizes(boolean outcome, Expr e) { |
| 106 | + not this.asExpr() instanceof LogNotExpr and |
| 107 | + originalGuard.sanitizes(outcome, e) |
| 108 | + or |
| 109 | + exists(boolean originalOutcome | |
| 110 | + this.asExpr() instanceof LogNotExpr and |
| 111 | + originalGuard.sanitizes(originalOutcome, e) and |
| 112 | + ( |
| 113 | + originalOutcome = true and outcome = false |
| 114 | + or |
| 115 | + originalOutcome = false and outcome = true |
| 116 | + ) |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Number.isInteger is a sanitizer guard because a number can't be used to exploit a SSRF. |
| 123 | + */ |
| 124 | +class IntegerCheck extends TaintTracking::SanitizerGuardNode, DataFlow::CallNode { |
| 125 | + IntegerCheck() { this = DataFlow::globalVarRef("Number").getAMemberCall("isInteger") } |
| 126 | + |
| 127 | + override predicate sanitizes(boolean outcome, Expr e) { |
| 128 | + outcome = true and |
| 129 | + e = getArgument(0).asExpr() |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * ValidatorCheck identifies if exists a call to validator's library methods. |
| 135 | + * validator is a library which has a variety of input-validation functions. We are interesed in |
| 136 | + * checking that source is a number (any type of number) or an alphanumeric value. |
| 137 | + */ |
| 138 | +class ValidatorCheck extends TaintTracking::SanitizerGuardNode, DataFlow::CallNode { |
| 139 | + ValidatorCheck() { |
| 140 | + exists(DataFlow::SourceNode mod, string method | |
| 141 | + mod = DataFlow::moduleImport("validator") and |
| 142 | + this = mod.getAChainedMethodCall(method) and |
| 143 | + method in [ |
| 144 | + "isAlphanumeric", "isAlpha", "isDecimal", "isFloat", "isHexadecimal", "isHexColor", |
| 145 | + "isInt", "isNumeric", "isOctal", "isUUID" |
| 146 | + ] |
| 147 | + ) |
| 148 | + } |
| 149 | + |
| 150 | + override predicate sanitizes(boolean outcome, Expr e) { |
| 151 | + outcome = true and |
| 152 | + e = getArgument(0).asExpr() |
| 153 | + } |
| 154 | +} |
0 commit comments