Search in sources :

Example 1 with AssignmentStatement

use of org.sonar.plugins.python.api.tree.AssignmentStatement in project sonar-python by SonarSource.

the class OverwrittenCollectionEntryCheck method check.

private static void check(SubscriptionContext ctx, StatementList statementList) {
    Map<CollectionKey, List<CollectionWrite>> collectionWrites = new HashMap<>();
    for (Statement statement : statementList.statements()) {
        CollectionWrite write = null;
        if (statement.is(Kind.ASSIGNMENT_STMT)) {
            AssignmentStatement assignment = (AssignmentStatement) statement;
            Expression expression = lhs(assignment);
            write = collectionWrite(assignment, expression);
        }
        if (write != null) {
            collectionWrites.computeIfAbsent(write.collectionKey, k -> new ArrayList<>()).add(write);
        } else {
            reportOverwrites(ctx, collectionWrites);
            collectionWrites.clear();
        }
    }
    reportOverwrites(ctx, collectionWrites);
}
Also used : NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) HashMap(java.util.HashMap) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) ArrayList(java.util.ArrayList) TreeUtils(org.sonar.python.tree.TreeUtils) Kind(org.sonar.plugins.python.api.tree.Tree.Kind) IssueLocation(org.sonar.plugins.python.api.IssueLocation) Name(org.sonar.plugins.python.api.tree.Name) Map(java.util.Map) Statement(org.sonar.plugins.python.api.tree.Statement) Expression(org.sonar.plugins.python.api.tree.Expression) Nullable(javax.annotation.Nullable) StatementList(org.sonar.plugins.python.api.tree.StatementList) SliceItem(org.sonar.plugins.python.api.tree.SliceItem) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) Token(org.sonar.plugins.python.api.tree.Token) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) Collectors(java.util.stream.Collectors) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) Tree(org.sonar.plugins.python.api.tree.Tree) Rule(org.sonar.check.Rule) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.python.api.symbols.Symbol) HashMap(java.util.HashMap) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) Expression(org.sonar.plugins.python.api.tree.Expression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) Statement(org.sonar.plugins.python.api.tree.Statement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) StatementList(org.sonar.plugins.python.api.tree.StatementList) List(java.util.List)

Example 2 with AssignmentStatement

use of org.sonar.plugins.python.api.tree.AssignmentStatement in project sonar-python by SonarSource.

the class CsrfDisabledCheck method flaskWtfCsrfEnabledFalseCheck.

/**
 * Checks that <code>'WTF_CSRF_ENABLED'</code> setting is not switched off.
 */
private static void flaskWtfCsrfEnabledFalseCheck(SubscriptionContext subscriptionContext) {
    AssignmentStatement asgn = (AssignmentStatement) subscriptionContext.syntaxNode();
    // Checks that the left hand side is some kind of subscription of `something['WTF_CSRF_ENABLED']`
    // Does not check what `something` is - overtainting seems extremely unlikely in this case.
    boolean isWtfCsrfEnabledSubscription = asgn.lhsExpressions().stream().flatMap(exprList -> exprList.expressions().stream()).filter(expr -> expr.is(Tree.Kind.SUBSCRIPTION)).flatMap(s -> ((SubscriptionExpression) s).subscripts().expressions().stream()).anyMatch(isStringSatisfying(s -> "WTF_CSRF_ENABLED".equals(s) || "WTF_CSRF_CHECK_DEFAULT".equals(s)));
    if (isWtfCsrfEnabledSubscription && Expressions.isFalsy(asgn.assignedValue())) {
        subscriptionContext.addIssue(asgn.assignedValue(), MESSAGE);
    }
}
Also used : Arrays(java.util.Arrays) RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) ClassDef(org.sonar.plugins.python.api.tree.ClassDef) ArrayList(java.util.ArrayList) TreeUtils(org.sonar.python.tree.TreeUtils) HashSet(java.util.HashSet) Decorator(org.sonar.plugins.python.api.tree.Decorator) Locale(java.util.Locale) Name(org.sonar.plugins.python.api.tree.Name) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Expression(org.sonar.plugins.python.api.tree.Expression) Expressions(org.sonar.python.checks.Expressions) Usage(org.sonar.plugins.python.api.symbols.Usage) KeyValuePair(org.sonar.plugins.python.api.tree.KeyValuePair) Predicate(java.util.function.Predicate) Set(java.util.Set) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Collectors(java.util.stream.Collectors) ListLiteral(org.sonar.plugins.python.api.tree.ListLiteral) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) Optional(java.util.Optional) Tree(org.sonar.plugins.python.api.tree.Tree) Pattern(java.util.regex.Pattern) Rule(org.sonar.check.Rule) DictionaryLiteral(org.sonar.plugins.python.api.tree.DictionaryLiteral) Symbol(org.sonar.plugins.python.api.symbols.Symbol) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression)

