← Java / Spring · 目次

JAVA-013 / 初級

ファイルダウンロードのパストラバーサル

A01:2021 Broken Access Control · CWE-22 Path Traversal

なぜ問題になるのか?

../../etc/passwd により任意ファイルを読み取れます。

修正方法

java
Path base = Path.of("/data/files").toRealPath();
Path file = base.resolve(name).toRealPath();
if (!file.startsWith(base) || !Files.isRegularFile(file)) {
    throw new SecurityException();
}

参考資料