Modify S2083(java): Improve the compliant sample (#3472)
This commit is contained in:
parent
7cb81aa05a
commit
f0db71d524
@ -31,15 +31,15 @@ public class ExampleController
|
|||||||
@Controller
|
@Controller
|
||||||
public class ExampleController
|
public class ExampleController
|
||||||
{
|
{
|
||||||
static private String targetDirectory = "/path/to/target/directory/";
|
private static String targetDirectory = "/path/to/target/directory/";
|
||||||
|
private static Path targetPath = new File(targetDirectory).toPath().normalize();
|
||||||
|
|
||||||
@GetMapping(value = "/delete")
|
@GetMapping(value = "/delete")
|
||||||
public void delete(@RequestParam("filename") String filename) throws IOException {
|
public void delete(@RequestParam("filename") String filename) throws IOException {
|
||||||
|
|
||||||
File file = new File(targetDirectory + filename);
|
File file = new File(targetPath + filename);
|
||||||
String canonicalDestinationPath = file.getCanonicalPath();
|
|
||||||
|
|
||||||
if (!canonicalDestinationPath.startsWith(targetDirectory)) {
|
if (!file.toPath().normalize().startsWith(targetPath)) {
|
||||||
throw new IOException("Entry is outside of the target directory");
|
throw new IOException("Entry is outside of the target directory");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user