Summary
PhpSpreadsheet improperly trusts user-controlled filenames to IOFactory::load(), allowing attacker-supplied PHP stream wrapper paths such as phar://, ftp://, or ssh2.sftp:// to pass file checks and be processed.
This can lead to unsafe PHAR metadata deserialization and potential remote code execution, or server-side request forgery to arbitrary network endpoints that the server can reach.
Affected context
The vulnerability arises in PHP applications that:
- Use PhpSpreadsheet to load spreadsheet files via IOFactory::load()
- Accept a filename or file path value that is directly or indirectly influenced by user input
- Do not normalize or constrain the filename to trusted directories or schemes
- Run in an environment where PHAR handling is enabled and/or outbound network access from the server is possible
In such setups, stream wrapper paths like phar://, ftp://, or ssh2.sftp:// may be treated as regular files and processed by PhpSpreadsheet.
Exploit preconditions
- Attack surface: Web endpoints or APIs that invoke
IOFactory::load()on attacker-controlled filename arguments. - Required access: Ability to supply or influence the filename/path parameter (e.g., query string, form field, JSON body, or indirect configuration).
- Trigger point: Call to
IOFactory::load($filename)where$filenameis not restricted to trusted, local file paths. - Why exploitation is possible:
- PhpSpreadsheet's file assertion logic relies on
is_file()semantics, which can succeed for certain PHP stream wrappers. - The
phar://wrapper causes PHP to deserialize PHAR metadata, which can invoke gadget chains present in the application codebase (CWE-502). - Network-capable wrappers such as
ftp://orssh2.sftp://can cause the server to initiate outbound connections, enabling SSRF (CWE-918).
Why this happens
The core issue is a combination of unsafe deserialization and insufficient restriction of resource identifiers:
- Unsafe Deserialization (CWE-502): PHAR archives embed metadata that PHP automatically deserializes when accessed via the
phar://stream wrapper. When an application accepts untrusted paths that can point to attacker-controlled PHAR files, the deserialization of metadata can trigger gadget chains and arbitrary code execution. - Server-Side Request Forgery (CWE-918): By not constraining the types of URIs that can be passed as filenames, the library allows network-capable stream wrappers (e.g.,
ftp://,ssh2.sftp://). This lets attackers coerce the server into making arbitrary outbound requests to internal or external endpoints.
Conceptually, the bug is that the filename parameter is treated as a trustworthy local file path, yet it is in fact a general resource locator whose scheme and target are attacker-controllable. The library's validation logic does not sufficiently restrict schemes, nor does it account for the side effects of invoking PHAR or network wrappers, leading to both deserialization and SSRF risks.
Potential impact
- Remote code execution on the application server if a suitable PHAR gadget chain is present.
- Execution of arbitrary PHP object graphs during PHAR metadata deserialization.
- Server-side request forgery to internal services (e.g., databases, admin consoles, cloud metadata services).
- Outbound connections to attacker-controlled servers, enabling data exfiltration or pivoting into internal networks.
- Abuse of server identity in requests (e.g., reaching whitelisted services from a trusted IP).
- Application-level data exposure if internal HTTP endpoints return sensitive information.
General mitigation guidance
Effective mitigation focuses on strictly controlling how filenames are constructed and which protocols are allowed, as well as reducing the impact of deserialization:
- Treat all user-supplied or user-influenced filenames as untrusted input; never pass them directly to
IOFactory::load()or similar file-loading APIs. - Enforce an allowlist of acceptable file schemes and locations, typically:
- Restrict to plain local filesystem paths (e.g., no
://allowed). - Constrain file loading to a predefined directory under application control.
- Normalize and validate paths before use (e.g., remove or reject path traversal segments, reject any stream wrapper syntax).
- Avoid relying on PHAR functionality where not strictly necessary; disable automatic PHAR handling at the application or runtime level if feasible.
- Apply least-privilege principles to the application runtime:
- Minimize write access to directories where PHAR payloads could be stored.
- Limit outbound network connectivity only to required destinations.
- Consider object deserialization hardening:
- Reduce or remove unnecessary classes and magic methods that can act as gadget chains.
- Use coding patterns that avoid dangerous behaviors in constructors, destructors, or
__wakeup()/__toString()methods.
How teams detect issues like this
Detection usually combines code review, static analysis, and runtime monitoring:
- Static analysis and code scanning:
- Flag calls to
IOFactory::load(), or other file-loading routines, where the argument can originate from HTTP request data, environment variables, or other untrusted sources. - Detect uses of
phar://,ftp://,ssh2.sftp://, and other stream wrappers, especially when paths are concatenated with user-controlled data. - Look for generic file access APIs (
file_get_contents,fopen, etc.) that accept unsanitized external input. - Code review signals:
- File upload or import features that immediately pass filenames to PhpSpreadsheet without normalization or scheme checks.
- Business logic that stores user input into configuration or temporary paths later used as filenames.
- Lack of a centralized abstraction for safe file handling.
- Policy and tooling:
- Enforce secure file-handling guidelines in coding standards and CI checks.
- Use automated security review tools or SAST platforms configured with rules for PHP stream wrappers and unsafe deserialization patterns.
- Monitor runtime logs and network telemetry for suspicious outbound connections from the application process.

