cwe-22-path-traversal

Remediate CWE-22 path traversal vulnerabilities in Java file I/O code.

3|1|Updated Mar 6, 2026
One-click install
npx skills add https://github.com/DevelopersCoffee/java-cwe-security-skills --skill cwe-22-path-traversal
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cwe-22-path-traversal
Source: https://github.com/DevelopersCoffee/java-cwe-security-skills/tree/main/cwe-22-path-traversal
Command: npx skills add https://github.com/DevelopersCoffee/java-cwe-security-skills --skill cwe-22-path-traversal

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses and remediates CWE-22 (Improper Limitation of a Pathname to a Restricted Directory), commonly known as Path Traversal vulnerabilities in Java applications, preventing unauthorized access to sensitive files.

Core Features & Use Cases

  • Vulnerability Identification: Detects patterns indicative of path traversal risks in file access operations.
  • Deterministic Remediation: Provides specific code transformations to secure file path handling.
  • Use Case: When a SAST tool flags a potential path traversal vulnerability in a file download endpoint, this skill can be applied to automatically refactor the code to safely handle file paths.

Quick Start

Use the cwe-22-path-traversal skill to secure the file access logic in the provided Java code snippet.

Frequently Asked Questions about cwe-22-path-traversal

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix CWE-22 path traversal vulnerabilities in Java file download endpoints?

Path traversal vulnerabilities occur when Java file I/O operations fail to restrict directory access, allowing attackers to use traversal sequences to reach unauthorized files outside the intended base directory.

How do I secure file path handling in Java applications against directory traversal attacks?

Securing file path handling requires implementing canonical path validation to confirm the resolved path stays within allowed boundaries, using allowlists for permitted files, and rejecting traversal sequences in user input.

Can I use automated remediation when a SAST tool flags path traversal risks in my Java code?

Yes, when a SAST tool flags path traversal risks in Java file access operations, automated remediation can refactor the code by applying deterministic transformations to safely handle and validate file paths.

What is the best way to prevent unauthorized file access in Java web controllers?

The best way to prevent unauthorized file access in Java web controllers is to implement secure file path handling that validates canonical paths and rejects directory traversal sequences before processing file I/O operations.

Why does my Java application still allow access to sensitive files after basic input validation?

Basic input validation often fails to prevent path traversal because it does not verify canonical paths; secure remediation requires confirming the final resolved path remains within the restricted directory boundary.

Does path traversal remediation work for all Java service layers with file I/O operations?

Path traversal remediation applies to Java applications with file I/O operations across web controllers and service layers, specifically targeting file download endpoints and file access logic requiring canonical path validation.