Path Traversal
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หมวดหมู่ OWASP: MASVS-STORAGE: พื้นที่เก็บข้อมูล
ภาพรวม
ช่องโหว่การข้ามเส้นทางเกิดขึ้นเมื่อผู้โจมตีสามารถควบคุมส่วนหนึ่งของเส้นทางซึ่งส่งไปยัง File System API โดยไม่ผ่านการตรวจสอบ ซึ่งอาจนำไปสู่การดําเนินการระบบไฟล์ที่ไม่ได้รับอนุญาต ตัวอย่างเช่น ผู้โจมตีอาจใช้สัญลักษณ์พิเศษอย่าง ../
เพื่อเปลี่ยนเป้าหมายทรัพยากรโดยไม่คาดคิดโดยการข้ามผ่านไดเรกทอรีเป้าหมาย
ผลกระทบ
ผลกระทบจะแตกต่างกันไปตามการดำเนินการและเนื้อหาของไฟล์ แต่โดยทั่วไปแล้วจะนำไปสู่การเขียนไฟล์ทับ (เมื่อเขียนไฟล์) การรั่วไหลของข้อมูล (เมื่ออ่านไฟล์) หรือการเปลี่ยนแปลงสิทธิ์ (เมื่อเปลี่ยนสิทธิ์ของไฟล์หรือไดเรกทอรี)
การลดปัญหา
กําหนดเส้นทางเป็นหน้า Canonical โดยใช้ 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;
}
แนวทางปฏิบัติแนะนำเพิ่มเติมคือการใช้การตรวจสอบเพื่อให้แน่ใจว่าจะเกิดเฉพาะผลลัพธ์ที่คาดไว้เท่านั้น ตัวอย่างมีดังต่อไปนี้
- ตรวจสอบว่ามีไฟล์อยู่แล้วหรือไม่เพื่อป้องกันการเขียนทับโดยไม่ตั้งใจ
- ตรวจสอบว่าไฟล์เป้าหมายเป็นเป้าหมายที่คาดหวังไว้เพื่อป้องกันข้อมูลรั่วไหลหรือการเปลี่ยนแปลงสิทธิ์อย่างไม่ถูกต้องหรือไม่
- ตรวจสอบว่าไดเรกทอรีปัจจุบันของการดำเนินการตรงกับที่คาดไว้ทุกประการในค่าที่แสดงผลจากเส้นทางตามแบบแผนหรือไม่
- ตรวจสอบว่าระบบสิทธิ์กําหนดขอบเขตไว้อย่างชัดเจนสําหรับการดำเนินการ เช่น ตรวจสอบว่าไม่ได้เรียกใช้บริการในฐานะรูท และตรวจสอบว่าสิทธิ์ไดเรกทอรีกําหนดขอบเขตไว้สําหรับบริการหรือคําสั่งที่ระบุ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-26 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."]]