|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.Reader; |
| 8 | +import java.io.UnsupportedEncodingException; |
| 9 | +import java.net.URLDecoder; |
| 10 | +import java.nio.file.Files; |
| 11 | + |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.core.io.ClassPathResource; |
| 14 | +import org.springframework.core.io.Resource; |
| 15 | +import org.springframework.core.io.ResourceLoader; |
| 16 | +import org.springframework.util.ResourceUtils; |
| 17 | +import org.springframework.util.StringUtils; |
| 18 | +import org.springframework.web.bind.annotation.GetMapping; |
| 19 | +import org.springframework.web.bind.annotation.RequestParam; |
| 20 | +import org.springframework.web.bind.annotation.RestController; |
| 21 | + |
| 22 | +/** Sample class of Spring RestController */ |
| 23 | +@RestController |
| 24 | +public class UnsafeLoadSpringResource { |
| 25 | + @GetMapping("/file1") |
| 26 | + //BAD: Get resource from ClassPathResource without input validation |
| 27 | + public String getFileContent1(@RequestParam(name="fileName") String fileName) { |
| 28 | + // A request such as the following can disclose source code and application configuration |
| 29 | + // fileName=/../../WEB-INF/views/page.jsp |
| 30 | + // fileName=/com/example/package/SampleController.class |
| 31 | + ClassPathResource clr = new ClassPathResource(fileName); |
| 32 | + char[] buffer = new char[4096]; |
| 33 | + StringBuilder out = new StringBuilder(); |
| 34 | + try { |
| 35 | + Reader in = new FileReader(clr.getFilename()); |
| 36 | + for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { |
| 37 | + out.append(buffer, 0, numRead); |
| 38 | + } |
| 39 | + } catch (IOException ie) { |
| 40 | + ie.printStackTrace(); |
| 41 | + } |
| 42 | + return out.toString(); |
| 43 | + } |
| 44 | + |
| 45 | + @GetMapping("/file1a") |
| 46 | + //GOOD: Get resource from ClassPathResource with input path validation |
| 47 | + public String getFileContent1a(@RequestParam(name="fileName") String fileName) { |
| 48 | + String result = null; |
| 49 | + if (fileName.startsWith("/safe_dir") && !fileName.contains("..")) { |
| 50 | + ClassPathResource clr = new ClassPathResource(fileName); |
| 51 | + char[] buffer = new char[4096]; |
| 52 | + StringBuilder out = new StringBuilder(); |
| 53 | + try { |
| 54 | + Reader in = new InputStreamReader(clr.getInputStream(), "UTF-8"); |
| 55 | + for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { |
| 56 | + out.append(buffer, 0, numRead); |
| 57 | + } |
| 58 | + } catch (IOException ie) { |
| 59 | + ie.printStackTrace(); |
| 60 | + } |
| 61 | + result = out.toString(); |
| 62 | + } |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + @GetMapping("/file2") |
| 67 | + //BAD: Get resource from ResourceUtils without input validation |
| 68 | + public String getFileContent2(@RequestParam(name="fileName") String fileName) { |
| 69 | + String content = null; |
| 70 | + |
| 71 | + try { |
| 72 | + // A request such as the following can disclose source code and system configuration |
| 73 | + // fileName=/etc/hosts |
| 74 | + // fileName=file:/etc/hosts |
| 75 | + // fileName=/opt/appdir/WEB-INF/views/page.jsp |
| 76 | + File file = ResourceUtils.getFile(fileName); |
| 77 | + //Read File Content |
| 78 | + content = new String(Files.readAllBytes(file.toPath())); |
| 79 | + } catch (IOException ie) { |
| 80 | + ie.printStackTrace(); |
| 81 | + } |
| 82 | + return content; |
| 83 | + } |
| 84 | + |
| 85 | + @GetMapping("/file2a") |
| 86 | + //GOOD: Get resource from ResourceUtils with input path validation |
| 87 | + public String getFileContent2a(@RequestParam(name="fileName") String fileName) { |
| 88 | + String content = null; |
| 89 | + |
| 90 | + if (fileName.startsWith("/safe_dir") && !fileName.contains("..")) { |
| 91 | + try { |
| 92 | + File file = ResourceUtils.getFile(fileName); |
| 93 | + //Read File Content |
| 94 | + content = new String(Files.readAllBytes(file.toPath())); |
| 95 | + } catch (IOException ie) { |
| 96 | + ie.printStackTrace(); |
| 97 | + } |
| 98 | + } |
| 99 | + return content; |
| 100 | + } |
| 101 | + |
| 102 | + @Autowired |
| 103 | + ResourceLoader resourceLoader; |
| 104 | + |
| 105 | + @GetMapping("/file3") |
| 106 | + //BAD: Get resource from ResourceLoader (same as application context) without input validation |
| 107 | + // Note it is not detected without the generic `resource.getInputStream()` check |
| 108 | + public String getFileContent3(@RequestParam(name="fileName") String fileName) { |
| 109 | + String content = null; |
| 110 | + |
| 111 | + try { |
| 112 | + // A request such as the following can disclose source code and system configuration |
| 113 | + // fileName=/WEB-INF/views/page.jsp |
| 114 | + // fileName=/WEB-INF/classes/com/example/package/SampleController.class |
| 115 | + // fileName=file:/etc/hosts |
| 116 | + Resource resource = resourceLoader.getResource(fileName); |
| 117 | + |
| 118 | + char[] buffer = new char[4096]; |
| 119 | + StringBuilder out = new StringBuilder(); |
| 120 | + |
| 121 | + Reader in = new InputStreamReader(resource.getInputStream(), "UTF-8"); |
| 122 | + for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { |
| 123 | + out.append(buffer, 0, numRead); |
| 124 | + } |
| 125 | + content = out.toString(); |
| 126 | + } catch (IOException ie) { |
| 127 | + ie.printStackTrace(); |
| 128 | + } |
| 129 | + return content; |
| 130 | + } |
| 131 | + |
| 132 | + @GetMapping("/file3a") |
| 133 | + //GOOD: Get resource from ResourceLoader (same as application context) with input path validation |
| 134 | + public String getFileContent3a(@RequestParam(name="fileName") String fileName) { |
| 135 | + String content = null; |
| 136 | + |
| 137 | + if (fileName.startsWith("/safe_dir") && !fileName.contains("..")) { |
| 138 | + try { |
| 139 | + Resource resource = resourceLoader.getResource(fileName); |
| 140 | + |
| 141 | + char[] buffer = new char[4096]; |
| 142 | + StringBuilder out = new StringBuilder(); |
| 143 | + |
| 144 | + Reader in = new InputStreamReader(resource.getInputStream(), "UTF-8"); |
| 145 | + for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { |
| 146 | + out.append(buffer, 0, numRead); |
| 147 | + } |
| 148 | + content = out.toString(); |
| 149 | + } catch (IOException ie) { |
| 150 | + ie.printStackTrace(); |
| 151 | + } |
| 152 | + } |
| 153 | + return content; |
| 154 | + } |
| 155 | +} |
0 commit comments