Unescaped database content in generated entity files
Description
entity-generator embeds database-introspected strings (column and table comments, native enum labels, column defaults) into the generated TypeScript as string literals. The shared quote() helper in packages/entity-generator/src/SourceFile.ts escapes single quotes but never escapes backslashes, so a value containing \' terminates the generated literal early and the remainder becomes executable TypeScript.
Details
protected quote(val: string) {
const backtick = val.startsWith(`'`) || val.includes('\n');
return backtick ? `\`${val.replaceAll('`', '\\``')}\`` : `'${val.replaceAll(`'`, `\\'`)}'`;
}In the single-quote branch, a literal backslash sitting in front of a ' combines with the helper's own inserted backslash. Two backslashes are one escaped-backslash token, so the following ' closes the string early.
The helper is used for effectively all introspected string content, including column comments (SourceFile.ts:857), table comments (SourceFile.ts:631) and native enum labels (NativeEnumSourceFile.ts). On PostgreSQL these come back from pg_catalog.col_description(), pg_catalog.obj_description() and pg_enum.enumlabel as plain text with no quote-doubling, so the stored value reaches quote() byte for byte.
Two further defects in the same helper:
- The backtick branch does not escape
${...}. Any value containing a newline takes that branch, soline1\n${payload}reaches the same outcome without needing the backslash trick at all. - The backtick branch's replacement is simply wrong.
quote("'a\b")emits'a\`b``, which is a syntax error. Any introspected value containing a backtick has been breaking generation regardless of any security consideration.
Preconditions
This is a development-time code generator with no runtime path. Reaching it requires all of the following:
- An untrusted party holds DDL rights on a schema the generator scans.
CREATEon the schema is enough for the enum vector:getNativeEnumDefinitions()returns every enum type in the scanned schemas whether or not any column references it, andEntityGeneratoremits a source file for each. Setting a table or column comment additionally requires ownership of that object. - A developer or CI job runs
mikro-orm generate-entitiesagainst that schema. - The generated file is then loaded or compiled.
No application user reaches this. Ordinary DML privileges are not sufficient, and there is no SQL-injection path into a comment or an enum label. Verified: a role with CREATE on one schema and no grants at all on the target tables can plant a poisoned enum label while select on those tables returns permission denied.
On PostgreSQL 14 and older, public granted CREATE to PUBLIC by default, so on those versions an attacker who has already achieved SQL injection could plant the payload as the application role. That path requires a prior compromise of the application.
PoC
Set a crafted column comment on a table the generator will scan:
const comment = String.raw`x\'+(()=>require("fs").writeFileSync("/tmp/PC",""))())//`;
await pgClient.query(`COMMENT ON COLUMN ghsa_mikro_test.note IS $tag$${comment}$tag$`);npx mikro-orm generate-entities --save --path generated then produces a file that parses cleanly and runs the payload on import.
Impact
CWE-94. Code execution on the machine running the generator, limited to projects that run entity generation against a schema a third party can write DDL into: a shared staging environment, a multi-tenant database, or a client database during a consulting engagement.
Severity is bounded by the trust model. The generator's stated purpose is turning a database schema into executable source, so pointing it at a schema an untrusted party controls is largely outside what it defends against, in the same way that npm install does not defend against a hostile lockfile. There is no runtime exposure and no application-user exposure. The escaping bug is real and is fixed on that basis, rather than because the attack scenario is a common deployment.
Fix
Escape backslashes before quotes, and escape ${ in the template-literal branch, matching RoutineSourceFile.ts in the same package which already handles both correctly.