Summary
A path traversal flaw in the unstructured library's partition_msg function allows crafted MSG files with malicious attachment paths to write or overwrite arbitrary files on the host filesystem.
When applications process untrusted MSG documents through this component, an attacker-controlled attachment path can escape the intended directory and target sensitive files, leading to broad system compromise.
Affected context
This vulnerability affects applications that integrate the unstructured library to ingest and pre-process MSG email files, particularly when:
- MSG files are accepted from untrusted or semi-trusted sources (e.g., user uploads, email gateways, ETL pipelines).
- Attachments in MSG files are automatically extracted or written to disk.
- The destination paths for attachments are derived from values inside the MSG file without strict normalization and validation.
The issue manifests during processing of malicious MSG files whose attachments encode directory traversal sequences or absolute paths that the library does not safely constrain.
Exploit preconditions
- Attack surface: Any service, batch job, or pipeline that calls
partition_msgon MSG files supplied directly or indirectly by an attacker. - Required access: Ability to submit or influence MSG files processed by the application (e.g., file upload, email ingestion, shared storage, or upstream data feeds).
- Trigger point: Invocation of
partition_msgon a crafted MSG file containing attachments with specially formed filenames or paths. - Why exploitation is possible: The attachment extraction logic trusts path information derived from the MSG content and writes files without fully resolving, normalizing, and enforcing a safe base directory, enabling traversal outside the intended location.
Why this happens
The root cause is insufficient validation and normalization of filesystem paths when writing email attachments extracted from MSG files.
The partition_msg function appears to construct output paths directly or semi-directly from attachment metadata, such as filenames or paths embedded in the MSG, without:
- Canonicalizing the final path (resolving
.., symlinks, and relative components). - Enforcing that the resolved path remains within an expected base directory.
This design maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), and is related to CWE-73: External Control of File Name or Path, where externally controlled data influences the destination of file operations.
As a result, an attacker can cause the library to write or overwrite arbitrary locations on the filesystem reachable by the running process.
Potential impact
- Overwriting application configuration files, logs, or data files that the process can write to.
- Planting or replacing files used by the application or other services, potentially leading to remote code execution if those files are later executed or interpreted (e.g., scripts, templates, or configuration with embedded code).
- Corrupting or tampering with data used for analytics, machine learning pipelines, or document processing.
- Assisting in privilege escalation or lateral movement when combined with other weaknesses, such as insecure file loading or permissive service configurations.
- Interfering with system availability by overwriting critical files or filling up disk space.
General mitigation guidance
Effective mitigation centers on strictly constraining where and how attachments are written:
- Enforce a fixed base directory for all extracted attachments and ensure all paths are resolved relative to this directory, never using caller or input-controlled absolute paths.
- Normalize and validate paths before file operations, resolving
.and..components and verifying that the canonical path remains within the designated safe directory. - Discard or sanitize unsafe filenames, rejecting or rewriting attachments whose names contain directory traversal sequences, absolute path prefixes, or other suspicious patterns.
- Apply least-privilege file system permissions, running the processing component under an account with minimal write capabilities and restricting write access to a narrow, non-sensitive directory.
- Isolate the processing environment, for example via containers, chroot jails, or sandboxing techniques so that even if traversal occurs, it is confined to a reduced-impact scope.
- Avoid implicit trust of document metadata, treating filenames and paths embedded in MSG or other containers as untrusted input rather than authoritative instructions.
How teams detect issues like this
Detection can combine code-level analysis with runtime monitoring:
- Static analysis and code review:
- Look for calls in the codebase where data derived from MSG attachments or other untrusted sources flows into file write operations (e.g.,
open,write,save) without normalization or boundary checks. - Flag patterns where filenames from document metadata are concatenated with directory paths directly, especially when using
os.path.joinor equivalent without subsequent canonicalization and enforcement of a safe root. - Verify that any helper utilities wrapping
partition_msgenforce path constraints and do not expose raw attachment names as filesystem paths. - Policy and CI checks:
- Introduce automated checks that disallow use of unvalidated external filenames in file operations, enforcing centralized path-handling helpers.
- Integrate SAST tools or linters configured with rules for CWE-22 and CWE-73 to spot unsanitized path usage early in the development lifecycle.
- Runtime and operational monitoring:
- Monitor for unexpected file creation or modification outside known attachment or temp directories during MSG processing.
- Use system auditing frameworks to detect attempts to write into sensitive locations (e.g., configuration directories, application code paths) by the document-processing service account.

