|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2023 SonarSource SA |
| 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 GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.checks.hotspots; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +import org.sonar.check.Rule; |
| 29 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 30 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 31 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 32 | +import org.sonar.plugins.python.api.tree.Argument; |
| 33 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 34 | +import org.sonar.plugins.python.api.tree.Expression; |
| 35 | +import org.sonar.plugins.python.api.tree.ExpressionList; |
| 36 | +import org.sonar.plugins.python.api.tree.ListLiteral; |
| 37 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 38 | +import org.sonar.plugins.python.api.tree.Tree; |
| 39 | +import org.sonar.plugins.python.api.tree.Tuple; |
| 40 | +import org.sonar.plugins.python.api.types.InferredType; |
| 41 | +import org.sonar.python.tree.NameImpl; |
| 42 | +import org.sonar.python.tree.TreeUtils; |
| 43 | + |
| 44 | +@Rule(key = "S6786") |
| 45 | +public class GraphQLIntrospectionCheck extends PythonSubscriptionCheck { |
| 46 | + |
| 47 | + private static final Set<String> GRAPHQL_VIEWS_FQNS = Set.of( |
| 48 | + "flask_graphql.GraphQLView.as_view", |
| 49 | + "graphql_server.flask.GraphQLView.as_view"); |
| 50 | + |
| 51 | + private static final Set<String> SAFE_VALIDATION_RULE_FQNS = Set.of( |
| 52 | + "graphene.validation.DisableIntrospection", |
| 53 | + "graphql.validation.NoSchemaIntrospectionCustomRule"); |
| 54 | + |
| 55 | + private static final String MESSAGE = "Disable introspection on this \"GraphQL\" server endpoint."; |
| 56 | + |
| 57 | + @Override |
| 58 | + public void initialize(Context context) { |
| 59 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, GraphQLIntrospectionCheck::checkGraphQLIntrospection); |
| 60 | + } |
| 61 | + |
| 62 | + private static void checkGraphQLIntrospection(SubscriptionContext ctx) { |
| 63 | + CallExpression callExpression = (CallExpression) ctx.syntaxNode(); |
| 64 | + Optional.ofNullable(callExpression.calleeSymbol()) |
| 65 | + .map(Symbol::fullyQualifiedName) |
| 66 | + .filter(GRAPHQL_VIEWS_FQNS::contains) |
| 67 | + .filter(fqn -> !hasSafeMiddlewares(callExpression.arguments())) |
| 68 | + .filter(fqn -> !hasSafeValidationRules(callExpression.arguments())) |
| 69 | + .ifPresent(fqn -> ctx.addIssue(callExpression.callee(), MESSAGE)); |
| 70 | + } |
| 71 | + |
| 72 | + private static boolean hasSafeMiddlewares(List<Argument> arguments) { |
| 73 | + RegularArgument argument = TreeUtils.argumentByKeyword("middleware", arguments); |
| 74 | + if (argument == null) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + return extractArgumentValues(argument) |
| 79 | + .map(values -> !values.isEmpty() && expressionsNameContainIntrospection(values)) |
| 80 | + .orElse(true); |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean hasSafeValidationRules(List<Argument> arguments) { |
| 84 | + RegularArgument argument = TreeUtils.argumentByKeyword("validation_rules", arguments); |
| 85 | + if (argument == null) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + return extractArgumentValues(argument) |
| 90 | + .map(values -> !values.isEmpty() && |
| 91 | + (expressionsNameContainIntrospection(values) || expressionsContainsSafeRuleFQN(values))) |
| 92 | + .orElse(true); |
| 93 | + } |
| 94 | + |
| 95 | + private static Optional<List<Expression>> extractArgumentValues(RegularArgument argument) { |
| 96 | + return Optional.of(argument) |
| 97 | + .map(RegularArgument::expression) |
| 98 | + .flatMap(GraphQLIntrospectionCheck::expressionsFromListOrTuple); |
| 99 | + } |
| 100 | + |
| 101 | + private static Optional<List<Expression>> expressionsFromListOrTuple(Expression expression) { |
| 102 | + return TreeUtils.toOptionalInstanceOf(ListLiteral.class, expression) |
| 103 | + .map(ListLiteral::elements) |
| 104 | + .map(ExpressionList::expressions) |
| 105 | + .or(() -> TreeUtils.toOptionalInstanceOf(Tuple.class, expression) |
| 106 | + .map(Tuple::elements)); |
| 107 | + } |
| 108 | + |
| 109 | + private static boolean expressionsNameContainIntrospection(List<Expression> expressions) { |
| 110 | + Stream<Optional<String>> expressionsNameAndType = Stream.concat(expressions.stream() |
| 111 | + .map(GraphQLIntrospectionCheck::nameFromIdentifierOrCallExpression), |
| 112 | + expressions.stream().map(GraphQLIntrospectionCheck::nameOfType)); |
| 113 | + |
| 114 | + return expressionsNameAndType |
| 115 | + .filter(Optional::isPresent) |
| 116 | + .map(Optional::get) |
| 117 | + .map(String::toUpperCase) |
| 118 | + .anyMatch(name -> name.contains("INTROSPECTION")); |
| 119 | + } |
| 120 | + |
| 121 | + private static boolean expressionsContainsSafeRuleFQN(List<Expression> expressions) { |
| 122 | + return expressions.stream() |
| 123 | + .map(TreeUtils::getSymbolFromTree) |
| 124 | + .filter(Optional::isPresent) |
| 125 | + .map(Optional::get) |
| 126 | + .map(Symbol::fullyQualifiedName) |
| 127 | + .filter(Objects::nonNull) |
| 128 | + .anyMatch(SAFE_VALIDATION_RULE_FQNS::contains); |
| 129 | + } |
| 130 | + |
| 131 | + private static Optional<String> nameOfType(Expression expression) { |
| 132 | + return TreeUtils.toOptionalInstanceOf(NameImpl.class, expression) |
| 133 | + .map(NameImpl::type) |
| 134 | + .map(InferredType::runtimeTypeSymbol) |
| 135 | + .map(Symbol::name); |
| 136 | + } |
| 137 | + |
| 138 | + private static Optional<String> nameFromIdentifierOrCallExpression(Expression expression) { |
| 139 | + return Optional.ofNullable(TreeUtils.nameFromExpression(expression)) |
| 140 | + .or(() -> TreeUtils.toOptionalInstanceOf(CallExpression.class, expression) |
| 141 | + .map(CallExpression::callee) |
| 142 | + .map(TreeUtils::nameFromExpression)); |
| 143 | + } |
| 144 | +} |
0 commit comments