|
| 1 | +package com.foo.rest.examples.spring.openapi.v3.security.xss.stored |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Operation |
| 4 | +import io.swagger.v3.oas.annotations.responses.ApiResponse |
| 5 | +import io.swagger.v3.oas.annotations.responses.ApiResponses |
| 6 | +import org.springframework.boot.SpringApplication |
| 7 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 8 | +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration |
| 9 | +import org.springframework.http.MediaType |
| 10 | +import org.springframework.web.bind.annotation.* |
| 11 | + |
| 12 | +data class CommentDto( |
| 13 | + val comment: String? = null, |
| 14 | + val author: String? = null |
| 15 | +) |
| 16 | + |
| 17 | +@SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) |
| 18 | +@RequestMapping(path = ["/api/stored"]) |
| 19 | +@RestController |
| 20 | +open class XSSStoredApplication { |
| 21 | + |
| 22 | + companion object { |
| 23 | + @JvmStatic |
| 24 | + fun main(args: Array<String>) { |
| 25 | + SpringApplication.run(XSSStoredApplication::class.java, *args) |
| 26 | + } |
| 27 | + |
| 28 | + // In-memory storage for stored XSS examples |
| 29 | + private val comments = mutableListOf<Pair<String, String>>() // Body parameter |
| 30 | + private val userBios = mutableMapOf<String, String>() // Path parameter |
| 31 | + private val guestbookEntries = mutableListOf<Pair<String, String>>() // Query parameter |
| 32 | + } |
| 33 | + |
| 34 | + // ==== BODY PARAMETER - Comment System ==== |
| 35 | + |
| 36 | + @PostMapping(path = ["/comment"], produces = [MediaType.TEXT_HTML_VALUE]) |
| 37 | + open fun storeComment(@RequestBody commentDto: CommentDto): String { |
| 38 | + // VULNERABLE: Stores user input without sanitization |
| 39 | + val comment = commentDto.comment ?: "No comment" |
| 40 | + val author = commentDto.author ?: "Anonymous" |
| 41 | + |
| 42 | + comments.add(Pair(author, comment)) |
| 43 | + |
| 44 | + return """ |
| 45 | + <!DOCTYPE html> |
| 46 | + <html> |
| 47 | + <head> |
| 48 | + <title>Comment Stored</title> |
| 49 | + </head> |
| 50 | + <body> |
| 51 | + <h2>Comment Stored Successfully!</h2> |
| 52 | + <p>Your comment has been saved and will be displayed to other users.</p> |
| 53 | + <a href="/api/stored/comments">View all comments</a> |
| 54 | + </body> |
| 55 | + </html> |
| 56 | + """.trimIndent() |
| 57 | + } |
| 58 | + |
| 59 | + @GetMapping(path = ["/comments"], produces = [MediaType.TEXT_HTML_VALUE]) |
| 60 | + open fun getComments(): String { |
| 61 | + // VULNERABLE: Displays stored user input without sanitization |
| 62 | + val commentsList = comments.joinToString("\n") { (author, comment) -> |
| 63 | + """ |
| 64 | + <div class="comment"> |
| 65 | + <p><strong>Author:</strong> $author</p> |
| 66 | + <p><strong>Comment:</strong> $comment</p> |
| 67 | + <hr> |
| 68 | + </div> |
| 69 | + """.trimIndent() |
| 70 | + } |
| 71 | + |
| 72 | + return """ |
| 73 | + <!DOCTYPE html> |
| 74 | + <html> |
| 75 | + <head> |
| 76 | + <title>All Comments</title> |
| 77 | + </head> |
| 78 | + <body> |
| 79 | + <h1>All Comments</h1> |
| 80 | + ${if (comments.isEmpty()) "<p>No comments yet.</p>" else commentsList} |
| 81 | + </body> |
| 82 | + </html> |
| 83 | + """.trimIndent() |
| 84 | + } |
| 85 | + |
| 86 | + // ==== PATH PARAMETER - User Bio System ==== |
| 87 | + |
| 88 | + @Operation( |
| 89 | + summary = "POST endpoint to store user bio (Stored XSS with path parameter)", |
| 90 | + description = "Stores user bio in memory without sanitization - allows Stored XSS attacks via path parameter" |
| 91 | + ) |
| 92 | + @ApiResponses( |
| 93 | + value = [ |
| 94 | + ApiResponse(responseCode = "200", description = "Bio stored successfully"), |
| 95 | + ApiResponse(responseCode = "400", description = "Invalid URI with special characters") |
| 96 | + ] |
| 97 | + ) |
| 98 | + @PostMapping(path = ["/user/{username}"], produces = [MediaType.TEXT_HTML_VALUE]) |
| 99 | + open fun storeBio( |
| 100 | + @PathVariable username: String, |
| 101 | + @RequestParam(name = "bio", required = false, defaultValue = "") bio: String |
| 102 | + ): String { |
| 103 | + // VULNERABLE: Stores user input from both path parameter and query parameter without sanitization |
| 104 | + userBios[username] = bio |
| 105 | + |
| 106 | + return """ |
| 107 | + <!DOCTYPE html> |
| 108 | + <html> |
| 109 | + <head> |
| 110 | + <title>Bio Stored</title> |
| 111 | + </head> |
| 112 | + </html> |
| 113 | + """.trimIndent() |
| 114 | + } |
| 115 | + |
| 116 | + @Operation( |
| 117 | + summary = "GET endpoint to retrieve user profile with bio (Stored XSS)", |
| 118 | + description = "Displays stored user bio without sanitization - executes stored XSS from path parameter data" |
| 119 | + ) |
| 120 | + @ApiResponses( |
| 121 | + value = [ |
| 122 | + ApiResponse(responseCode = "200", description = "User profile displayed"), |
| 123 | + ApiResponse(responseCode = "400", description = "Invalid URI with special characters") |
| 124 | + ] |
| 125 | + ) |
| 126 | + @GetMapping(path = ["/user/{username}"], produces = [MediaType.TEXT_HTML_VALUE]) |
| 127 | + open fun getUserProfile(@PathVariable username: String): String { |
| 128 | + // VULNERABLE: Displays stored user input without sanitization |
| 129 | + val bio = userBios[username] ?: "No bio available" |
| 130 | + |
| 131 | + return """ |
| 132 | + <!DOCTYPE html> |
| 133 | + <html> |
| 134 | + <head> |
| 135 | + <title>User Profile</title> |
| 136 | + </head> |
| 137 | + <body> |
| 138 | + <div class="profile-info"> |
| 139 | + <p><strong>Bio:</strong> $bio</p> |
| 140 | + </div> |
| 141 | + </body> |
| 142 | + </html> |
| 143 | + """.trimIndent() |
| 144 | + } |
| 145 | + |
| 146 | + // ==== QUERY PARAMETER - Guestbook System ==== |
| 147 | + |
| 148 | + @PostMapping(path = ["/guestbook"], produces = [MediaType.TEXT_HTML_VALUE]) |
| 149 | + open fun storeGuestbookEntry( |
| 150 | + @RequestParam(name = "name", required = false, defaultValue = "Anonymous") name: String, |
| 151 | + @RequestParam(name = "entry", required = false, defaultValue = "") entry: String |
| 152 | + ): String { |
| 153 | + // VULNERABLE: Stores user input from query parameters without sanitization |
| 154 | + guestbookEntries.add(Pair(name, entry)) |
| 155 | + |
| 156 | + return """ |
| 157 | + <!DOCTYPE html> |
| 158 | + <html> |
| 159 | + <head> |
| 160 | + <title>Entry Stored</title> |
| 161 | + </head> |
| 162 | + <body> |
| 163 | + <h2>Guestbook Entry Stored!</h2> |
| 164 | + <p>Thank you for signing our guestbook!</p> |
| 165 | + <a href="/api/stored/guestbook">View guestbook</a> |
| 166 | + </body> |
| 167 | + </html> |
| 168 | + """.trimIndent() |
| 169 | + } |
| 170 | + |
| 171 | + @GetMapping(path = ["/guestbook"], produces = [MediaType.TEXT_HTML_VALUE]) |
| 172 | + open fun getGuestbook(): String { |
| 173 | + // VULNERABLE: Displays stored user input without sanitization |
| 174 | + val entriesList = guestbookEntries.joinToString("\n") { (name, entry) -> |
| 175 | + """ |
| 176 | + <div class="entry"> |
| 177 | + <p><strong>$name</strong> wrote:</p> |
| 178 | + <p>$entry</p> |
| 179 | + <hr> |
| 180 | + </div> |
| 181 | + """.trimIndent() |
| 182 | + } |
| 183 | + |
| 184 | + return """ |
| 185 | + <!DOCTYPE html> |
| 186 | + <html> |
| 187 | + <head> |
| 188 | + <title>Guestbook</title> |
| 189 | + </head> |
| 190 | + <body> |
| 191 | + <h1>Guestbook</h1> |
| 192 | + ${if (guestbookEntries.isEmpty()) "<p>No entries yet. Be the first to sign!</p>" else entriesList} |
| 193 | + </body> |
| 194 | + </html> |
| 195 | + """.trimIndent() |
| 196 | + } |
| 197 | +} |
0 commit comments