Skip to content

Polymorphic sub model enhancements ignoring #1710

Closed
@svetch

Description

@svetch

Description and expected behavior
example model:

model Profile {
  id          String   @id @default(cuid())
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  displayName String

  ...

  type        String

  @@delegate(type)
  @@allow('read', true)
}

enum Role {
  ADMIN
  USER
}

model User extends Profile {
  ...
  email    String    @unique @deny('read', true) OR @allow('read', false, true)
  password String    @omit
  role     Role      @default(USER) @deny('read', true)
  token    String?   @omit
}

model Organization extends Profile {...}

Now if I get first user without zenstack context, then its works correctly.

const DB = getEnhancedPrisma(undefined, {
    DATABASE_URL: ...
});

const result = await DB.user.findFirst({
    where: {
        id: 'USER_ID',
    },
});

// result = {
...
displayName: ...
type: ...
...
}

BUT for the profile queries the sub models access policies don't validate, and also includes the omitted fields of submodel.

const DB = getEnhancedPrisma(undefined, {
    DATABASE_URL: ...
});

const result = await DB.profile.findFirst({
    where: {
        id: 'USER_ID',
    },
});

// result = {
...
displayName: ...
password: ...
token: ...
...
};

And also you can update the sub model fields without any permission check.

const DB = getEnhancedPrisma({
    id: 'RANDOM_ID',
}, {
    DATABASE_URL: ...
});

const res = await DB.profile.update({
    where: {
        id: 'user_2frIS4TQmzPgsfL2qCaiee4oyL0',
    },
    data: {
        delegate_aux_user: {
            update: {
              locale: 'test-EN',
            },
        },
    },
});
// result = {
...
locale: "test-EN"
...
};

Possible solution
I figured out that the issue occurs because the model metadata does not include the delegate relational fields. As a result, all enhancements that should be applied to these fields are ignored, and permissions for both reading and writing are automatically granted.

To resolve this problem, the generator CLI plugin needs to be updated to ensure that the delegate relational fields are included in the model metadata, not just in the Prisma schema.

Environment (please complete the following information):

  • ZenStack version: 2.5.1
  • Prisma version: 5.19.1
  • Database type: Postgresql

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions