|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.python.checks; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Optional; |
| 22 | +import org.sonar.check.Rule; |
| 23 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 24 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 25 | +import org.sonar.plugins.python.api.symbols.v2.SymbolV2; |
| 26 | +import org.sonar.plugins.python.api.symbols.v2.UsageV2; |
| 27 | +import org.sonar.plugins.python.api.tree.BaseTreeVisitor; |
| 28 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 29 | +import org.sonar.plugins.python.api.tree.Decorator; |
| 30 | +import org.sonar.plugins.python.api.tree.Expression; |
| 31 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 32 | +import org.sonar.plugins.python.api.tree.Name; |
| 33 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 34 | +import org.sonar.plugins.python.api.tree.ReturnStatement; |
| 35 | +import org.sonar.plugins.python.api.tree.Tree; |
| 36 | +import org.sonar.plugins.python.api.tree.Tuple; |
| 37 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatchers; |
| 38 | +import org.sonar.python.checks.utils.Expressions; |
| 39 | +import org.sonar.python.tree.TreeUtils; |
| 40 | + |
| 41 | +@Rule(key = "S6863") |
| 42 | +public class FlaskErrorHandlerStatusCheck extends PythonSubscriptionCheck { |
| 43 | + |
| 44 | + private static final String MESSAGE = "Specify an explicit HTTP status code for this error handler."; |
| 45 | + |
| 46 | + private static final String FLASK_APP_ERRORHANDLER_FQN = "flask.app.Flask.errorhandler"; |
| 47 | + private static final String BLUEPRINT_ERRORHANDLER_FQN = "flask.blueprints.Blueprint.errorhandler"; |
| 48 | + private static final String BLUEPRINT_APP_ERRORHANDLER_FQN = "flask.blueprints.Blueprint.app_errorhandler"; |
| 49 | + |
| 50 | + private static final String JSONIFY_FQN = "flask.json.jsonify"; |
| 51 | + private static final String RENDER_TEMPLATE_FQN = "flask.templating.render_template"; |
| 52 | + private static final String RENDER_TEMPLATE_STRING_FQN = "flask.templating.render_template_string"; |
| 53 | + private static final String MAKE_RESPONSE_FQN = "flask.helpers.make_response"; |
| 54 | + private static final String RESPONSE_FQN = "flask.wrappers.Response"; |
| 55 | + |
| 56 | + @Override |
| 57 | + public void initialize(Context context) { |
| 58 | + context.registerSyntaxNodeConsumer(Tree.Kind.FUNCDEF, FlaskErrorHandlerStatusCheck::checkErrorHandler); |
| 59 | + } |
| 60 | + |
| 61 | + private static void checkErrorHandler(SubscriptionContext ctx) { |
| 62 | + FunctionDef functionDef = (FunctionDef) ctx.syntaxNode(); |
| 63 | + if (!isErrorHandlerFunction(functionDef, ctx)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + checkFunctionReturns(ctx, functionDef); |
| 67 | + } |
| 68 | + |
| 69 | + private static void checkFunctionReturns(SubscriptionContext ctx, FunctionDef functionDef) { |
| 70 | + ReturnStatementVisitor visitor = new ReturnStatementVisitor(ctx); |
| 71 | + functionDef.body().accept(visitor); |
| 72 | + visitor.getProblematicReturns().forEach(returnStmt -> |
| 73 | + ctx.addIssue(returnStmt, MESSAGE) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private static boolean isErrorHandlerFunction(FunctionDef functionDef, SubscriptionContext ctx) { |
| 78 | + return functionDef.decorators().stream() |
| 79 | + .anyMatch(decorator -> isErrorHandlerDecorator(decorator, ctx)); |
| 80 | + } |
| 81 | + |
| 82 | + private static boolean isErrorHandlerDecorator(Decorator decorator, SubscriptionContext ctx) { |
| 83 | + Expression expression = decorator.expression(); |
| 84 | + if (!(expression instanceof CallExpression callExpr)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + return TypeMatchers.any( |
| 88 | + TypeMatchers.isType(FLASK_APP_ERRORHANDLER_FQN), |
| 89 | + TypeMatchers.isType(BLUEPRINT_ERRORHANDLER_FQN), |
| 90 | + TypeMatchers.isType(BLUEPRINT_APP_ERRORHANDLER_FQN) |
| 91 | + ).isTrueFor(callExpr.callee(), ctx); |
| 92 | + } |
| 93 | + |
| 94 | + private static class ReturnStatementVisitor extends BaseTreeVisitor { |
| 95 | + private final SubscriptionContext ctx; |
| 96 | + private final List<ReturnStatement> problematicReturns = new ArrayList<>(); |
| 97 | + |
| 98 | + public ReturnStatementVisitor(SubscriptionContext ctx) { |
| 99 | + this.ctx = ctx; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void visitReturnStatement(ReturnStatement returnStatement) { |
| 104 | + List<Expression> expressions = returnStatement.expressions(); |
| 105 | + |
| 106 | + if (expressions.isEmpty()) { |
| 107 | + problematicReturns.add(returnStatement); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (expressions.size() >= 2) { |
| 112 | + // Has status code |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + Expression returnValue = expressions.get(0); |
| 117 | + |
| 118 | + checkReturnedValue(returnStatement, returnValue); |
| 119 | + } |
| 120 | + |
| 121 | + private void checkReturnedValue(ReturnStatement returnStatement, Expression returnValue) { |
| 122 | + if (returnValue.is(Tree.Kind.NAME)) { |
| 123 | + Name nameExpr = (Name) returnValue; |
| 124 | + |
| 125 | + // Check if status_code is set on this variable via attribute assignment |
| 126 | + if (hasStatusCodeSet(nameExpr)) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + Optional<Expression> assignedValue = Expressions.singleAssignedNonNameValue(nameExpr); |
| 131 | + if (assignedValue.isEmpty()) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + returnValue = assignedValue.get(); |
| 136 | + if (returnValue instanceof Tuple tuple && tuple.elements().size() >= 2) { |
| 137 | + return; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (returnValue instanceof CallExpression callExpr) { |
| 142 | + if (isProblematicFlaskFunction(callExpr)) { |
| 143 | + problematicReturns.add(returnStatement); |
| 144 | + } |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + problematicReturns.add(returnStatement); |
| 149 | + } |
| 150 | + |
| 151 | + private boolean isProblematicFlaskFunction(CallExpression callExpr) { |
| 152 | + // Functions that don't create proper Response objects |
| 153 | + if (TypeMatchers.any( |
| 154 | + TypeMatchers.isType(JSONIFY_FQN), |
| 155 | + TypeMatchers.isType(RENDER_TEMPLATE_FQN), |
| 156 | + TypeMatchers.isType(RENDER_TEMPLATE_STRING_FQN) |
| 157 | + ).isTrueFor(callExpr.callee(), ctx)) { |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + // make_response and Response can have status as second positional arg or 'status' kwarg |
| 162 | + if (TypeMatchers.any( |
| 163 | + TypeMatchers.isType(MAKE_RESPONSE_FQN), |
| 164 | + TypeMatchers.isType(RESPONSE_FQN) |
| 165 | + ).isTrueFor(callExpr.callee(), ctx)) { |
| 166 | + return TreeUtils.nthArgumentOrKeyword(1, "status", callExpr.arguments()) == null; |
| 167 | + } |
| 168 | + |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void visitFunctionDef(FunctionDef pyFunctionDefTree) { |
| 174 | + // Don't visit nested functions |
| 175 | + } |
| 176 | + |
| 177 | + public List<ReturnStatement> getProblematicReturns() { |
| 178 | + return problematicReturns; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Check if status_code is set on the given variable via attribute assignment. |
| 183 | + * Example: response.status_code = 404 |
| 184 | + */ |
| 185 | + private static boolean hasStatusCodeSet(Name nameExpr) { |
| 186 | + SymbolV2 symbol = nameExpr.symbolV2(); |
| 187 | + if (symbol == null) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + return symbol.usages().stream() |
| 192 | + .map(UsageV2::tree) |
| 193 | + .filter(Name.class::isInstance) |
| 194 | + .map(Tree::parent) |
| 195 | + .filter(QualifiedExpression.class::isInstance) |
| 196 | + .map(QualifiedExpression.class::cast) |
| 197 | + .filter(qualifiedExpr -> "status_code".equals(qualifiedExpr.name().name())) |
| 198 | + .anyMatch(qualifiedExpr -> TreeUtils.firstAncestorOfKind(qualifiedExpr, Tree.Kind.ASSIGNMENT_STMT) != null); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments