Path Traversal in TypeSpec OpenAPI3 Emitter
The @typespec/openapi3 emitter fails to sanitize version-derived path tokens, allowing arbitrary file creation or overwrite outside the intended output directory.
The @typespec/openapi3 emitter package is vulnerable to a path traversal flaw caused by improper sanitization of user-controlled input. Specifically, the value of a @versioned enum member in a TypeSpec (.tsp) file is interpolated directly into the output filename template without checking for path traversal characters (..) or absolute path indicators. During the emission process, the TypeSpec compiler's emitFile function performs no validation to ensure the resolved path remains within the designated emitterOutputDir.
An attacker capable of providing a malicious .tsp file to the compiler can force the application to write or overwrite files anywhere on the local filesystem that the compiler process has write access to. While the written content is constrained to valid OpenAPI YAML or JSON structures, the ability to overwrite configuration files or sensitive application data allows for potential denial-of-service or system instability. This vulnerability affects the TypeSpec compiler stack, specifically openapi3 and compiler versions 1.15.0 and earlier.
Attack Chain
- Attacker crafts a malicious TypeSpec source file (
.tsp) containing a@versionedenum member. - The attacker sets a version value containing directory traversal sequences, such as
../../../../tmp/target_file. - The attacker invokes the TypeSpec compiler on the malicious source file.
- The
openapi3emitter reads the crafted version value during the serialization of the OpenAPI specification. - The compiler performs path interpolation using the malicious version string, constructing a path that points outside the
emitterOutputDir. - The
emitFileutility executes the file write operation using the resolved traversal path. - The target file on the host filesystem is overwritten with the generated OpenAPI YAML/JSON content.
Impact
Successful exploitation allows for arbitrary file creation or overwrite, leading to file corruption and potential denial-of-service scenarios. The impact is limited to the compiler process's filesystem permissions and the file formats supported by the emitter, precluding direct remote code execution or unauthorized file disclosure in the documented exploitation path.
Recommendation
Prioritize the implementation of path sanitization within the TypeSpec compiler emission pipeline.
- Implement logic in the
openapi3emitter to resolve the absolute path of the output file after token interpolation and verify it resides within theemitterOutputDir. - Slugify or strictly validate all filename tokens derived from spec attributes to strip path separators, absolute path prefixes, and traversal components.
- Audit and patch
packages/compiler/src/core/emitter-utils.tsto enforce filesystem containment checks during thewriteFileroutine. - Review all TypeSpec projects for usage of externally supplied or untrusted input in
@versionedenum definitions.
Immediate actions
Audit build pipelines for TypeSpec compiler execution environments to ensure they run with least-privilege service accounts.
Mitigations
Monitor for unexpected file write activity in build directories and ensure build environments are ephemeral.
File overwrite via TypeSpec compiler