use of org.sonar.plugins.python.api.tree.HasSymbol in project sonar-python by SonarSource.
the class HardCodedCredentialsCheck method handleAssignmentStatement.
private void handleAssignmentStatement(AssignmentStatement assignmentStatement, SubscriptionContext ctx) {
ExpressionList lhs = assignmentStatement.lhsExpressions().get(0);
Expression expression = lhs.expressions().get(0);
if (expression instanceof HasSymbol) {
Symbol symbol = ((HasSymbol) expression).symbol();
String matchedCredential = credentialSymbolName(symbol);
if (matchedCredential != null) {
checkAssignedValue(assignmentStatement, matchedCredential, ctx);
}
}
if (expression.is(Kind.SUBSCRIPTION)) {
SubscriptionExpression subscriptionExpression = (SubscriptionExpression) expression;
for (Expression expr : subscriptionExpression.subscripts().expressions()) {
if (expr.is(Kind.STRING_LITERAL)) {
String matchedCredential = matchedCredential(((StringLiteral) expr).trimmedQuotesValue(), variablePatterns());
if (matchedCredential != null) {
checkAssignedValue(assignmentStatement, matchedCredential, ctx);
}
}
}
}
}
use of org.sonar.plugins.python.api.tree.HasSymbol in project sonar-python by SonarSource.
the class XMLParserXXEVulnerableCheck method checkSettingFeatureGesToTrue.
private static boolean checkSettingFeatureGesToTrue(CallExpression callExpression) {
List<Argument> arguments = callExpression.arguments();
if (arguments.size() != 2 || arguments.stream().anyMatch(argument -> !argument.is(Tree.Kind.REGULAR_ARGUMENT))) {
return false;
}
Expression first = ((RegularArgument) arguments.get(0)).expression();
Expression second = ((RegularArgument) arguments.get(1)).expression();
if (!(first instanceof HasSymbol)) {
return false;
}
Symbol symbol = ((HasSymbol) first).symbol();
if (symbol == null) {
return false;
}
return "xml.sax.handler.feature_external_ges".equals(symbol.fullyQualifiedName()) && !Expressions.isFalsy(second);
}
use of org.sonar.plugins.python.api.tree.HasSymbol in project sonar-python by SonarSource.
the class ItemOperationsTypeCheck method isValidSubscription.
@Override
public boolean isValidSubscription(Expression subscriptionObject, String requiredMethod, @Nullable String classRequiredMethod, Map<LocationInFile, String> secondaries) {
if (subscriptionObject.is(Tree.Kind.GENERATOR_EXPR)) {
return false;
}
if (subscriptionObject.is(Tree.Kind.CALL_EXPR)) {
Symbol subscriptionCalleeSymbol = ((CallExpression) subscriptionObject).calleeSymbol();
if (subscriptionCalleeSymbol != null && subscriptionCalleeSymbol.is(FUNCTION) && ((FunctionSymbol) subscriptionCalleeSymbol).isAsynchronous()) {
FunctionSymbol functionSymbol = (FunctionSymbol) subscriptionCalleeSymbol;
secondaries.put(functionSymbol.definitionLocation(), String.format(SECONDARY_MESSAGE, functionSymbol.name()));
return false;
}
}
if (subscriptionObject instanceof HasSymbol) {
Symbol symbol = ((HasSymbol) subscriptionObject).symbol();
if (symbol == null || isTypingOrCollectionsSymbol(symbol)) {
return true;
}
if (symbol.is(FUNCTION, CLASS)) {
secondaries.put(symbol.is(FUNCTION) ? ((FunctionSymbol) symbol).definitionLocation() : ((ClassSymbol) symbol).definitionLocation(), String.format(SECONDARY_MESSAGE, symbol.name()));
return canHaveMethod(symbol, requiredMethod, classRequiredMethod);
}
}
InferredType type = subscriptionObject.type();
String typeName = InferredTypes.typeName(type);
String secondaryMessage = typeName != null ? String.format(SECONDARY_MESSAGE, typeName) : DEFAULT_SECONDARY_MESSAGE;
secondaries.put(typeClassLocation(type), secondaryMessage);
return type.canHaveMember(requiredMethod);
}
use of org.sonar.plugins.python.api.tree.HasSymbol in project sonar-python by SonarSource.
the class IterationOnNonIterableCheck method isValidIterable.
boolean isValidIterable(Expression expression, Map<LocationInFile, String> secondaries) {
if (expression.is(Tree.Kind.CALL_EXPR)) {
CallExpression callExpression = (CallExpression) expression;
Symbol calleeSymbol = callExpression.calleeSymbol();
if (calleeSymbol != null && calleeSymbol.is(Symbol.Kind.FUNCTION) && ((FunctionSymbol) calleeSymbol).isAsynchronous()) {
FunctionSymbol functionSymbol = (FunctionSymbol) calleeSymbol;
secondaries.put(functionSymbol.definitionLocation(), String.format(SECONDARY_MESSAGE, functionSymbol.name()));
return false;
}
}
if (expression instanceof HasSymbol) {
Symbol symbol = ((HasSymbol) expression).symbol();
if (symbol != null) {
if (symbol.is(Symbol.Kind.FUNCTION)) {
FunctionSymbol functionSymbol = (FunctionSymbol) symbol;
secondaries.put(functionSymbol.definitionLocation(), String.format(SECONDARY_MESSAGE, functionSymbol.name()));
return functionSymbol.hasDecorators();
}
if (symbol.is(Symbol.Kind.CLASS)) {
ClassSymbolImpl classSymbol = (ClassSymbolImpl) symbol;
secondaries.put(classSymbol.definitionLocation(), String.format(SECONDARY_MESSAGE, classSymbol.name()));
// Metaclasses might add the method by default
return classSymbol.hasSuperClassWithUnknownMetaClass() || classSymbol.hasUnresolvedTypeHierarchy();
}
}
}
InferredType type = expression.type();
String typeName = InferredTypes.typeName(type);
String secondaryMessage = typeName != null ? String.format(SECONDARY_MESSAGE, typeName) : DEFAULT_SECONDARY_MESSAGE;
secondaries.put(InferredTypes.typeClassLocation(type), secondaryMessage);
return type.canHaveMember("__iter__") || type.canHaveMember("__getitem__");
}
use of org.sonar.plugins.python.api.tree.HasSymbol in project sonar-python by SonarSource.
the class IncorrectExceptionTypeCheck method initialize.
@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.RAISE_STMT, ctx -> {
RaiseStatement raiseStatement = (RaiseStatement) ctx.syntaxNode();
if (raiseStatement.expressions().isEmpty()) {
return;
}
Expression raisedExpression = raiseStatement.expressions().get(0);
if (!raisedExpression.type().canBeOrExtend(BASE_EXCEPTION)) {
ctx.addIssue(raiseStatement, MESSAGE);
return;
}
Symbol symbol = null;
if (raisedExpression instanceof HasSymbol) {
symbol = ((HasSymbol) raisedExpression).symbol();
} else if (raisedExpression.is(Tree.Kind.CALL_EXPR)) {
symbol = ((CallExpression) raisedExpression).calleeSymbol();
}
if (!mayInheritFromBaseException(symbol)) {
ctx.addIssue(raiseStatement, MESSAGE);
}
});
}
Aggregations