wpultimatesecurity/wordpress-security-skills

filesystem-security

Use when reading, writing, including, or deleting files from paths that include user input - include / require, readfile, unlink, filegetcontents, or the WPFilesystem API.

View source

How to use

  1. Copy the installation command and review the source package before installing it in your own agent environment.
  2. Check the source document for required tools, credentials and supported hosts.
  3. Describe your input and desired deliverable; review the output against the source material.
npx skills add https://github.com/wpultimatesecurity/wordpress-security-skills --skill filesystem-security

Before you start

  • Installation does not include a model subscription, API allowance or permission to access your files.
  • Repository stars and installation counts describe activity, not an independent quality or security test.
Original skill document

Rendered from the source repository. Headings, examples, code, tables, links, and referenced images are preserved.

Filesystem security

When to use this skill

Use this skill whenever code touches the filesystem with paths derived from input:

  • Building a file path from $_GET / $_POST / $_REQUEST.
  • include, require, readfile, file_get_contents, or unlink of a dynamic file.
  • Serving downloads, exports, or logs based on a request parameter.
  • Deleting files selected by the user.
  • Using WP_Filesystem for writes, deletes, or directory creation.

Path traversal and local file inclusion are high-impact WordPress vulnerabilities — ../../wp-config.php or a malicious uploaded .php can expose secrets or achieve RCE.

Related: see the file-upload-security skill for $_FILES handling and the object-injection-deserialization skill for uploaded data formats.

Core principles (and why they matter)

  1. Never `include` / `require` a user-controlled path. That is remote/local code

execution. Keep templates and includes hardcoded.

  1. Constrain every file operation to a known base directory. Resolve with realpath()

and assert the result starts with the allowed base path.

  1. `validate_file()` catches traversal, drive letters, and `:` characters. A non-zero

return value means the path is suspect.

  1. Use `sanitize_file_name()` and `wp_normalize_path()`. Normalize separators and strip

dangerous characters before building paths.

  1. Use WordPress deletion helpers. wp_delete_file() and wp_delete_file_from_directory()

are safer than raw unlink().

  1. Use `WP_Filesystem` for writes. It handles permissions, credentials, and stream

wrappers consistently across hosts.

Step-by-step implementation

  1. Reject any request that wants to include/require a file based on input; use a hardcoded

allowlist of template files instead.

  1. For read/delete handlers:
  • Verify nonce + capability.
  • Sanitize the filename with sanitize_file_name().
  • Optionally run validate_file().
  • Build the target path under a known base directory.
  • Resolve with realpath() and confirm it stays under the base.
  1. For writes/deletes, prefer WP_Filesystem with request_filesystem_credentials().
  2. Never expose the full server path in errors or URLs.

Supporting references

ReferenceLoad when
Filesystem security checklistBefore final verification of the filesystem security controls.
Secure filesystem operationsImplementing confined file reads and deletion, and writes through WP_Filesystem.

Common AI mistakes / anti-patterns

Mistake 1 — include $_GET['page']

php
// ❌ Insecure: local/remote code execution.
include 'templates/' . $_GET['page'] . '.php';
php
// ✅ Secure: hardcoded allowlist of template files.
$allowed = array( 'dashboard', 'settings', 'logs' );
$page    = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : 'dashboard';
if ( ! in_array( $page, $allowed, true ) ) {
    wp_die( esc_html__( 'Invalid page.', 'my-plugin' ), 400 );
}
include __DIR__ . '/templates/' . $page . '.php';

Mistake 2 — unlink() of a user-named file

php
// ❌ Insecure: arbitrary file deletion via traversal.
unlink( WP_CONTENT_DIR . '/exports/' . $_POST['file'] );
php
// ✅ Secure: sanitize, resolve, and confine to the base directory.
$base    = realpath( WP_CONTENT_DIR . '/exports' );
$request = sanitize_file_name( wp_unslash( $_POST['file'] ?? '' ) );
$target  = realpath( $base . '/' . $request );

if ( false === $target || strpos( $target, $base . DIRECTORY_SEPARATOR ) !== 0 ) {
    wp_die( esc_html__( 'Invalid file.', 'my-plugin' ), 400 );
}

if ( ! current_user_can( 'manage_options' ) ) {
    wp_die( esc_html__( 'Forbidden.', 'my-plugin' ), 403 );
}

wp_delete_file( $target );

Mistake 3 — readfile() without base-directory containment

