Why this matters
Applications that disclose file existence based on user input can be exploited to infer filesystem structure. Ensure user input is properly validated and sanitized.
Ensure that the application does not disclose file existence based on user input to prevent filesystem oracle attacks.
Applications that disclose file existence based on user input can be exploited to infer filesystem structure. Ensure user input is properly validated and sanitized.
Side-by-side examples engineers can pattern-match during review.
import java.io.File;
@Controller
public class ExampleController
{
static private String targetDirectory = "/path/to/target/directory/";
@GetMapping(value = "/exists")
public void delete(@RequestParam("filetitle") String filetitle) throws IOException {
File file = new File(targetDirectory + filetitle);
if (!file.exists()) { // Noncompliant
throw new IOException("File does not exists in the target directory");
}
}
}import java.io.File;
@Controller
public class ExampleController
{
static private String targetDirectory = "/path/to/target/directory/";
@GetMapping(value = "/exists")
public void delete(@RequestParam("filetitle") String filetitle) throws IOException {
File file = new File(targetDirectory + filetitle);
String canonicalDestinationPath = file.getCanonicalPath();
if (!canonicalDestinationPath.startsWith(targetDirectory)) {
throw new IOException("Entry is outside of the target directory");
} else if (!file.exists()) {
throw new IOException("File does not exists in the target directory");
}
}
}import java.io.File;
@Controller
public class ExampleController
{
static private String targetDirectory = "/path/to/target/directory/";
@GetMapping(value = "/exists")
public void delete(@RequestParam("filetitle") String filetitle) throws IOException {
File file = new File(targetDirectory + filetitle);
if (!file.exists()) { // Noncompliant
throw new IOException("File does not exists in the target directory");
}
}
}import java.io.File;
@Controller
public class ExampleController
{
static private String targetDirectory = "/path/to/target/directory/";
@GetMapping(value = "/exists")
public void delete(@RequestParam("filetitle") String filetitle) throws IOException {
File file = new File(targetDirectory + filetitle);
String canonicalDestinationPath = file.getCanonicalPath();
if (!canonicalDestinationPath.startsWith(targetDirectory)) {
throw new IOException("Entry is outside of the target directory");
} else if (!file.exists()) {
throw new IOException("File does not exists in the target directory");
}
}
}From the same buckets as this rule.