|
| 1 | +package python |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + sitter "github.com/smacker/go-tree-sitter" |
| 7 | + "globstar.dev/analysis" |
| 8 | +) |
| 9 | + |
| 10 | +var FlaskFormatStringReturn *analysis.Analyzer = &analysis.Analyzer{ |
| 11 | + Name: "flask-format-string-return", |
| 12 | + Language: analysis.LangPy, |
| 13 | + Description: "Returning formatted strings directly from Flask routes creates cross-site scripting vulnerabilities when user input is incorporated without proper escaping. Attackers can inject malicious JavaScript that executes in users' browsers. Flask's template engine with `render_template()` automatically handles proper escaping to prevent these attacks.", |
| 14 | + Category: analysis.CategorySecurity, |
| 15 | + Severity: analysis.SeverityWarning, |
| 16 | + Run: checkFlaskFormatStringReturn, |
| 17 | +} |
| 18 | + |
| 19 | +func checkFlaskFormatStringReturn(pass *analysis.Pass) (interface{}, error) { |
| 20 | + taintDataMap := make(map[string]bool) |
| 21 | + intermVarMap := make(map[string]bool) |
| 22 | + |
| 23 | + // tainted variable from decorated function |
| 24 | + analysis.Preorder(pass, func(node *sitter.Node) { |
| 25 | + if node.Type() != "decorated_definition" { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + decoratorNode := node.NamedChild(0) |
| 30 | + if decoratorNode.Type() != "decorator" { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + callNode := decoratorNode.NamedChild(0) |
| 35 | + if callNode.Type() != "call" { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + funcNode := callNode.ChildByFieldName("function") |
| 40 | + if funcNode.Type() != "attribute" { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + funcAttr := funcNode.ChildByFieldName("attribute") |
| 45 | + if funcAttr.Type() != "identifier" && funcAttr.Content(pass.FileContext.Source) != "route" { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + defNode := node.ChildByFieldName("definition") |
| 50 | + if defNode.Type() != "function_definition" { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + paramNode := defNode.ChildByFieldName("parameters") |
| 55 | + if paramNode.Type() != "parameters" { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + params := getNamedChildren(paramNode, 0) |
| 60 | + |
| 61 | + for _, p := range params { |
| 62 | + taintDataMap[p.Content(pass.FileContext.Source)] = true |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + // variable tainted by request |
| 67 | + analysis.Preorder(pass, func(node *sitter.Node) { |
| 68 | + if node.Type() != "assignment" { |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + leftNode := node.ChildByFieldName("left") |
| 73 | + rightNode := node.ChildByFieldName("right") |
| 74 | + |
| 75 | + if rightNode == nil { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + if isRequestCall(rightNode, pass.FileContext.Source) { |
| 80 | + taintDataMap[leftNode.Content(pass.FileContext.Source)] = true |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + // variable of data formatted by tainted variable |
| 85 | + analysis.Preorder(pass, func(node *sitter.Node) { |
| 86 | + if node.Type() != "assignment" { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + leftNode := node.ChildByFieldName("left") |
| 91 | + rightNode := node.ChildByFieldName("right") |
| 92 | + |
| 93 | + if rightNode == nil { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + if isTaintFormatted(rightNode, pass.FileContext.Source, intermVarMap, taintDataMap) { |
| 98 | + intermVarMap[leftNode.Content(pass.FileContext.Source)] = true |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + // detection |
| 103 | + analysis.Preorder(pass, func(node *sitter.Node) { |
| 104 | + if node.Type() != "return_statement" { |
| 105 | + return |
| 106 | + } |
| 107 | + returnValNode := node.NamedChild(0) |
| 108 | + if isTaintFormatted(returnValNode, pass.FileContext.Source, intermVarMap, taintDataMap) { |
| 109 | + pass.Report(pass, node, "Flask route returns formatted string allowing XSS - use render_template() instead") |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + return nil, nil |
| 114 | +} |
| 115 | + |
| 116 | +func isTaintFormatted(node *sitter.Node, source []byte, intermVarMap, taintDataMap map[string]bool) bool { |
| 117 | + switch node.Type() { |
| 118 | + case "call": |
| 119 | + funcNode := node.ChildByFieldName("function") |
| 120 | + if funcNode.Type() != "attribute" { |
| 121 | + return false |
| 122 | + } |
| 123 | + if !strings.HasSuffix(funcNode.Content(source), ".format") { |
| 124 | + return false |
| 125 | + } |
| 126 | + |
| 127 | + funcArgsListNode := node.ChildByFieldName("arguments") |
| 128 | + if funcArgsListNode.Type() != "argument_list" { |
| 129 | + return false |
| 130 | + } |
| 131 | + |
| 132 | + argsNode := getNamedChildren(funcArgsListNode, 0) |
| 133 | + for _, arg := range argsNode { |
| 134 | + if isRequestCall(arg, source) || taintDataMap[arg.Content(source)] || intermVarMap[arg.Content(source)] { |
| 135 | + return true |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + case "binary_operator": |
| 140 | + leftNode := node.ChildByFieldName("left") |
| 141 | + rightNode := node.ChildByFieldName("right") |
| 142 | + |
| 143 | + if leftNode.Type() != "string" { |
| 144 | + return false |
| 145 | + } |
| 146 | + |
| 147 | + if rightNode.Type() == "identifier" { |
| 148 | + return isRequestCall(rightNode, source) || taintDataMap[rightNode.Content(source)] || intermVarMap[rightNode.Content(source)] |
| 149 | + } else if rightNode.Type() == "tuple" { |
| 150 | + tupleArgNodes := getNamedChildren(rightNode, 0) |
| 151 | + for _, targ := range tupleArgNodes { |
| 152 | + if isRequestCall(targ, source) || taintDataMap[targ.Content(source)] || intermVarMap[targ.Content(source)] { |
| 153 | + return true |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + case "string": |
| 159 | + stringChildNodes := getNamedChildren(node, 0) |
| 160 | + for _, child := range stringChildNodes { |
| 161 | + if child.Type() == "interpolation" { |
| 162 | + exprNode := child.NamedChild(0) |
| 163 | + if isRequestCall(exprNode, source) || taintDataMap[exprNode.Content(source)] || intermVarMap[exprNode.Content(source)] { |
| 164 | + return true |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + case "identifier": |
| 170 | + return intermVarMap[node.Content(source)] || taintDataMap[node.Content(source)] |
| 171 | + } |
| 172 | + |
| 173 | + return false |
| 174 | +} |
0 commit comments