Search in sources :

Example 1 with ASTStandardCondition

use of net.sourceforge.pmd.lang.apex.ast.ASTStandardCondition in project pmd by pmd.

the class ApexSOQLInjectionRule method reportStrings.

private void reportStrings(ASTMethodCallExpression m, Object data) {
    final HashSet<ASTVariableExpression> setOfSafeVars = new HashSet<>();
    final List<ASTStandardCondition> conditions = m.findDescendantsOfType(ASTStandardCondition.class);
    for (ASTStandardCondition c : conditions) {
        List<ASTVariableExpression> vars = c.findDescendantsOfType(ASTVariableExpression.class);
        setOfSafeVars.addAll(vars);
    }
    final List<ASTBinaryExpression> binaryExpr = m.findChildrenOfType(ASTBinaryExpression.class);
    for (ASTBinaryExpression b : binaryExpr) {
        List<ASTVariableExpression> vars = b.findDescendantsOfType(ASTVariableExpression.class);
        for (ASTVariableExpression v : vars) {
            String fqName = Helper.getFQVariableName(v);
            if (selectContainingVariables.containsKey(fqName)) {
                boolean isLiteral = selectContainingVariables.get(fqName);
                if (isLiteral) {
                    continue;
                }
            }
            if (setOfSafeVars.contains(v) || safeVariables.contains(fqName)) {
                continue;
            }
            final ASTMethodCallExpression parentCall = v.getFirstParentOfType(ASTMethodCallExpression.class);
            boolean isSafeMethod = Helper.isMethodName(parentCall, STRING, ESCAPE_SINGLE_QUOTES) || Helper.isMethodName(parentCall, STRING, JOIN);
            if (!isSafeMethod) {
                addViolation(data, v);
            }
        }
    }
}
Also used : ASTVariableExpression(net.sourceforge.pmd.lang.apex.ast.ASTVariableExpression) ASTBinaryExpression(net.sourceforge.pmd.lang.apex.ast.ASTBinaryExpression) ASTStandardCondition(net.sourceforge.pmd.lang.apex.ast.ASTStandardCondition) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 ASTBinaryExpression (net.sourceforge.pmd.lang.apex.ast.ASTBinaryExpression)1 ASTMethodCallExpression (net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)1 ASTStandardCondition (net.sourceforge.pmd.lang.apex.ast.ASTStandardCondition)1 ASTVariableExpression (net.sourceforge.pmd.lang.apex.ast.ASTVariableExpression)1