|
| 1 | +package io.codemodder.remediation.loginjection; |
| 2 | + |
| 3 | +import static io.codemodder.javaparser.JavaParserTransformer.wrap; |
| 4 | + |
| 5 | +import com.github.javaparser.ast.CompilationUnit; |
| 6 | +import com.github.javaparser.ast.Node; |
| 7 | +import com.github.javaparser.ast.NodeList; |
| 8 | +import com.github.javaparser.ast.body.Parameter; |
| 9 | +import com.github.javaparser.ast.body.VariableDeclarator; |
| 10 | +import com.github.javaparser.ast.expr.BinaryExpr; |
| 11 | +import com.github.javaparser.ast.expr.Expression; |
| 12 | +import com.github.javaparser.ast.expr.MethodCallExpr; |
| 13 | +import com.github.javaparser.ast.expr.NameExpr; |
| 14 | +import com.github.javaparser.resolution.types.ResolvedType; |
| 15 | +import io.codemodder.DependencyGAV; |
| 16 | +import io.codemodder.ast.ASTs; |
| 17 | +import io.codemodder.ast.LocalDeclaration; |
| 18 | +import io.codemodder.remediation.RemediationStrategy; |
| 19 | +import io.codemodder.remediation.SuccessOrReason; |
| 20 | +import io.github.pixee.security.Newlines; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +/** |
| 25 | + * The shapes of code we want to be able to fix: |
| 26 | + * |
| 27 | + * <pre> |
| 28 | + * log.info("User with id: " + userId + " has been created"); |
| 29 | + * logger.error("User with id: " + userId + " has been created", ex); |
| 30 | + * log.warn(msg); |
| 31 | + * </pre> |
| 32 | + */ |
| 33 | +final class LogStatementFixer implements RemediationStrategy { |
| 34 | + |
| 35 | + @Override |
| 36 | + public SuccessOrReason fix(final CompilationUnit compilationUnit, final Node node) { |
| 37 | + MethodCallExpr logCall = (MethodCallExpr) node; |
| 38 | + NodeList<Expression> arguments = logCall.getArguments(); |
| 39 | + return fixArguments(arguments); |
| 40 | + } |
| 41 | + |
| 42 | + private SuccessOrReason fixArguments(final NodeList<Expression> arguments) { |
| 43 | + |
| 44 | + // first and only is NameExpr (not an exception) (args == 1), (args can be 2 if exception) |
| 45 | + if ((arguments.size() == 1 && arguments.get(0).isNameExpr()) |
| 46 | + || (arguments.size() == 2 |
| 47 | + && arguments.get(0).isNameExpr() |
| 48 | + && isException(arguments.get(1)))) { |
| 49 | + NameExpr argument = arguments.get(0).asNameExpr(); |
| 50 | + wrapWithNewlineSanitizer(argument); |
| 51 | + return SuccessOrReason.success(List.of(DependencyGAV.OWASP_XSS_JAVA_ENCODER)); |
| 52 | + } |
| 53 | + |
| 54 | + // first is string literal and second is NameExpr (args can be 3 if not exception) |
| 55 | + if ((arguments.size() == 2 |
| 56 | + && arguments.get(0).isStringLiteralExpr() |
| 57 | + && arguments.get(1).isNameExpr()) |
| 58 | + || (arguments.size() == 3 |
| 59 | + && arguments.get(0).isStringLiteralExpr() |
| 60 | + && arguments.get(1).isNameExpr() |
| 61 | + && isException(arguments.get(2)))) { |
| 62 | + NameExpr argument = arguments.get(1).asNameExpr(); |
| 63 | + wrapWithNewlineSanitizer(argument); |
| 64 | + return SuccessOrReason.success(List.of(DependencyGAV.OWASP_XSS_JAVA_ENCODER)); |
| 65 | + } |
| 66 | + |
| 67 | + // first is BinaryExpr with NameExpr in it (args == 2) (args can be 3 if last is exception) |
| 68 | + if ((arguments.size() == 2 && arguments.get(0).isBinaryExpr()) |
| 69 | + || (arguments.size() == 3 |
| 70 | + && arguments.get(0).isBinaryExpr() |
| 71 | + && isException(arguments.get(2)))) { |
| 72 | + BinaryExpr binaryExpr = arguments.get(0).asBinaryExpr(); |
| 73 | + Optional<Expression> expressionToWrap = findExpressionToWrap(binaryExpr); |
| 74 | + if (expressionToWrap.isPresent()) { |
| 75 | + wrapWithNewlineSanitizer(expressionToWrap.get()); |
| 76 | + return SuccessOrReason.success(List.of(DependencyGAV.OWASP_XSS_JAVA_ENCODER)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return SuccessOrReason.reason("Unfixable log call shape"); |
| 81 | + } |
| 82 | + |
| 83 | + private boolean isException(final Expression expression) { |
| 84 | + if (expression.isNameExpr()) { |
| 85 | + try { |
| 86 | + ResolvedType type = expression.calculateResolvedType(); |
| 87 | + String typeName = type.describe(); |
| 88 | + return isExceptionTypeName(typeName); |
| 89 | + } catch (Exception e) { |
| 90 | + Optional<LocalDeclaration> declarationRef = |
| 91 | + ASTs.findEarliestLocalDeclarationOf(expression, "ex"); |
| 92 | + if (declarationRef.isPresent()) { |
| 93 | + LocalDeclaration localDeclaration = declarationRef.get(); |
| 94 | + Node declaration = localDeclaration.getDeclaration(); |
| 95 | + // handle if its a parameter or a local variable |
| 96 | + if (declaration instanceof Parameter param) { |
| 97 | + String typeAsString = param.getTypeAsString(); |
| 98 | + return isExceptionTypeName(typeAsString); |
| 99 | + } else if (declaration instanceof VariableDeclarator var) { |
| 100 | + String typeAsString = var.getTypeAsString(); |
| 101 | + return isExceptionTypeName(typeAsString); |
| 102 | + } |
| 103 | + } |
| 104 | + Optional<Node> nameSourceNodeRef = ASTs.findNonCallableSimpleNameSource(expression, "e"); |
| 105 | + if (nameSourceNodeRef.isPresent()) { |
| 106 | + Node declaration = nameSourceNodeRef.get(); |
| 107 | + // handle if its a parameter or a local variable |
| 108 | + if (declaration instanceof Parameter param) { |
| 109 | + String typeAsString = param.getTypeAsString(); |
| 110 | + return isExceptionTypeName(typeAsString); |
| 111 | + } else if (declaration instanceof VariableDeclarator var) { |
| 112 | + String typeAsString = var.getTypeAsString(); |
| 113 | + return isExceptionTypeName(typeAsString); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + private static boolean isExceptionTypeName(final String typeName) { |
| 122 | + return typeName.endsWith("Exception") || typeName.endsWith("Throwable"); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * We handle 4 expression code shapes. <code> |
| 127 | + * print(user.getName()); |
| 128 | + * print("Hello, " + user.getName()); |
| 129 | + * print(user.getName() + ", hello!"); |
| 130 | + * print("Hello, " + user.getName() + ", hello!"); |
| 131 | + * </code> |
| 132 | + * |
| 133 | + * <p>Note that we should only handle, for the tougher cases, string literals in combination with |
| 134 | + * the given expression. Note any other combination of expressions. |
| 135 | + */ |
| 136 | + private static Optional<Expression> findExpressionToWrap(final Expression argument) { |
| 137 | + |
| 138 | + if (argument.isNameExpr()) { |
| 139 | + return Optional.of(argument); |
| 140 | + } else if (argument.isBinaryExpr()) { |
| 141 | + BinaryExpr binaryExpr = argument.asBinaryExpr(); |
| 142 | + if (binaryExpr.getLeft().isBinaryExpr() && binaryExpr.getRight().isStringLiteralExpr()) { |
| 143 | + BinaryExpr leftBinaryExpr = binaryExpr.getLeft().asBinaryExpr(); |
| 144 | + if (leftBinaryExpr.getLeft().isStringLiteralExpr() |
| 145 | + && !leftBinaryExpr.getRight().isStringLiteralExpr()) { |
| 146 | + return Optional.of(leftBinaryExpr.getRight()); |
| 147 | + } |
| 148 | + } else if (binaryExpr.getLeft().isStringLiteralExpr() |
| 149 | + && binaryExpr.getRight().isStringLiteralExpr()) { |
| 150 | + return Optional.empty(); |
| 151 | + } else if (binaryExpr.getLeft().isStringLiteralExpr()) { |
| 152 | + return Optional.of(binaryExpr.getRight()); |
| 153 | + } else if (binaryExpr.getRight().isStringLiteralExpr()) { |
| 154 | + return Optional.of(binaryExpr.getLeft()); |
| 155 | + } |
| 156 | + } |
| 157 | + return Optional.empty(); |
| 158 | + } |
| 159 | + |
| 160 | + private static void wrapWithNewlineSanitizer(final Expression expression) { |
| 161 | + wrap(expression).withStaticMethod(Newlines.class.getName(), "stripNewLines", true); |
| 162 | + } |
| 163 | +} |
0 commit comments