|
| 1 | +package io.codemodder.remediation.xss; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.github.javaparser.JavaParser; |
| 6 | +import com.github.javaparser.ast.CompilationUnit; |
| 7 | +import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; |
| 8 | +import io.codemodder.CodemodFileScanningResult; |
| 9 | +import io.codemodder.codetf.DetectorRule; |
| 10 | +import io.codemodder.javaparser.JavaParserFactory; |
| 11 | +import io.codemodder.remediation.FixCandidateSearcher; |
| 12 | +import io.codemodder.remediation.SearcherStrategyRemediator; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Optional; |
| 16 | +import java.util.stream.Stream; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.Arguments; |
| 20 | +import org.junit.jupiter.params.provider.MethodSource; |
| 21 | + |
| 22 | +final class ResponseEntityWriteFixStrategyTest { |
| 23 | + |
| 24 | + private ResponseEntityWriteFixStrategy fixer; |
| 25 | + private DetectorRule rule; |
| 26 | + private JavaParser parser; |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + void setup() throws IOException { |
| 30 | + this.fixer = new ResponseEntityWriteFixStrategy(); |
| 31 | + this.parser = JavaParserFactory.newFactory().create(List.of()); |
| 32 | + this.rule = new DetectorRule("xss", "XSS", null); |
| 33 | + } |
| 34 | + |
| 35 | + private static Stream<Arguments> fixableSamples() { |
| 36 | + return Stream.of( |
| 37 | + Arguments.of( |
| 38 | + """ |
| 39 | + class Samples { |
| 40 | + String should_be_fixed(String s) { |
| 41 | + return ResponseEntity.ok("Value: " + s); |
| 42 | + } |
| 43 | + } |
| 44 | + """, |
| 45 | + """ |
| 46 | + import org.owasp.encoder.Encode; |
| 47 | + class Samples { |
| 48 | + String should_be_fixed(String s) { |
| 49 | + return ResponseEntity.ok("Value: " + Encode.forHtml(s)); |
| 50 | + } |
| 51 | + } |
| 52 | + """), |
| 53 | + Arguments.of( |
| 54 | + """ |
| 55 | + class Samples { |
| 56 | + String should_be_fixed(Object s) { |
| 57 | + return ResponseEntity.ok("Value: " + s.toString()); |
| 58 | + } |
| 59 | + } |
| 60 | + """, |
| 61 | + """ |
| 62 | + import org.owasp.encoder.Encode; |
| 63 | + class Samples { |
| 64 | + String should_be_fixed(Object s) { |
| 65 | + return ResponseEntity.ok("Value: " + Encode.forHtml(s.toString())); |
| 66 | + } |
| 67 | + } |
| 68 | + """)); |
| 69 | + } |
| 70 | + |
| 71 | + @ParameterizedTest |
| 72 | + @MethodSource("fixableSamples") |
| 73 | + void it_fixes_obvious_response_write_methods(final String beforeCode, final String afterCode) { |
| 74 | + CompilationUnit cu = parser.parse(beforeCode).getResult().orElseThrow(); |
| 75 | + LexicalPreservingPrinter.setup(cu); |
| 76 | + |
| 77 | + var result = scanAndFix(cu, 3); |
| 78 | + assertThat(result.changes()).isNotEmpty(); |
| 79 | + String actualCode = LexicalPreservingPrinter.print(cu); |
| 80 | + assertThat(actualCode).isEqualToIgnoringWhitespace(afterCode); |
| 81 | + } |
| 82 | + |
| 83 | + private CodemodFileScanningResult scanAndFix(final CompilationUnit cu, final int line) { |
| 84 | + XSSFinding finding = new XSSFinding("should_be_fixed", line, null); |
| 85 | + var remediator = |
| 86 | + new SearcherStrategyRemediator.Builder<XSSFinding>() |
| 87 | + .withSearcherStrategyPair( |
| 88 | + new FixCandidateSearcher.Builder<XSSFinding>() |
| 89 | + .withMatcher(ResponseEntityWriteFixStrategy::match) |
| 90 | + .build(), |
| 91 | + fixer) |
| 92 | + .build(); |
| 93 | + return remediator.remediateAll( |
| 94 | + cu, |
| 95 | + "path", |
| 96 | + rule, |
| 97 | + List.of(finding), |
| 98 | + XSSFinding::key, |
| 99 | + XSSFinding::line, |
| 100 | + x -> Optional.empty(), |
| 101 | + x -> Optional.ofNullable(x.column())); |
| 102 | + } |
| 103 | + |
| 104 | + @ParameterizedTest |
| 105 | + @MethodSource("unfixableSamples") |
| 106 | + void it_does_not_fix_unfixable_samples(final String beforeCode, final int line) { |
| 107 | + CompilationUnit cu = parser.parse(beforeCode).getResult().orElseThrow(); |
| 108 | + LexicalPreservingPrinter.setup(cu); |
| 109 | + var result = scanAndFix(cu, line); |
| 110 | + assertThat(result.changes()).isEmpty(); |
| 111 | + } |
| 112 | + |
| 113 | + private static Stream<Arguments> unfixableSamples() { |
| 114 | + return Stream.of( |
| 115 | + // this is not a ResponseEntity, shouldn't touch it |
| 116 | + Arguments.of( |
| 117 | + // this is not a ResponseEntity, shouldn't touch it |
| 118 | + """ |
| 119 | + class Samples { |
| 120 | + String should_be_fixed(String s) { |
| 121 | + return ResponseEntity.something_besides_ok("Value: " + s); |
| 122 | + } |
| 123 | + } |
| 124 | + """, |
| 125 | + 3)); |
| 126 | + } |
| 127 | +} |
0 commit comments