Security

From static-web-server/static-web-server · View interactive version

Security is an AI agent skill from the GitHub repository static-web-server/static-web-server. Review or implement security-sensitive code in the Static Web Server (SWS) project — path traversal defenses, symlink and hidden-file policy, TLS, security headers, CORS, basic auth, proxy headers, connection limits, untrusted input parsing, and vulnerability handling. Use when touching fs/path.rs,

How to use this skill

Skill file

Security

SWS serves files from disk to untrusted clients. Every change on the request path is security-relevant.

When to load: editing src/fs/path.rs, src/static_files/ (especially security.rs, resolve.rs), src/cors.rs, src/basic_auth.rs, src/tls.rs, src/security_headers.rs, src/logger/access/, src/logger/addr.rs, src/logger/trace_context/, src/redirects.rs, src/rewrites.rs, src/markdown.rs, src/directory_listing/download.rs, src/mem_cache/, or handling a vulnerability report.

Principles

Path Handling Layers

Order as executed in static_files::handle():

  1. Method allowlist: GET, HEAD, OPTIONS (exts/http.rs), checked in handler.rs and again in static_files. Others → 405
  2. Sanitization: fs::path::sanitize_path(base, uri_path) percent-decodes, then rebuilds component by component, dropping .., ., root and Windows prefixes. Output is lexically under base
  3. Memory cache lookup (GET/HEAD only): a hit serves content that already passed layers 4–5 when it was inserted
  4. Resolution: resolve::file_metadata() picks index files, .html suffix fallback, pre-compressed variants
  5. security::enforce(file_path, is_dir, opts), run on the file actually served (index file, .html sibling, and the pre-compressed variant separately):

1. Containment: canonicalize() the path, require the canonical base as prefix. Escape → 404. Positive results are cached per thread (bounded size, wholesale TTL eviction); the TTL bounds how long a directory swapped for a symlink stays trusted

2. Symlink policy (when --follow-symlinks is off): symlink_metadata() on every component. Symlink → 403; check error → 404

3. Hidden files (when --include-hidden is off): any component starting with . → 404. So is an 8.3 short-name alias (GIT~1 for .git on NTFS and FAT): a component containing ~ with no same-name entry, ignoring case, in its parent directory. On macOS it also ignores Unicode normalization: APFS, HFS+ and macOS FAT open é in either form but list it as stored. A name matched that way must open the inode listed for the entry, since the macOS NTFS driver opens short names and an entry in another form would hide a non-ASCII alias such as RÉSUMÉ~1. Elsewhere normalization is kept, since Linux vfat has the same problem. Only ~ components cost a directory read; an unlistable parent → 404

Containment is check-then-use: a symlink retargeted between check and open can still escape (documented in fs::path::is_path_within_base). Don't widen the window by adding I/O between enforce() and opening the file.

The base must already be canonical (root in server/opts.rs, virtual-host roots in settings/). With --use-relative-root the root is resolved at request time; keep containment intact there.

Other path consumers

Every feature that maps a request to a file reuses these layers:

Basic Auth

CORS

TLS

Security Headers

--security-headers defaults to true in builds with the tls feature, false otherwise. When enabled, every response gets:

| Header | Value |

|--------|-------|

| X-Frame-Options | DENY |

| X-Content-Type-Options | nosniff |

| Content-Security-Policy | frame-ancestors 'self' |

| Referrer-Policy | strict-origin-when-cross-origin |

| Strict-Transport-Security | max-age=63072000; includeSubDomains, only with --tls (RFC 6797 forbids it over plain HTTP) |

Behind a TLS-terminating proxy, HSTS is added at the proxy or with an [[advanced.headers]] rule. Custom headers run after this step and can override any value.

Connection and Request Limits

Untrusted Input

Inputs: URI path and query, Host, Origin, Accept, Accept-Encoding, Range, If-*, Authorization, Sec-Fetch-Dest, User-Agent, Referer, X-Forwarded-For / X-Real-IP.

Vulnerability Handling

Secrets

Never commit private keys, .env files, or configs with real credentials. Test keys in tests/tls/ are for tests only. Recommend chmod 600 for TLS keys in docs.

Review Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.