Example 3 with AssignmentStatement

use of org.sonar.plugins.python.api.tree.AssignmentStatement in project sonar-python by SonarSource.

the class CsrfDisabledCheck method metaCheck.

/**
 * Detects <code>class Meta</code> withing <code>FlaskForm</code>-subclasses,
 * with <code>csrf</code> set to <code>False</code>.
 */
private static void metaCheck(SubscriptionContext subscriptionContext) {
    ClassDef classDef = (ClassDef) subscriptionContext.syntaxNode();
    if (!"Meta".equals(classDef.name().name())) {
        return;
    }
    boolean isWithinFlaskForm = Optional.ofNullable(TreeUtils.firstAncestorOfKind(classDef, Tree.Kind.CLASSDEF)).map(parentClassDef -> ((ClassDef) parentClassDef).name().symbol()).filter(s -> s.is(Symbol.Kind.CLASS)).map(ClassSymbol.class::cast).filter(parentClassSymbol -> parentClassSymbol.canBeOrExtend("flask_wtf.FlaskForm")).isPresent();
    if (!isWithinFlaskForm) {
        return;
    }
    classDef.body().statements().forEach(stmt -> {
        if (stmt.is(Tree.Kind.ASSIGNMENT_STMT)) {
            AssignmentStatement asgn = (AssignmentStatement) stmt;
            if (isLhsCalled("csrf").test(asgn) && Expressions.isFalsy(asgn.assignedValue())) {
                subscriptionContext.addIssue(asgn.assignedValue(), MESSAGE);
            }
        }
    });
}
Also used : Arrays(java.util.Arrays) RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) ClassDef(org.sonar.plugins.python.api.tree.ClassDef) ArrayList(java.util.ArrayList) TreeUtils(org.sonar.python.tree.TreeUtils) HashSet(java.util.HashSet) Decorator(org.sonar.plugins.python.api.tree.Decorator) Locale(java.util.Locale) Name(org.sonar.plugins.python.api.tree.Name) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Expression(org.sonar.plugins.python.api.tree.Expression) Expressions(org.sonar.python.checks.Expressions) Usage(org.sonar.plugins.python.api.symbols.Usage) KeyValuePair(org.sonar.plugins.python.api.tree.KeyValuePair) Predicate(java.util.function.Predicate) Set(java.util.Set) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Collectors(java.util.stream.Collectors) ListLiteral(org.sonar.plugins.python.api.tree.ListLiteral) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) Optional(java.util.Optional) Tree(org.sonar.plugins.python.api.tree.Tree) Pattern(java.util.regex.Pattern) Rule(org.sonar.check.Rule) DictionaryLiteral(org.sonar.plugins.python.api.tree.DictionaryLiteral) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassDef(org.sonar.plugins.python.api.tree.ClassDef) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol)

Example 4 with AssignmentStatement

use of org.sonar.plugins.python.api.tree.AssignmentStatement in project sonar-python by SonarSource.

the class DebugModeCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Kind.CALL_EXPR, ctx -> {
        CallExpression callExpression = (CallExpression) ctx.syntaxNode();
        List<Argument> arguments = callExpression.arguments();
        if (!(callExpression.callee() instanceof QualifiedExpression)) {
            return;
        }
        if ("django.conf.settings.configure".equals(getQualifiedName(callExpression)) && !arguments.isEmpty()) {
            arguments.stream().filter(DebugModeCheck::isDebugArgument).forEach(arg -> ctx.addIssue(arg, MESSAGE));
        }
    });
    context.registerSyntaxNodeConsumer(Kind.ASSIGNMENT_STMT, ctx -> {
        if (!settingFiles.contains(ctx.pythonFile().fileName())) {
            return;
        }
        AssignmentStatement assignmentStatementTree = (AssignmentStatement) ctx.syntaxNode();
        for (ExpressionList lhsExpression : assignmentStatementTree.lhsExpressions()) {
            boolean isDebugProperties = lhsExpression.expressions().stream().anyMatch(DebugModeCheck::isDebugIdentifier);
            if (isDebugProperties && isTrueLiteral(assignmentStatementTree.assignedValue())) {
                ctx.addIssue(assignmentStatementTree, MESSAGE);
            }
        }
    });
}
Also used : RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) Argument(org.sonar.plugins.python.api.tree.Argument) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) ExpressionList(org.sonar.plugins.python.api.tree.ExpressionList)

Example 5 with AssignmentStatement

use of org.sonar.plugins.python.api.tree.AssignmentStatement in project sonar-python by SonarSource.

the class ClearTextProtocolsCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.STRING_ELEMENT, ctx -> {
        Tree node = ctx.syntaxNode();
        String value = Expressions.unescape((StringElement) node);
        unsafeProtocol(value).map(protocol -> protocol.substring(0, protocol.length() - 3)).ifPresent(protocol -> ctx.addIssue(node, message(protocol)));
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, ctx -> {
        Symbol symbol = ((CallExpression) ctx.syntaxNode()).calleeSymbol();
        isUnsafeLib(symbol).ifPresent(protocol -> ctx.addIssue(ctx.syntaxNode(), message(protocol)));
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.ASSIGNMENT_STMT, ctx -> handleAssignmentStatement((AssignmentStatement) ctx.syntaxNode(), ctx));
}
Also used : Arrays(java.util.Arrays) StringElement(org.sonar.plugins.python.api.tree.StringElement) URISyntaxException(java.net.URISyntaxException) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) HashMap(java.util.HashMap) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) TreeUtils(org.sonar.python.tree.TreeUtils) List(java.util.List) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Map(java.util.Map) Optional(java.util.Optional) Expression(org.sonar.plugins.python.api.tree.Expression) Tree(org.sonar.plugins.python.api.tree.Tree) URI(java.net.URI) Pattern(java.util.regex.Pattern) Rule(org.sonar.check.Rule) Nullable(javax.annotation.Nullable) Symbol(org.sonar.plugins.python.api.symbols.Symbol) Expressions(org.sonar.python.checks.Expressions) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) Tree(org.sonar.plugins.python.api.tree.Tree) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Aggregations

AssignmentStatement (org.sonar.plugins.python.api.tree.AssignmentStatement)32 Expression (org.sonar.plugins.python.api.tree.Expression)21 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)18 Symbol (org.sonar.plugins.python.api.symbols.Symbol)15 Name (org.sonar.plugins.python.api.tree.Name)13 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)12 Tree (org.sonar.plugins.python.api.tree.Tree)12 RegularArgument (org.sonar.plugins.python.api.tree.RegularArgument)11 List (java.util.List)8 Optional (java.util.Optional)8 Rule (org.sonar.check.Rule)8 SubscriptionContext (org.sonar.plugins.python.api.SubscriptionContext)8 TreeUtils (org.sonar.python.tree.TreeUtils)8 HashMap (java.util.HashMap)7 PythonSubscriptionCheck (org.sonar.plugins.python.api.PythonSubscriptionCheck)7 Usage (org.sonar.plugins.python.api.symbols.Usage)7 ExpressionList (org.sonar.plugins.python.api.tree.ExpressionList)7 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6