php
// ❌ Insecure: ../../wp-config.php is reachable.
readfile( '/var/www/uploads/' . $_GET['file'] );
php
// ✅ Secure: resolve and confine.
$base    = realpath( wp_upload_dir()['basedir'] );
$request = sanitize_file_name( wp_unslash( $_GET['file'] ?? '' ) );
$target  = realpath( $base . '/' . $request );

if ( false === $target || strpos( $target, $base . DIRECTORY_SEPARATOR ) !== 0 ) {
    wp_die( esc_html__( 'Invalid file.', 'my-plugin' ), 400 );
}

readfile( $target );

Mistake 4 — Trusting basename() alone

php
// ❌ Insecure: basename does not prevent traversal to siblings.
$file = basename( $_GET['file'] );
readfile( '/var/www/uploads/' . $file );
php
// ✅ Secure: basename plus realpath containment.
$base    = realpath( '/var/www/uploads' );
$file    = basename( sanitize_file_name( wp_unslash( $_GET['file'] ?? '' ) ) );
$target  = realpath( $base . '/' . $file );
if ( false === $target || strpos( $target, $base . DIRECTORY_SEPARATOR ) !== 0 ) {
    wp_die( esc_html__( 'Invalid file.', 'my-plugin' ), 400 );
}
readfile( $target );

Mistake 5 — Writing files with raw PHP functions

php
// ❌ Insecure: no permission/credential handling, may fail on restrictive hosts.
file_put_contents( $path, $data );
php
// ✅ Secure: use WP_Filesystem after requesting credentials.
function my_plugin_write_file( $path, $data ) {
    global $wp_filesystem;

    if ( ! function_exists( 'request_filesystem_credentials' ) ) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }

    $url   = admin_url( 'admin-post.php?action=my_plugin_save' );
    $creds = request_filesystem_credentials( $url );
    if ( false === $creds || ! WP_Filesystem( $creds ) ) {
        return new WP_Error( 'filesystem', __( 'Could not initialize filesystem.', 'my-plugin' ) );
    }

    return $wp_filesystem->put_contents( $path, $data, FS_CHMOD_FILE );
}

Correct code examples

A complete safe file download/delete helper using base-directory containment is in `references/secure-filesystem-operations.php`.

Checklist

  • [ ] No include / require uses user input; templates are selected from a hardcoded allowlist.
  • [ ] File paths are built under a known base directory.
  • [ ] realpath() resolves the final path and containment is verified.
  • [ ] Filenames pass through sanitize_file_name() and wp_normalize_path().
  • [ ] validate_file() returns 0 for paths that include user input.
  • [ ] File delete operations use wp_delete_file() or wp_delete_file_from_directory().
  • [ ] File writes use WP_Filesystem with request_filesystem_credentials().
  • [ ] State-changing handlers verify nonce + capability first.
  • [ ] Full server paths are not exposed in errors or URLs.

Official references

Related by task

from this repository

More skills

All skills
wpultimatesecurity
Community

capability-permission-checks

Use when adding admin pages, menu items, AJAX/REST handlers, action links, or any code that performs a privileged operation in WordPress. Gates actions with currentusercan() using the correct capability (not roles), including per-object checks like editpost, and pairs the check with a nonce. Prevents privilege escalation and broken access control. Apply proactively to every privileged code path.

installs
1
GitHub stars
50
Updated
Sep 29
wpultimatesecurity
Community

multisite-security

Use when writing code that runs on a WordPress multisite network - switchtoblog, network admin pages, getsites, or capabilities that differ between site and network scope. Uses managenetwork / managenetworkoptions and issuperadmin correctly, restores context with restorecurrentblog, isolates per-site data, and never trusts a blog id from input. Prevents cross-site data leakage and network privilege escalation.

installs
1
GitHub stars
50
Updated
Sep 29
wpultimatesecurity
Community

nonces-csrf-protection

Use when handling any form submission, AJAX request, admin-post action, settings page, link that triggers an action, or any other user-initiated request in a WordPress plugin or theme. Generates nonces with wpnoncefield / wpcreatenonce and verifies them with checkadminreferer, checkajaxreferer, or wpverifynonce, always paired with a capability check, to prevent CSRF. Apply proactively whenever code accepts or acts on a request.

installs
1
GitHub stars
50
Updated
Sep 29
wpultimatesecurity
Community

secrets-credentials-management

Use when handling passwords, API keys, tokens, or third-party credentials in a WordPress plugin or theme. Hashes passwords with wphashpassword / wpcheckpassword, generates tokens with wpgeneratepassword, keeps secrets out of code and the database in plaintext, and uses Application Passwords for API auth. Prevents credential leakage and insecure storage.

installs
1
GitHub stars
50
Updated
Sep 29