use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.
the class AlarmDetector method ensureAtLeast.
private static void ensureAtLeast(@NonNull JavaContext context, @NonNull UCallExpression node, int parameter, long min) {
UExpression argument = node.getValueArguments().get(parameter);
long value = getLongValue(context, argument);
if (value < min) {
String message = String.format("Value will be forced up to %1$d as of Android 5.1; " + "don't rely on this to be exact", min);
context.report(ISSUE, argument, context.getUastLocation(argument), message);
}
}
use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.
the class AllowAllHostnameVerifierDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
JavaEvaluator evaluator = context.getEvaluator();
if (evaluator.methodMatches(method, null, false, "javax.net.ssl.HostnameVerifier")) {
UExpression argument = node.getValueArguments().get(0);
PsiElement resolvedArgument = UastUtils.tryResolve(argument);
if (resolvedArgument instanceof PsiField) {
PsiField field = (PsiField) resolvedArgument;
if ("ALLOW_ALL_HOSTNAME_VERIFIER".equals(field.getName())) {
Location location = context.getUastLocation(argument);
String message = "Using the ALLOW_ALL_HOSTNAME_VERIFIER HostnameVerifier " + "is unsafe because it always returns true, which could cause " + "insecure network traffic due to trusting TLS/SSL server " + "certificates for wrong hostnames";
context.report(ISSUE, argument, location, message);
}
}
}
}
use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.
the class PermissionRequirement method getAnnotationStringValues.
@Nullable
public static String[] getAnnotationStringValues(@Nullable UAnnotation annotation, @NonNull String name) {
if (annotation != null) {
UExpression attributeValue = annotation.findDeclaredAttributeValue(name);
if (attributeValue == null && ATTR_VALUE.equals(name)) {
attributeValue = annotation.findDeclaredAttributeValue(null);
}
if (attributeValue == null) {
return null;
}
if (UastExpressionUtils.isArrayInitializer(attributeValue)) {
List<UExpression> initializers = ((UCallExpression) attributeValue).getValueArguments();
List<String> result = Lists.newArrayListWithCapacity(initializers.size());
ConstantEvaluator constantEvaluator = new ConstantEvaluator(null);
for (UExpression element : initializers) {
Object o = constantEvaluator.evaluate(element);
if (o instanceof String) {
result.add((String) o);
}
}
if (result.isEmpty()) {
return null;
} else {
return result.toArray(new String[0]);
}
} else {
// Use constant evaluator since we want to resolve field references as well
Object o = ConstantEvaluator.evaluate(null, attributeValue);
if (o instanceof String) {
return new String[] { (String) o };
} else if (o instanceof String[]) {
return (String[]) o;
} else if (o instanceof Object[]) {
Object[] array = (Object[]) o;
List<String> strings = Lists.newArrayListWithCapacity(array.length);
for (Object element : array) {
if (element instanceof String) {
strings.add((String) element);
}
}
return strings.toArray(new String[0]);
}
}
}
return null;
}
use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.
the class SecurityDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
List<UExpression> args = node.getValueArguments();
String methodName = node.getMethodName();
if (context.getEvaluator().isMemberInSubClassOf(method, FILE_CLASS, false)) {
// java.io.File.setWritable(true, false)
if ("setReadable".equals(methodName)) {
if (args.size() == 2 && Boolean.TRUE.equals(ConstantEvaluator.evaluate(context, args.get(0))) && Boolean.FALSE.equals(ConstantEvaluator.evaluate(context, args.get(1)))) {
context.report(SET_READABLE, node, context.getUastLocation(node), "Setting file permissions to world-readable can be " + "risky, review carefully");
}
return;
} else if ("setWritable".equals(methodName)) {
if (args.size() == 2 && Boolean.TRUE.equals(ConstantEvaluator.evaluate(context, args.get(0))) && Boolean.FALSE.equals(ConstantEvaluator.evaluate(context, args.get(1)))) {
context.report(SET_WRITABLE, node, context.getUastLocation(node), "Setting file permissions to world-writable can be " + "risky, review carefully");
}
return;
}
}
assert visitor != null;
for (UExpression arg : args) {
arg.accept(visitor);
}
}
use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.
the class ServiceCastDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
UElement parent = LintUtils.skipParentheses(UastUtils.getQualifiedParentOrThis(call).getUastParent());
if (UastExpressionUtils.isTypeCast(parent)) {
UBinaryExpressionWithType cast = (UBinaryExpressionWithType) parent;
List<UExpression> args = call.getValueArguments();
if (args.size() == 1 && args.get(0) instanceof UReferenceExpression) {
PsiElement resolvedServiceConst = ((UReferenceExpression) args.get(0)).resolve();
if (!(resolvedServiceConst instanceof PsiField)) {
return;
}
String name = ((PsiField) resolvedServiceConst).getName();
String expectedClass = getExpectedType(name);
if (expectedClass != null && cast != null) {
String castType = cast.getType().getCanonicalText();
if (castType.indexOf('.') == -1) {
expectedClass = stripPackage(expectedClass);
}
if (!castType.equals(expectedClass)) {
// android.content.ClipboardManager and android.text.ClipboardManager
if (isClipboard(castType) && isClipboard(expectedClass)) {
return;
}
String message = String.format("Suspicious cast to `%1$s` for a `%2$s`: expected `%3$s`", stripPackage(castType), name, stripPackage(expectedClass));
context.report(ISSUE, call, context.getUastLocation(cast), message);
}
}
}
}
}
Aggregations