Skip to main content
Menu
Advisories

Path traversal via native enum and routine names in generated file paths

GHSA-MFXV-V6Q8-GFRXPublished Updated GitHub

Description

@mikro-orm/entity-generator derives output filenames from database-introspected native enum type names and routine names without the escaping applied to entity class names. A name containing ../ escapes the configured output directory, so running mikro-orm generate-entities against a schema an untrusted party can write to can create files outside the target path and overwrite existing .ts files.

Details

Entity files are safe: SourceFile.getBaseName() uses meta.className, produced by AbstractNamingStrategy.getEntityName(), which codepoint-escapes every non-identifier character (/ becomes $47).

The two other source file types skip that step:

  • NativeEnumSourceFile.getBaseName() passes this.nativeEnum.name (raw pg_type.typname) straight to the fileName option, which defaults to identity.
  • RoutineSourceFile.getBaseName() passes the routine name through toPascalCase(), which only splits on [_\s-] and leaves / and . intact.

EntityGenerator then joins the result onto the configured base directory, so ../ segments traverse upward.

Preconditions

  1. An untrusted party holds CREATE on a schema the generator scans. Verified: a role with only GRANT CREATE ON SCHEMA public, no table ownership and no superuser, can create both a hostile type and a hostile function. On PostgreSQL 15 and later public no longer grants CREATE to PUBLIC by default, so this requires an explicit grant rather than being available to any authenticated role.
  2. A developer runs the generate-entities CLI against that schema.

There is no runtime or application-user exposure.

PoC

CREATE TYPE "../../../PWNED_ENUM" AS ENUM ('a','b');
CREATE FUNCTION "../../../PWNED_ROUTINE"() RETURNS int AS $$ SELECT 1 $$ LANGUAGE sql;

With the generator configured to write to out/nested/deep/output, the enum file lands in out/, three levels above the configured directory, while the legitimate entity is written in place. A pre-existing file is overwritten if the crafted name resolves onto it.

Impact

CWE-22.

Within the affected range this composes with GHSA-vr2q-6mwx-8248, the string-literal escaping flaw in the same package, because both defects are reachable through a single database object. The enum name determines the output path and the enum labels determine the file contents:

CREATE TYPE "../../src/bootstrap" AS ENUM ('x\''+payload//');

On <= 7.1.8 neither is escaped, so one statement writes attacker-controlled, executable TypeScript over a path of the attacker's choosing inside the project, including a file the application already imports. The developer does not have to open, review, or import the generated output for the payload to run, which is what distinguishes this from the escaping flaw on its own, where the payload lands in a new file under the generated-entities directory that a reviewer is at least looking at.

Considered alone, with the escaping flaw fixed, the impact is bounded to file corruption. The attacker does not control content in that case: identifiers are codepoint-escaped, enum values and routine bodies are emitted as quoted string literals, and the extension is forced to .ts, so an existing source file can be destroyed by being replaced with generator-shaped output but arbitrary code cannot be implanted, and non-.ts files such as .npmrc or shell profiles cannot be written. In the routine case the emitted file is not even valid TypeScript, since the class name retains the slashes.

The dev-time-only entry point and the requirement that an untrusted party already hold schema-level CREATE on the introspected database keep the overall severity low.

Fix

Enum and routine names are normalized before they are used as a file name, so database metadata cannot contain a path separator. For routines this also keeps slashes out of the emitted export const name, which was invalid TypeScript.

Generated files are additionally restricted to the project folder, or to the configured path when that points outside the project, so no file name can reach an arbitrary filesystem location. A custom fileName returning a nested or sibling directory continues to work.

Affected versions

References

GitHub · mikro-orm/mikro-orm