From 843648da96fb54b9cbdaa45289004e581accad53 Mon Sep 17 00:00:00 2001 From: Johnathan Gilday Date: Wed, 26 Jun 2024 14:11:14 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Use=20Record?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codemods/SensitiveDataLoggingCodemod.java | 61 +++++++------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/core-codemods/src/main/java/io/codemodder/codemods/SensitiveDataLoggingCodemod.java b/core-codemods/src/main/java/io/codemodder/codemods/SensitiveDataLoggingCodemod.java index cab9afa37..1782abcab 100644 --- a/core-codemods/src/main/java/io/codemodder/codemods/SensitiveDataLoggingCodemod.java +++ b/core-codemods/src/main/java/io/codemodder/codemods/SensitiveDataLoggingCodemod.java @@ -73,7 +73,7 @@ public CodemodFileScanningResult visit( if (analysis.isSensitiveAndDirectlyLogged()) { // remove the log statement altogether statement.get().remove(); - String analysisText = analysis.isSensitiveAnalysisText(); + String analysisText = analysis.sensitiveAnalysisText(); CodemodChange change = CodemodChange.from(startLine, analysisText); changes.add(change); } @@ -119,48 +119,14 @@ private SensitivityAndFixAnalysis performSensitivityAnalysis( * We can fix if there's only one statement on the given line (meaning, it may span multiple * lines, but only one statement is started on the line). */ - private Optional getSingleStatement(final CompilationUnit cu, final Integer line) { + private static Optional getSingleStatement( + final CompilationUnit cu, final Integer line) { return cu.findAll(Statement.class).stream() .filter(s -> s.getRange().isPresent()) .filter(s -> s.getRange().get().begin.line == line) .findFirst(); } - /** The results of the sensitivity analysis and, optionally, the fix to apply. */ - private interface SensitivityAndFixAnalysis { - - /** - * A detailed analysis of whether the data is sensitive, like a password, security token, etc. - * and its directly logged. - */ - String isSensitiveAnalysisText(); - - /** Whether the statement logs sensitive data. */ - boolean isSensitiveAndDirectlyLogged(); - } - - private static class SensitivityAndFixAnalysisDTO implements SensitivityAndFixAnalysis { - - @JsonProperty("sensitive_analysis_text") - private String sensitiveAnalysisText; - - @JsonProperty("is_data_directly_logged") - private String isDataDirectlyLogged; - - @JsonProperty("is_it_sensitive_and_directly_logged") - private boolean isSensitiveAndDirectlyLogged; - - @Override - public String isSensitiveAnalysisText() { - return sensitiveAnalysisText; - } - - @Override - public boolean isSensitiveAndDirectlyLogged() { - return isSensitiveAndDirectlyLogged; - } - } - @Override public boolean shouldRun() { List runs = sarif.rawDocument().getRuns(); @@ -168,7 +134,7 @@ public boolean shouldRun() { } /** Reads the source code from the given file and numbers each line. */ - private List readNumberedLines(final Path source) throws IOException { + private static List readNumberedLines(final Path source) throws IOException { final var counter = new AtomicInteger(); try (final var lines = Files.lines(source)) { return lines.map(line -> counter.incrementAndGet() + ": " + line).toList(); @@ -194,4 +160,23 @@ private static String snippet(final List lines, final int line) { * the code snippet sent to OpenAI. */ private static final int CONTEXT = 10; + + /** The results of the sensitivity analysis. */ + private interface SensitivityAndFixAnalysis { + + /** + * A detailed analysis of whether the data is sensitive, like a password, security token, etc. + * and its directly logged. + */ + String sensitiveAnalysisText(); + + /** Whether the statement logs sensitive data. */ + boolean isSensitiveAndDirectlyLogged(); + } + + private record SensitivityAndFixAnalysisDTO( + @JsonProperty("sensitive_analysis_text") String sensitiveAnalysisText, + @JsonProperty("is_data_directly_logged") String isDataDirectlyLogged, + @JsonProperty("is_it_sensitive_and_directly_logged") boolean isSensitiveAndDirectlyLogged) + implements SensitivityAndFixAnalysis {} }