Skip to content

Unauthenticated users can read from tables #1627

Closed
@Adhil523

Description

@Adhil523

Description and expected behavior

Given the below schema, running db.gymUser.findMany() without authentication in the repl returns all the user data.

model Gym extends Base {
  // ...
  members            GymUser[]

  @@allow('all', auth().admin.role == "ADMIN")
  @@allow('create,read', auth() != null)
  @@allow('update', members?[user == auth() && role == "ADMIN"])
}

model GymUser extends Base {
  userID                 String
  user                   User             @relation(fields: [userID], references: [id])
  gymID                  String?
  gym                    Gym?             @relation(fields: [gymID], references: [id])
  role                   Role

  @@allow('create', auth() != null)
  @@allow('read',gym.members?[user == auth() && (role == "ADMIN" || role == "TRAINER")])
  @@allow('read,update', user == auth())
  @@allow('update', gym.members?[user == auth() && role == "ADMIN"])
  @@deny('update', future().userID != userID)

  @@unique([userID, gymID])

}

Prisma Queries

Query as anonymous user
{
  "where": {
    "OR": [
      {
        "OR": [
          {
            "gym": {
              "members": {
                "some": {
                  "AND": [
                    {
                      "OR": []
                    },
                    {
                      "OR": [
                        {
                          "role": {
                            "equals": "ADMIN"
                          }
                        },
                        {
                          "role": {
                            "equals": "TRAINER"
                          }
                        }
                      ]
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    ]
  }
}
Query as an authenticated user
{
  "where": {
    "OR": [
      {
        "OR": [
          {
            "gym": {
              "members": {
                "some": {
                  "AND": [
                    {
                      "user": {
                        "is": {
                          "id": "clymmldqa0000cd0svaueud8d"
                        }
                      }
                    },
                    {
                      "OR": [
                        {
                          "role": {
                            "equals": "ADMIN"
                          }
                        },
                        {
                          "role": {
                            "equals": "TRAINER"
                          }
                        }
                      ]
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    ]
  }
}

It seems like this part is the problem when auth() is null in this @@allow('read',gym.members?[user == auth() && (role == "ADMIN" || role == "TRAINER")])

           "gym": {
              "members": {
                "some": {
                  "AND": [
                    {
                      "OR": []
                    },

Expected behavior is that user==auth() part will fail, as the query was based on the example provided in the docs.

Environment (please complete the following information):

  • ZenStack version: 2.0.1
  • Prisma version: 5.12.1

Additional context
Might be related to #397

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions