パス トラバーサル
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
OWASP カテゴリ: MASVS-STORAGE: ストレージ
概要
パス トラバーサルの脆弱性は、検証なしでファイルシステム API に渡されるパスの一部を攻撃者が制御できる場合に発生します。これにより、不正なファイル システム操作が行われる可能性があります。たとえば、攻撃者が「../
」などの特殊文字を使用してターゲット ディレクトリの外に移動し、突然リソース ターゲットを変更してしまう場合があります。
影響
影響は、操作とファイルの内容によって異なりますが、一般にファイルの上書き(ファイルの書き込み時)、データ漏洩(ファイルの読み取り時)、権限の変更(ファイルやディレクトリの権限の変更時)が発生します。
リスクの軽減
File.getCanonicalPath()
を使用してパスを正規化し、プレフィックスを所定のディレクトリと比較します。
Kotlin
@Throws(IllegalArgumentException::class)
fun saferOpenFile(path: String, expectedDir: String?): File {
val f = File(path)
val canonicalPath = f.canonicalPath
require(canonicalPath.startsWith(expectedDir!!))
return f
}
Java
public File saferOpenFile (String path, String expectedDir) throws IllegalArgumentException {
File f = new File(path);
String canonicalPath = f.getCanonicalPath();
if (!canonicalPath.startsWith(expectedDir)) {
throw new IllegalArgumentException();
}
return f;
}
その他にも、検証を使用して、所定の結果のみが発生していることを確認することもおすすめします。以下に例を示します。
- 誤って上書きされないように、ファイルがすでに存在するかどうかを確認する。
- データの漏洩や権限が不正に変更されるのを防ぐために、ターゲット ファイルが所定のターゲットであるかどうかを確認する。
- 操作のカレント ディレクトリが、正規パスからの戻り値で正確に想定どおりかどうかを確認する。
- 権限システムのスコープを操作に明示的に設定する(サービスを root として実行していないことを確認する、ディレクトリ権限のスコープを指定されたサービスまたはコマンドに設定するなど)。
あなたへのおすすめ
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2024-02-23 UTC。
[null,null,["最終更新日 2024-02-23 UTC。"],[],[],null,["# Path traversal\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-STORAGE: Storage](https://mas.owasp.org/MASVS/05-MASVS-STORAGE)\n\nOverview\n--------\n\nPath traversal vulnerabilities occur when an attacker can control part of the\npath that is then passed to the file system APIs without validation. This can\nlead to unauthorized file system operations. For example, an attacker might use\nspecial characters such as `../` to unexpectedly change the resource target, by\ntraversing outside of the targeted directory.\n\nImpact\n------\n\nThe impact varies depending on the operation and file content, but generally\nleads to a file overwrite (when writing files), data leak (when reading files),\nor permission changes (when changing file or directory permissions).\n\nMitigations\n-----------\n\nCanonicalize the path using [`File.getCanonicalPath()`](/reference/java/io/File#getCanonicalPath()) and compare the\nprefix with the expected directory: \n\n### Kotlin\n\n @Throws(IllegalArgumentException::class)\n fun saferOpenFile(path: String, expectedDir: String?): File {\n val f = File(path)\n val canonicalPath = f.canonicalPath\n require(canonicalPath.startsWith(expectedDir!!))\n return f\n }\n\n### Java\n\n public File saferOpenFile (String path, String expectedDir) throws IllegalArgumentException {\n File f = new File(path);\n String canonicalPath = f.getCanonicalPath();\n if (!canonicalPath.startsWith(expectedDir)) {\n throw new IllegalArgumentException();\n }\n return f;\n }\n\nAn additional best practice is to use validation to ensure only expected\noutcomes occur. Examples include the following:\n\n- Checking if the file already exists to prevent an accidental overwrite.\n- Checking if the targeted file is an expected target to prevent leaking data or incorrectly changing permissions.\n- Checking if the current directory of the operation is exactly as expected in the return value from the canonical path.\n- Ensuring a permissions system is explicitly scoped to the operation, such as checking that it isn't running services as root, and ensuring that the directory permissions are scoped to the service or command specified."]]