Skip to content

SQL contract builder writes to global objects when a namespace or table is named __proto__, constructor or toString #31141

Closed

Description

@wmadden-electric

What happens

The SQL contract builder groups tables, models, enums and value sets by namespace in plain objects, and reads a group with ?? {}. A name that every JavaScript object inherits is read from the prototype, so the builder writes to a global object and the contract comes out wrong.

packages/2-sql-family/2-authoring/contract-ts/src/build-contract.ts:

const tablesByNamespace: Record<string, Record<string, StorageTableInput>> = {};
for (const table of tables) {
  const namespaceTables = tablesByNamespace[table.namespaceId] ?? {};
  namespaceTables[table.tableName] = lowerTable(table, tableLoweringContext);
  tablesByNamespace[table.namespaceId] = namespaceTables;
}

With a table users in a namespace of each name:

Namespace name Result
__proto__ The lookup returns Object.prototype. The table is written onto it, so every object in the process gains a users property, and the table is missing from the contract.
constructor The lookup returns the global Object function, and the table is written onto it.
toString The lookup returns the built-in toString function, and the table is written onto it.

The same lines, runnable on their own:

const tablesByNamespace = {};
const namespaceTables = tablesByNamespace['__proto__'] ?? {};
namespaceTables['users'] = { columns: {} };
tablesByNamespace['__proto__'] = namespaceTables;

Object.keys(tablesByNamespace); // []
({}).users;                     // { columns: {} }

A target cannot guard against this. The hook a target has for column types runs once per column, so a model with no columns never reaches it, and the grouping above runs either way.

Reproduced on 8.0.0-rc.23 and on main.

Two defects

__proto__ cannot be stored as a key of a plain object. Assigning it sets the object's prototype and stores nothing. This holds one level down too: a table or model named __proto__ vanishes from its group. No fix inside the builder makes the name safe in later stages, which copy, merge and rebuild these objects.

Every other inherited name (constructor, toString, valueOf, hasOwnProperty and the rest) is a normal key once stored. The defect is the read before anything is stored: it returns the inherited value, so the ?? {} fallback never runs.

Proposed fix

  1. Refuse __proto__ wherever a name becomes a key in the SQL builder: namespace, table, column, model, field, enum, value set. The Mongo family already refuses a field or relation stored as __proto__ (packages/2-mongo-family/1-foundation/mongo-contract/src/stored-names.ts), and the Postgres contract printer refuses it for the names it writes.
  2. Read every grouping keyed by such a name with Object.hasOwn, so an inherited value is never taken for a stored one. This covers the groupings by namespace in build-contract.ts (tables, models, enums, value sets) and the objects keyed by table and model name inside them.

constructor, toString and the like stay allowed. They are plausible table and column names in a real database, and they are reliable keys once the read is correct.

Tests

  • constructor and toString as a namespace name and as a table name: the table is in the emitted contract, the contract loads back from contract.json, and no global object changed.
  • __proto__ as a namespace, table, column, model, field and enum name: refused, and no global object changed.

Not in this issue

The same ?? {} read on a name-keyed object appears elsewhere in the SQL family, the Mongo family and the framework. Those need their own sweep.

Activity

  1. wmadden-electric commented on Nov 20, 2026

    @wmadden-electric
    ContributorAuthor

    The Prisma team is working on this fix. Please do not open a pull request for it.

  2. added a commit that references this issue on Nov 20, 2026
    5bfd99a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions