agentleFS
wpultimatesecuritywpultimatesecurity/WordPress-Security-Skills/skills/filesystem-security/SKILL.md

filesystem-security

Skill45 starsChanged 3 months agoMITView on GitHub

Use when reading, writing, including, or deleting files from paths that include user input - include / require, readfile, unlink, file_get_contents, or the WP_Filesystem API. Validates paths with validate_file, normalizes with wp_normalize_path, confines operations to an allowed base directory, and uses wp_delete_file / WP_Filesystem. Prevents path traversal, local file inclusion, and arbitrary file deletion.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

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

    line 20
  2. Distinctive:

    Never expose the full server path in errors or URLs.

    line 60

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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.
  2. Constrain every file operation to a known base directory. Resolve with realpath() and assert the result starts with the allowed base path.
  3. validate_file() catches traversal, drive letters, and : characters. A non-zero return value means the path is suspect.
  4. Use sanitize_file_name() and wp_normalize_path(). Normalize separators and strip dangerous characters before building paths.
  5. Use WordPress deletion helpers. wp_delete_file() and wp_delete_file_from_directory() are safer than raw unlink().
  6. 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.
  2. 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.
  3. For writes/deletes, prefer WP_Filesystem with request_filesystem_credentials().
  4. 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']

// ❌ Insecure: local/remote code execution.
include 'templates/' . $_GET['page'] . '.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

// ❌ Insecure: arbitrary file deletion via traversal.
unlink( WP_CONTENT_DIR . '/exports/' . $_POST['file'] );
// ✅ 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

// ❌ Insecure: ../../wp-config.php is reachable.
readfile( '/var/www/uploads/' . $_GET['file'] );
// ✅ 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

// ❌ Insecure: basename does not prevent traversal to siblings.
$file = basename( $_GET['file'] );
readfile( '/var/www/uploads/' . $file );
// ✅ 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

// ❌ Insecure: no permission/credential handling, may fail on restrictive hosts.
file_put_contents( $path, $data );
// ✅ 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

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.