Skip to content

Commit

Permalink
🚧 Use Record
Browse files Browse the repository at this point in the history
  • Loading branch information
gilday committed Jun 26, 2024
1 parent 84fc415 commit 843648d
Showing 1 changed file with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -119,56 +119,22 @@ 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<Statement> getSingleStatement(final CompilationUnit cu, final Integer line) {
private static Optional<Statement> 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<Run> runs = sarif.rawDocument().getRuns();
return runs != null && !runs.isEmpty() && !runs.get(0).getResults().isEmpty();
}

/** Reads the source code from the given file and numbers each line. */
private List<String> readNumberedLines(final Path source) throws IOException {
private static List<String> 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();
Expand All @@ -194,4 +160,23 @@ private static String snippet(final List<String> 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 {}
}

0 comments on commit 843648d

Please sign in to comment.