@@ -66,6 +66,7 @@ HttpSession session = request.getSession();
66
66
* [ Custom error page] ( #error-page )
67
67
* [ Broken session Managment] ( #Session )
68
68
* [ Cross site Scripting] ( #Xss-Fix )
69
+ * [ Unrestricted file upload] ( #file-upload-secure )
69
70
70
71
71
72
## Secure-headers
@@ -155,4 +156,42 @@ ${StringEscapeUtils.escapeHtml(obj.name)}
155
156
}
156
157
```
157
158
159
+ ## file-upload-secure
160
+
161
+ ```
162
+
163
+ public ModelAndView upload(String file) {
164
+ String UPLOAD_FOLDER = "C://test//";
165
+
166
+ String fileExtentions = "jpg,jpeg,png";
167
+
168
+ String substring = FilenameUtils.getExtension(file);
169
+
170
+ System.out.println(substring);
171
+ if (!fileExtentions.contains(substring) || substring.isEmpty())
172
+ {
173
+ System.out.println("sorry");
174
+ return new ModelAndView("status", "message", " file type not supported");
175
+
176
+ }
177
+
178
+ else {
179
+ int abcd=file.hashCode();
180
+ System.out.println("good");
181
+ String filter= String.valueOf(abcd).concat(".png");
182
+ System.out.println(filter);
183
+ byte[] bytes = file.getBytes();
184
+ Path path = Paths.get(UPLOAD_FOLDER + filter);
185
+ try {
186
+ Files.write(path, bytes);
187
+ } catch (IOException e) {
188
+ // TODO Auto-generated catch block
189
+ e.printStackTrace();
190
+ }
191
+ }
192
+ //log
193
+ return new ModelAndView("status", "message", "succes");
194
+
195
+ }
196
+ ```
158
197
0 commit comments