use of net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration in project pmd by pmd.
the class ApexCRUDViolationRule method checkForAccessibility.
private void checkForAccessibility(final ASTSoqlExpression node, Object data) {
final boolean isCount = node.getNode().getCanonicalQuery().startsWith("SELECT COUNT()");
final Set<String> typesFromSOQL = getTypesFromSOQLQuery(node);
final Set<ASTMethodCallExpression> prevCalls = getPreviousMethodCalls(node);
for (ASTMethodCallExpression prevCall : prevCalls) {
collectCRUDMethodLevelChecks(prevCall);
}
boolean isGetter = false;
String returnType = null;
final ASTMethod wrappingMethod = node.getFirstParentOfType(ASTMethod.class);
final ASTUserClass wrappingClass = node.getFirstParentOfType(ASTUserClass.class);
if (isCount || wrappingClass != null && Helper.isTestMethodOrClass(wrappingClass) || wrappingMethod != null && Helper.isTestMethodOrClass(wrappingMethod)) {
return;
}
if (wrappingMethod != null) {
isGetter = isMethodAGetter(wrappingMethod);
returnType = getReturnType(wrappingMethod);
}
final ASTVariableDeclaration variableDecl = node.getFirstParentOfType(ASTVariableDeclaration.class);
if (variableDecl != null) {
String type = variableDecl.getNode().getLocalInfo().getType().getApexName();
type = getSimpleType(type);
StringBuilder typeCheck = new StringBuilder().append(variableDecl.getNode().getDefiningType().getApexName()).append(":").append(type);
if (!isGetter) {
if (typesFromSOQL.isEmpty()) {
validateCRUDCheckPresent(node, data, ANY, typeCheck.toString());
} else {
for (String typeFromSOQL : typesFromSOQL) {
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL);
}
}
}
}
final ASTAssignmentExpression assignment = node.getFirstParentOfType(ASTAssignmentExpression.class);
if (assignment != null) {
final ASTVariableExpression variable = assignment.getFirstChildOfType(ASTVariableExpression.class);
if (variable != null) {
String variableWithClass = Helper.getFQVariableName(variable);
if (varToTypeMapping.containsKey(variableWithClass)) {
String type = varToTypeMapping.get(variableWithClass);
if (!isGetter) {
if (typesFromSOQL.isEmpty()) {
validateCRUDCheckPresent(node, data, ANY, type);
} else {
for (String typeFromSOQL : typesFromSOQL) {
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL);
}
}
}
}
}
}
final ASTReturnStatement returnStatement = node.getFirstParentOfType(ASTReturnStatement.class);
if (returnStatement != null) {
if (!isGetter) {
if (typesFromSOQL.isEmpty()) {
validateCRUDCheckPresent(node, data, ANY, returnType);
} else {
for (String typeFromSOQL : typesFromSOQL) {
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL);
}
}
}
}
}
use of net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration in project pmd by pmd.
the class ApexDangerousMethodsRule method collectBenignVariables.
private void collectBenignVariables(ASTUserClass node) {
List<ASTField> fields = node.findDescendantsOfType(ASTField.class);
for (ASTField field : fields) {
if (BOOLEAN.equalsIgnoreCase(field.getNode().getFieldInfo().getType().getApexName())) {
whiteListedVariables.add(Helper.getFQVariableName(field));
}
}
List<ASTVariableDeclaration> declarations = node.findDescendantsOfType(ASTVariableDeclaration.class);
for (ASTVariableDeclaration decl : declarations) {
if (BOOLEAN.equalsIgnoreCase(decl.getNode().getLocalInfo().getType().getApexName())) {
whiteListedVariables.add(Helper.getFQVariableName(decl));
}
}
}
use of net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration in project pmd by pmd.
the class ApexSOQLInjectionRule method visit.
@Override
public Object visit(ASTUserClass node, Object data) {
if (Helper.isTestMethodOrClass(node) || Helper.isSystemLevelClass(node)) {
// stops all the rules
return data;
}
final List<ASTMethod> methodExpr = node.findDescendantsOfType(ASTMethod.class);
for (ASTMethod m : methodExpr) {
findSafeVariablesInSignature(m);
}
final List<ASTFieldDeclaration> fieldExpr = node.findDescendantsOfType(ASTFieldDeclaration.class);
for (ASTFieldDeclaration a : fieldExpr) {
findSanitizedVariables(a);
findSelectContainingVariables(a);
}
// String foo = String.escapeSignleQuotes(...);
final List<ASTVariableDeclaration> variableDecl = node.findDescendantsOfType(ASTVariableDeclaration.class);
for (ASTVariableDeclaration a : variableDecl) {
findSanitizedVariables(a);
findSelectContainingVariables(a);
}
// baz = String.escapeSignleQuotes(...);
final List<ASTAssignmentExpression> assignmentCalls = node.findDescendantsOfType(ASTAssignmentExpression.class);
for (ASTAssignmentExpression a : assignmentCalls) {
findSanitizedVariables(a);
findSelectContainingVariables(a);
}
// Database.query(...) check
final List<ASTMethodCallExpression> potentialDbQueryCalls = node.findDescendantsOfType(ASTMethodCallExpression.class);
for (ASTMethodCallExpression m : potentialDbQueryCalls) {
if (!Helper.isTestMethodOrClass(m) && Helper.isMethodName(m, DATABASE, QUERY)) {
reportStrings(m, data);
reportVariables(m, data);
}
}
safeVariables.clear();
selectContainingVariables.clear();
return data;
}
use of net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration in project pmd by pmd.
the class ApexSuggestUsingNamedCredRule method visit.
@Override
public Object visit(ASTUserClass node, Object data) {
if (Helper.isTestMethodOrClass(node)) {
return data;
}
List<ASTVariableDeclaration> variableDecls = node.findDescendantsOfType(ASTVariableDeclaration.class);
for (ASTVariableDeclaration varDecl : variableDecls) {
findAuthLiterals(varDecl);
}
List<ASTField> fieldDecl = node.findDescendantsOfType(ASTField.class);
for (ASTField fDecl : fieldDecl) {
findFieldLiterals(fDecl);
}
List<ASTMethodCallExpression> methodCalls = node.findDescendantsOfType(ASTMethodCallExpression.class);
for (ASTMethodCallExpression method : methodCalls) {
flagAuthorizationHeaders(method, data);
}
listOfAuthorizationVariables.clear();
return data;
}
use of net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration in project pmd by pmd.
the class ApexXSSFromURLParamRule method findTaintedVariables.
private void findTaintedVariables(AbstractApexNode<?> node, Object data) {
final ASTMethodCallExpression right = node.getFirstChildOfType(ASTMethodCallExpression.class);
if (right != null) {
if (Helper.isMethodCallChain(right, URL_PARAMETER_METHOD)) {
ASTVariableExpression left = node.getFirstChildOfType(ASTVariableExpression.class);
String varType = null;
if (node instanceof ASTVariableDeclaration) {
varType = ((ASTVariableDeclaration) node).getNode().getLocalInfo().getType().getApexName();
}
if (left != null) {
if (varType == null || !"id".equalsIgnoreCase(varType)) {
urlParameterStrings.add(Helper.getFQVariableName(left));
}
}
}
processEscapingMethodCalls(right, data);
}
}
Aggregations