Am I affected?

type your gleam.run/gleam version to check

Description

Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') vulnerability in gleam-lang gleam allows a malicious package publisher to delete a directory outside the project's build directory when a developer builds a project.

A Hex package release's metadata carries an app field with the release's OTP application name. Gleam reads this field from the hex.pm release API when it downloads dependencies and uses it as the package's build directory name without validating it (hex_manifest_package in compiler-cli/src/dependencies.rs, ProjectPaths::build_directory_for_package). When a dependency fails to compile, Gleam deletes that directory with remove_dir_all. hex.pm didn't restrict the field to a directory name before 2026-10-12, so a publisher could set it to a value such as ../../../../target or an absolute path. When a project depends on such a release directly or transitively and the release fails to compile, gleam build, gleam check, gleam run, gleam test or gleam export deletes the directory the name resolves to. For a pure Gleam package no publisher code runs.

This issue affects gleam: from 0.32.4 before 1.19.2.

Technical analysis

  1. hex_manifest_package (compiler-cli/src/dependencies.rs) sets the manifest package's otp_app to meta.app from GET packages/<name>/releases/<version> without validation. hex.pm doesn't restrict this field to a directory name. The signed registry record's dependency app label is decoded but never used as a path.
  2. ManifestPackage::application_name (compiler-core/src/manifest.rs) returns otp_app when it is set.
  3. ProjectPaths::build_directory_for_package (compiler-core/src/paths.rs) joins the application name onto the target's build directory. A .. component escapes the build directory, and an absolute path replaces the whole path.
  4. When a dependency fails to compile, ProjectCompiler::load_cache_or_compile_package (compiler-core/src/build/project_compiler.rs) calls delete_directory on that path, which runs std::fs::remove_dir_all (compiler-cli/src/fs.rs).
  5. The manifest built by download_dependencies is passed to build::main in the same run, so the name reaches compilation without a manifest.toml round trip. otp_app is validated against ^[a-z][a-z0-9_]*$ only when manifest.toml is read from disk (commit 2dc0467, first released in v1.16.0), which rejects a stored malicious manifest on a later run but not the first run that produces it.
  6. Extraction uses the package name, which hex.pm validates, so the effect is a directory delete without an overwrite. For a package with build_tools = ["gleam"] no publisher code runs, so source that fails to compile or a malformed gleam.toml triggers the delete. For rebar3 and Mix dependencies, publisher code runs anyway.

Proof of concept

Not reproduced end to end, because Gleam only uses hex.pm and has no setting for another repository. A unit test calling ProjectPaths::build_directory_for_package with the application names ../../../../canary and an absolute path computed build directories outside the project, and std::fs::remove_dir_all, the call behind delete_directory, deleted a canary directory placed there. The chain from meta.app to the delete was established by reading the source.

Weaknesses & attack patterns

Weakness

CWE-22 · Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') in catalog → MITRE ↗

Attack patterns

CAPEC-126 · Path Traversal MITRE ↗

A publisher of a package in a project's dependency tree can make a Gleam build delete a directory that the developer's account can write, outside the project's build directory or anywhere on the filesystem when the application name is an absolute path. For a pure Gleam package this happens without running any of the publisher's code. Developers and CI jobs that build projects with Gleam are affected; gleam deps download alone isn't.

Affected — Gleam

≥ 0.32.4 < 1.19.2 affected
every other version: unaffected
cpe cpe:2.3:a:gleam-lang:gleam:*:*:*:*:*:*:*:*
version type semver
modules · source files · routines
modules gleam_cli::dependencies · gleam_core::manifest · gleam_core::paths · gleam_core::build::project_compiler · gleam_cli::fs
source files compiler-cli/src/dependencies.rs · compiler-core/src/manifest.rs · compiler-core/src/paths.rs · compiler-core/src/build/project_compiler.rs · compiler-cli/src/fs.rs
routines gleam_cli::dependencies::hex_manifest_package · gleam_core::manifest::ManifestPackage::application_name · gleam_core::paths::ProjectPaths::build_directory_for_package · gleam_core::build::project_compiler::ProjectCompiler::load_cache_or_compile_package · gleam_cli::fs::delete_directory

Affected — ghcr.io / gleam-lang/gleam Container registry ↗

≥ v0.32.4-elixir < v1.19.2-elixir affected
≥ v0.32.4-erlang < v1.19.2-erlang affected
≥ v0.32.4-node < v1.19.2-node affected
≥ v0.32.4-node-slim < v1.19.2-node-slim affected
≥ v0.32.4-elixir-slim < v1.19.2-elixir-slim affected
≥ v0.32.4-erlang-slim < v1.19.2-erlang-slim affected
≥ v0.32.4-erlang-alpine < v1.19.2-erlang-alpine affected
≥ v0.32.4-elixir-alpine < v1.19.2-elixir-alpine affected
≥ v0.32.4-node-alpine < v1.19.2-node-alpine affected
≥ v0.32.4-scratch < v1.19.2-scratch affected
every other version: unaffected
cpe cpe:2.3:a:gleam-lang:gleam:*:*:*:*:*:*:*:*
version type other
modules · source files · routines
modules gleam_cli::dependencies · gleam_core::manifest · gleam_core::paths · gleam_core::build::project_compiler · gleam_cli::fs
source files compiler-cli/src/dependencies.rs · compiler-core/src/manifest.rs · compiler-core/src/paths.rs · compiler-core/src/build/project_compiler.rs · compiler-cli/src/fs.rs
routines gleam_cli::dependencies::hex_manifest_package · gleam_core::manifest::ManifestPackage::application_name · gleam_core::paths::ProjectPaths::build_directory_for_package · gleam_core::build::project_compiler::ProjectCompiler::load_cache_or_compile_package · gleam_cli::fs::delete_directory

Affected — GitHub / gleam-lang/gleam Repository ↗

≥ d13f27e and up affected
→ d15c981 not affected
→ 54bbb2b not affected
every other version: unaffected
cpe cpe:2.3:a:gleam-lang:gleam:*:*:*:*:*:*:*:*
version type git
modules · source files · routines
modules gleam_cli::dependencies · gleam_core::manifest · gleam_core::paths · gleam_core::build::project_compiler · gleam_cli::fs
source files compiler-cli/src/dependencies.rs · compiler-core/src/manifest.rs · compiler-core/src/paths.rs · compiler-core/src/build/project_compiler.rs · compiler-cli/src/fs.rs
routines gleam_cli::dependencies::hex_manifest_package · gleam_core::manifest::ManifestPackage::application_name · gleam_core::paths::ProjectPaths::build_directory_for_package · gleam_core::build::project_compiler::ProjectCompiler::load_cache_or_compile_package · gleam_cli::fs::delete_directory

Configurations

Affects projects whose dependency tree, directly or transitively, contains a hex.pm package release whose OTP application name is a path, when the project is built with gleam build, gleam check, gleam run, gleam test or gleam export. gleam deps download alone doesn't reach the delete. Gleam only uses hex.pm, which rejects such application names when a release is published since 2026-10-12, so only releases published before then can have one.

Solutions

Upgrade Gleam to 1.19.2 or later. Gleam then rejects a hex.pm release whose OTP application name, or the application name it gives a dependency, isn't a lowercase letter followed by lowercase letters, digits and _.

References

Credits

Peter Ullrich Finder
Louis Pilfold Remediation developer
Eric Meadows-Jönsson Coordinator
Jonatan Männchen / EEF Analyst

CVSS breakdown

CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:A/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N
« All CVEs