An attacker could try to pass a filename of content that could traverse the server path and control system files. Make sure all user-inputs is checked and sanitized before use.
classTest{@PostMapping(value="/fileupload")publicModelAndViewimportFile(@RequestParam("file")MultipartFilemyFile)throwsIOException{varuser=(WebGoatUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();vardestinationDir=newFile(fileLocation,user.getUsername());destinationDir.mkdirs();myFile.transferTo(newFile(destinationDir,myFile.getOriginalFilename()));log.debug("File saved to {}",newFile(destinationDir,myFile.getOriginalFilename()));returnnewModelAndView(newRedirectView("files",true),newModelMap().addAttribute("uploadSuccess","File uploaded successful"));}}
Compliant Code Examples
classTest{@PostMapping(value="/fileupload")publicModelAndViewimportFile(@RequestParam("file")MultipartFilemyFile)throwsIOException{// Safe: using sanitized filenameStringsanitizedFilename="upload_"+System.currentTimeMillis()+".dat";FiledestinationDir=newFile(fileLocation);myFile.transferTo(newFile(destinationDir,sanitizedFilename));returnnewModelAndView(newRedirectView("files",true));}}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
How to use this rule
1
2
rulesets:- java-security # Rules to enforce Java security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Security scans to your CI pipelines