|
1 | 1 | package io.codemodder.codemods;
|
2 | 2 |
|
3 |
| -import static io.codemodder.CodemodResources.getClassResourceAsString; |
4 |
| - |
5 |
| -import com.contrastsecurity.sarif.Result; |
6 |
| -import com.github.difflib.patch.AbstractDelta; |
7 |
| -import com.github.difflib.patch.InsertDelta; |
8 |
| -import com.github.difflib.patch.Patch; |
9 | 3 | import io.codemodder.*;
|
| 4 | +import io.codemodder.plugins.llm.CodeChangingLLMRemediationOutcome; |
| 5 | +import io.codemodder.plugins.llm.NoActionLLMRemediationOutcome; |
10 | 6 | import io.codemodder.plugins.llm.OpenAIService;
|
11 |
| -import io.codemodder.plugins.llm.SarifToLLMForBinaryVerificationAndFixingCodemod; |
| 7 | +import io.codemodder.plugins.llm.SarifToLLMForMultiOutcomeCodemod; |
| 8 | +import io.codemodder.plugins.llm.StandardModel; |
12 | 9 | import io.codemodder.providers.sarif.semgrep.SemgrepScan;
|
13 | 10 | import java.util.List;
|
14 | 11 | import javax.inject.Inject;
|
|
17 | 14 | id = "pixee:java/log-failed-login",
|
18 | 15 | importance = Importance.HIGH,
|
19 | 16 | reviewGuidance = ReviewGuidance.MERGE_AFTER_REVIEW)
|
20 |
| -public final class LogFailedLoginCodemod extends SarifToLLMForBinaryVerificationAndFixingCodemod { |
| 17 | +public final class LogFailedLoginCodemod extends SarifToLLMForMultiOutcomeCodemod { |
21 | 18 |
|
22 | 19 | @Inject
|
23 | 20 | public LogFailedLoginCodemod(
|
24 | 21 | @SemgrepScan(ruleId = "log-failed-login") final RuleSarif sarif, final OpenAIService openAI) {
|
25 |
| - super(sarif, openAI); |
| 22 | + super( |
| 23 | + sarif, |
| 24 | + openAI, |
| 25 | + List.of( |
| 26 | + new NoActionLLMRemediationOutcome( |
| 27 | + "logs_failed_login_with_logger", |
| 28 | + """ |
| 29 | + The code uses a logger to log a message that indicates a failed login attempt. |
| 30 | + The message is logged at the INFO or higher level. |
| 31 | + """ |
| 32 | + .replace('\n', ' ')), |
| 33 | + new NoActionLLMRemediationOutcome( |
| 34 | + "logs_failed_login_with_console", |
| 35 | + """ |
| 36 | + The code sends a message to the console that indicates a failed login attempt. |
| 37 | + The code may output this message to either System.out or System.err. |
| 38 | + """ |
| 39 | + .replace('\n', ' ')), |
| 40 | + new NoActionLLMRemediationOutcome( |
| 41 | + "throws_exception", |
| 42 | + """ |
| 43 | + The code throws an exception that indicates a failed login attempt. |
| 44 | + Throwing such an exception is a reasonable alternative to logging the failed login attempt. |
| 45 | + When the username for the failed login is in-scope, the exception message includes the username. |
| 46 | + """ |
| 47 | + .replace('\n', ' ')), |
| 48 | + new NoActionLLMRemediationOutcome( |
| 49 | + "no_authentication", |
| 50 | + """ |
| 51 | + The login validation fails because the request lacks credentials to validate. This is not considered a failed login attempt that requires auditing. |
| 52 | + """ |
| 53 | + .replace('\n', ' ')), |
| 54 | + new CodeChangingLLMRemediationOutcome( |
| 55 | + "add_missing_logging", |
| 56 | + """ |
| 57 | + None of the other outcomes apply. |
| 58 | + The code that validates the login credentials does not log a message when the login attempt fails, |
| 59 | + NOR does it throw an exception that reasonably indicates a failed login attempt and includes the username in the exception message. |
| 60 | + """ |
| 61 | + .replace('\n', ' '), |
| 62 | + """ |
| 63 | + Immediately following the login failure, add precisely one statement to log the failed login attempt at the INFO level. |
| 64 | + If the username for the failed login is in scope, the new log message references the username. |
| 65 | + Add exactly one such log statement! Exactly one! |
| 66 | + The new log statement is consistent with the rest of the code with respect to formatting, braces, casing, etc. |
| 67 | + When no logger is in scope, the new code emits a log message to the console. |
| 68 | + """ |
| 69 | + .replace('\n', ' '))), |
| 70 | + StandardModel.GPT_4O, |
| 71 | + StandardModel.GPT_4); |
26 | 72 | }
|
27 | 73 |
|
28 | 74 | @Override
|
29 |
| - protected String getThreatPrompt( |
30 |
| - final CodemodInvocationContext context, final List<Result> results) { |
31 |
| - return getClassResourceAsString(getClass(), "threat_prompt.txt"); |
32 |
| - } |
33 |
| - |
34 |
| - @Override |
35 |
| - protected String getFixPrompt() { |
36 |
| - return getClassResourceAsString(getClass(), "fix_prompt.txt"); |
37 |
| - } |
38 |
| - |
39 |
| - @Override |
40 |
| - protected boolean isPatchExpected(Patch<String> patch) { |
41 |
| - // This codemod should make two or fewer modifications. |
42 |
| - if (patch.getDeltas().size() > 2) { |
43 |
| - return false; |
44 |
| - } |
45 |
| - |
46 |
| - // This codemod should only insert lines. |
47 |
| - for (AbstractDelta<String> delta : patch.getDeltas()) { |
48 |
| - if (!(delta instanceof InsertDelta<String>)) { |
49 |
| - return false; |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - return true; |
| 75 | + protected String getThreatPrompt() { |
| 76 | + return """ |
| 77 | + The tool has cited an authentication check that does not include a means for auditing failed login attempt. |
| 78 | + """ |
| 79 | + .replace('\n', ' '); |
54 | 80 | }
|
55 | 81 | }
|
0 commit comments