Path Security API
The Path Security module validates and sanitizes paths for safe filesystem operations. It prevents path traversal attacks, null byte injection, symlink escapes, and other filesystem exploits when generating skill names and writing skill files.Security Measures
- Path traversal prevention - Blocks
../sequences and absolute path injection - Null byte protection - Strips null bytes (
\0) from paths and filenames - Symlink resolution - Resolves symlinks before validation to prevent escapes
- Spec compliance - Enforces agentskills.io naming spec
- Bounded writes - Ensures all writes stay within allowed directories
Functions
sanitizeName
Sanitize a skill name for safe filesystem and spec-compliant usage. Applies the following transformations:- Unicode normalization (NFKD) and ASCII folding
- Lowercase conversion
- Replace non-alphanumeric chars (except hyphens) with hyphens
- Collapse consecutive hyphens
- Strip leading/trailing hyphens
- Truncate to MAX_NAME_LENGTH (64 chars)
- Ensure result matches agentskills.io spec regex
The unsanitized name string
string - A sanitized, spec-compliant kebab-case name
Throws: Error if the name cannot be sanitized to a valid result
Example
isPathSafe
Check whether a target path is safely contained within an allowed root. Guards against:- Path traversal (
../) - Null bytes in path components
- Symlink escapes (resolves symlinks before comparison)
- Absolute path injection
The path to validate
The directory that must contain
targetboolean - True if target is safely within allowedRoot, false otherwise
Example
isSafeSymlink
Check whether a symlink is safe to create. Ensures both the link location and its target are within allowed boundaries.Where the symlink will be created
What the symlink will point to
The root directory that must contain both paths
boolean - True if the symlink is safe to create, false otherwise
Example
safeWrite
Write content to a file only if the path is safe. Creates parent directories as needed. Uses atomic-write for safety (writes to temp file, then renames).The file content to write
The file path to write to
The directory that must contain
targetstring - The resolved path of the written file
Throws: Error if the target path is not safe
Example
Constants
SPEC_NAME_REGEX
Regular expression for validating skill names per agentskills.io spec.- Must start and end with alphanumeric character
- May contain hyphens in the middle
- Lowercase only
- Examples:
debug-test,git-workflow,api-client
-leading-hyphentrailing-hyphen-UPPERCASEhas_underscorehas.dot
MAX_NAME_LENGTH
Maximum allowed name length per agentskills.io spec.Sanitization Process
ThesanitizeName function applies these transformations in order:
Security Best Practices
When working with file paths in Auto-Skill:- Always sanitize user input - Use
sanitizeName()for any user-provided or derived names - Validate before writing - Use
isPathSafe()before any filesystem operations - Prefer safeWrite - Use
safeWrite()instead of rawfs.writeFileSync() - Check symlinks - Use
isSafeSymlink()before creating cross-agent symlinks - Trust no input - Even internally-generated names should be validated
Related
- Spec Validator - Validates SKILL.md files against agentskills.io spec
- agentskills.io - Official skill specification