|
| 1 | +import org.springframework.stereotype.Controller; |
| 2 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 3 | +import org.springframework.web.bind.annotation.GetMapping; |
| 4 | +import org.springframework.web.bind.annotation.PostMapping; |
| 5 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 6 | +import static org.springframework.web.bind.annotation.RequestMethod.*; |
| 7 | +import org.springframework.web.bind.annotation.RequestParam; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import java.sql.DriverManager; |
| 10 | +import java.sql.Connection; |
| 11 | +import java.sql.PreparedStatement; |
| 12 | +import java.sql.SQLException; |
| 13 | + |
| 14 | +@Controller |
| 15 | +public class CsrfUnprotectedRequestTypeTest { |
| 16 | + |
| 17 | + // Test Spring sources with `PreparedStatement.executeUpdate()` as a default database update method call |
| 18 | + |
| 19 | + // BAD: allows request type not default-protected from CSRF when updating a database |
| 20 | + @RequestMapping("/") |
| 21 | + public void bad1() { // $ hasCsrfUnprotectedRequestType |
| 22 | + try { |
| 23 | + String sql = "DELETE"; |
| 24 | + Connection conn = DriverManager.getConnection("url"); |
| 25 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 26 | + ps.executeUpdate(); // database update method call |
| 27 | + } catch (SQLException e) { } |
| 28 | + } |
| 29 | + |
| 30 | + // BAD: uses GET request when updating a database |
| 31 | + @RequestMapping(value = "", method = RequestMethod.GET) |
| 32 | + public void bad2() { // $ hasCsrfUnprotectedRequestType |
| 33 | + try { |
| 34 | + String sql = "DELETE"; |
| 35 | + Connection conn = DriverManager.getConnection("url"); |
| 36 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 37 | + ps.executeUpdate(); // database update method call |
| 38 | + } catch (SQLException e) { } |
| 39 | + } |
| 40 | + |
| 41 | + // BAD: uses GET request when updating a database |
| 42 | + @GetMapping(value = "") |
| 43 | + public void bad3() { // $ hasCsrfUnprotectedRequestType |
| 44 | + try { |
| 45 | + String sql = "DELETE"; |
| 46 | + Connection conn = DriverManager.getConnection("url"); |
| 47 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 48 | + ps.executeUpdate(); // database update method call |
| 49 | + } catch (SQLException e) { } |
| 50 | + } |
| 51 | + |
| 52 | + // BAD: allows GET request when updating a database |
| 53 | + @RequestMapping(value = "", method = { RequestMethod.GET, RequestMethod.POST }) |
| 54 | + public void bad4() { // $ hasCsrfUnprotectedRequestType |
| 55 | + try { |
| 56 | + String sql = "DELETE"; |
| 57 | + Connection conn = DriverManager.getConnection("url"); |
| 58 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 59 | + ps.executeUpdate(); // database update method call |
| 60 | + } catch (SQLException e) { } |
| 61 | + } |
| 62 | + |
| 63 | + // BAD: uses request type not default-protected from CSRF when updating a database |
| 64 | + @RequestMapping(value = "", method = { GET, HEAD, OPTIONS, TRACE }) |
| 65 | + public void bad5() { // $ hasCsrfUnprotectedRequestType |
| 66 | + try { |
| 67 | + String sql = "DELETE"; |
| 68 | + Connection conn = DriverManager.getConnection("url"); |
| 69 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 70 | + ps.executeUpdate(); // database update method call |
| 71 | + } catch (SQLException e) { } |
| 72 | + } |
| 73 | + |
| 74 | + // GOOD: uses POST request when updating a database |
| 75 | + @RequestMapping(value = "", method = RequestMethod.POST) |
| 76 | + public void good1() { |
| 77 | + try { |
| 78 | + String sql = "DELETE"; |
| 79 | + Connection conn = DriverManager.getConnection("url"); |
| 80 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 81 | + ps.executeUpdate(); // database update method call |
| 82 | + } catch (SQLException e) { } |
| 83 | + } |
| 84 | + |
| 85 | + // GOOD: uses POST request when updating a database |
| 86 | + @RequestMapping(value = "", method = POST) |
| 87 | + public void good2() { |
| 88 | + try { |
| 89 | + String sql = "DELETE"; |
| 90 | + Connection conn = DriverManager.getConnection("url"); |
| 91 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 92 | + ps.executeUpdate(); // database update method call |
| 93 | + } catch (SQLException e) { } |
| 94 | + } |
| 95 | + |
| 96 | + // GOOD: uses POST request when updating a database |
| 97 | + @PostMapping(value = "") |
| 98 | + public void good3() { |
| 99 | + try { |
| 100 | + String sql = "DELETE"; |
| 101 | + Connection conn = DriverManager.getConnection("url"); |
| 102 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 103 | + ps.executeUpdate(); // database update method call |
| 104 | + } catch (SQLException e) { } |
| 105 | + } |
| 106 | + |
| 107 | + // GOOD: uses a request type that is default-protected from CSRF when updating a database |
| 108 | + @RequestMapping(value = "", method = { POST, PUT, PATCH, DELETE }) |
| 109 | + public void good4() { |
| 110 | + try { |
| 111 | + String sql = "DELETE"; |
| 112 | + Connection conn = DriverManager.getConnection("url"); |
| 113 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 114 | + ps.executeUpdate(); // database update method call |
| 115 | + } catch (SQLException e) { } |
| 116 | + } |
| 117 | + |
| 118 | + // Test database update method calls other than `PreparedStatement.executeUpdate()` |
| 119 | + |
| 120 | + // BAD: allows request type not default-protected from CSRF when |
| 121 | + // updating a database using `PreparedStatement.executeLargeUpdate()` |
| 122 | + @RequestMapping("/") |
| 123 | + public void bad6() { // $ hasCsrfUnprotectedRequestType |
| 124 | + try { |
| 125 | + String sql = "DELETE"; |
| 126 | + Connection conn = DriverManager.getConnection("url"); |
| 127 | + PreparedStatement ps = conn.prepareStatement(sql); |
| 128 | + ps.executeLargeUpdate(); // database update method call |
| 129 | + } catch (SQLException e) { } |
| 130 | + } |
| 131 | + |
| 132 | + @Autowired |
| 133 | + private MyBatisService myBatisService; |
| 134 | + |
| 135 | + // BAD: uses GET request when updating a database with MyBatis XML mapper method |
| 136 | + @GetMapping(value = "") |
| 137 | + public void bad7(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType |
| 138 | + myBatisService.bad7(name); |
| 139 | + } |
| 140 | + |
| 141 | + // BAD: uses GET request when updating a database with `@DeleteProvider` |
| 142 | + @GetMapping(value = "badDelete") |
| 143 | + public void badDelete(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType |
| 144 | + myBatisService.badDelete(name); |
| 145 | + } |
| 146 | + |
| 147 | + // BAD: uses GET request when updating a database with `@UpdateProvider` |
| 148 | + @GetMapping(value = "badUpdate") |
| 149 | + public void badUpdate(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType |
| 150 | + myBatisService.badUpdate(name); |
| 151 | + } |
| 152 | + |
| 153 | + // BAD: uses GET request when updating a database with `@InsertProvider` |
| 154 | + @GetMapping(value = "badInsert") |
| 155 | + public void badInsert(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType |
| 156 | + myBatisService.badInsert(name); |
| 157 | + } |
| 158 | +} |
0 commit comments