Przejdź do treści
Biblioteka skilli
Bezpieczeństwo · Budowanie i kod

filesystem-security

/filesystem-security

Chroni odczyt, zapis i usuwanie plików z ścieżek wejściowych przed przejściem katalogów i dołączaniem plików lokalnych.

Licencja
MIT
Ostatnia zmiana repozytorium
29.09.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
3
Skrypty
1
Komentarze HTML
0
Adresy URL w SKILL.md
10
Przestarzałość

Brak sygnałów. Repozytorium zmienione 29.09.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co sprawdzić przed instalacją
  • 1 skrypt w paczce. Przejrzyj je przed instalacją, bo Claude może je uruchomić.

Skrypty: references/secure-filesystem-operations.php

Co robi i kiedy

Chroni odczyt, zapis i usuwanie plików z ścieżek wejściowych przed przejściem katalogów i dołączaniem plików lokalnych.

Oryginalny opis (EN)

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.

Jak działa

  1. When to use this skill
  2. Core principles (and why they matter)
  3. Step-by-step implementation
  4. Common AI mistakes / anti-patterns
  5. Correct code examples
  6. Checklist
  7. Official references
Sekcje z nagłówków SKILL.md, w oryginale · 192 linii, 952 słów
Podgląd SKILL.md
---
name: filesystem-security
description: >
  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.
compatibility: "Examples generally use PHP 7.4 syntax; check each API against target WordPress/PHP versions. Use maintained WordPress and supported PHP in production. Shell examples require their named tools."
license: MIT
metadata:
  tags: "wordpress, security, php, filesystem, path-traversal, lfi, rce"
---

# 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

| Reference | Load when |
| --- | --- |
| [Filesystem security checklist](references/checklist.md) | Before final verification of the filesystem security controls. |
| [Secure filesystem operations](references/secure-filesystem-operations.php) | Implementing 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`](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

- [`validate_file()`](https://developer.wordpress.org/reference/functions/validate_file/)
- [`wp_normalize_path()`](https://developer.wordpress.org/reference/functions/wp_normalize_path/)
- [`sanitize_file_name()`](https://developer.wordpress.org/reference/functions/sanitize_file_name/)
- [`wp_delete_file()`](https://developer.wordpress.org/reference/functions/wp_delete_file/)
- [`wp_delete_file_from_directory()`](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/)
- [`WP_Filesystem`](https://developer.wordpress.org/reference/classes/wp_filesystem/)
- [`request_filesystem_credentials()`](https://developer.wordpress.org/reference/functions/request_filesystem_credentials/)
- [`wp_mkdir_p()`](https://developer.wordpress.org/reference/functions/wp_mkdir_p/)
- [`realpath()`](https://www.php.net/manual/en/function.realpath.php)
- [OWASP — Path Traversal](https://owasp.org/www-community/attacks/Path_Traversal)
Pliki w paczce (3)
  • SKILL.md 8 KB
  • references/checklist.md 1 KB
  • references/secure-filesystem-operations.php 4 KB

Wersje

W bazie nie ma innych skilli o nazwie filesystem-security.

Migawka bazy z 13.11.2026 · wersja paczki d0900be01b44 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100file-upload-security
    Zabezpiecza wysyłanie plików w WordPressie: sprawdza typ MIME i rozszerzenie, blokuje pliki wykonywalne i ścieżki spoza dozwolonego katalogu.
  • 100filesystem
    Wykonuje operacje na plikach i katalogach wbudowanymi narzędziami Claude Code zamiast serwera MCP Filesystem: odczyt, zapis, edycja, wyszukiwanie i drzewo katalogów.
  • 100filesystem-mcp-guardrails
    Pomaga projektować i przeglądać dostęp MCP do systemu plików: granice ścieżek, dozwolone katalogi główne, listy dozwolonych metod i bezpieczne operacje na plikach lokalnych.
  • 100roots-support
    Wymusza obsługę katalogów roots w serwerach MCP: zakres dostępu do plików i granice ścieżek przy budowie lub przeglądzie kodu.
  • 100security-audit
    Skanuje kod pod kątem podatności: walidacja wejścia, path traversal, wykrywanie CVE i bezpieczne wzorce kodowania, dla uwierzytelniania, płatności, uploadów plików i zapytań do bazy.
  • 100review-critical-paths
    Znajduje i sprawdza krytyczne ścieżki kodu aplikacji: logowanie, trasy API, parsowanie wejścia, płatności, sekrety, zmiany uprawnień i usuwanie danych.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.