Prevent Path Traversal in File Operations and Includes
Share
User input in include/require or file read/write paths allows attackers to read wp-config.php, delete files, or execute arbitrary PHP via ../ sequences. [CWE-22 · A01:2021]
Why This Matters
arbitrary file read, write, or deletion on the server including wp-config.php
Prevent Path Traversal in File Operations and Includes
Impact: CRITICAL (arbitrary file read, write, or deletion on the server including wp-config.php)
Any file operation that uses user-supplied input — include, require, readfile, file_get_contents, unlink — without path validation is vulnerable to traversal attacks. An attacker sends ../../wp-config.php to read database credentials, or ../../.htaccess to modify access rules.
CVE-2024-10470 (WPLMS theme, CVSS 9.8) — unauthenticated path traversal via a download parameter used directly in readfile() and unlink(), allowing attackers to delete wp-config.php and trigger a WordPress reinstall. CVE-2024-9047 (WordPress File Upload plugin) — unauthenticated file read and deletion via path traversal.
Incorrect (user input in file paths):
// ❌ Template inclusion with user-controlled value$template = $_GET['template'];include WP_CONTENT_DIR . '/plugins/my-plugin/templates/' . $template . '.php';// Attacker: ?template=../../../../wp-config → reads wp-config.php// ❌ File download with user-supplied filename$file = $_GET['download'];$path = WP_CONTENT_DIR . '/uploads/' . $file;if ( file_exists( $path ) ) { readfile( $path ); // Reads any file on the server}// ❌ File deletion with user input$filename = $_POST['file'];unlink( WP_CONTENT_DIR . '/exports/' . $filename );// Attacker: file=../../../wp-config.php → deletes wp-config.php