payload icon indicating copy to clipboard operation
payload copied to clipboard

Collections with Draft Versions cannot be Access filtered when they have Hidden Fields

Open mckinley opened this issue 2 years ago • 4 comments

Link to reproduction

https://github.com/mckinley/payload/commit/9c189e4fb1e14e71adc75f61596bf928d91592f3

Describe the Bug

Hi there! Thanks for Payload! Seems like a great project!

I think I've found a problem. I'm happy to work through it and contribute a PR if folks don't mind taking a look and agree that it's an issue. This is for the latest stable release, 2.18.3.

When a collection has versions: { drafts: true } set, and there is an access control also set like:

access: {
    read: () => {
      return {
        hiddenField: {
          equals: "value",
        },
      };
    },
  },

the admin panel no longer works when listing the relevant resources.

The error thrown is something like: [16:15:37] ERROR (payload): QueryError: The following path cannot be queried: version.hiddenField

There is no error message that shows in the UI. Screenshot 2024-05-26 at 8 22 41 PM

The admin panel uses a URL similar to "http://localhost:3000/api/posts?depth=0&draft=true&limit=10&invoke=a3293979-b657-429c-a55a-860ad750d0c1". As long as `draft=true' is present in a request in this situation, it fails.

The error is identified by payload at this line: https://github.com/payloadcms/payload/blob/4c832ad98440e5f56a490e5f884a218cbfd2056d/packages/payload/src/database/getLocalizedPaths.ts#L78 And relates to logic here: packages/payload/src/database/queryValidation

I found the bug while combining payload tenancy with draft versioning. Payload tenancy uses a hidden tenant field to filter access to collections.

I've walked through the code and done a fair amount of debugging. I am happy to add more details about why this is happening if desired. I also created a demonstration repo that has two commits. The first generates a new payload app from the command line. The second adds the setup to demonstrate the issue. I also have a test written in a forked payload repo that demonstrates the issue.

Hopefully I've followed the reproduction guide well and everything is clear.

To Reproduce

First of all, review this collection configuration. Determine if this makes sense as a common use case that Payload would like to support.

Then there are a few ways to reproduce.

Using Payload Reproduction Guide Strategy

git clone [email protected]:mckinley/payload.git
cd payload
pnpm install
git switch version-test
node 'node_modules/jest/bin/jest.js' 'test/_community/int.spec.ts' -t '_Community Tests allows Collections to be Access filtered when Draft Versions are enabled'
  1. Notice failing test.

Using Basic Demo

git clone [email protected]:mckinley/versions-test.git
cd versions-test
npm install
npm run dev
  1. Go to http://localhost:3000
  2. Create your user etc.
  3. Go to http://localhost:3000/admin
  4. Click on "Resources"
  5. Notice that the page is broken

DIY

  1. Add the following to any collection definition:
  fields: [
    {
      name: 'hiddenField',
      type: 'text',
      hidden: true,
    },
  ],
  access: {
    read: () => {
      return {
        hiddenField: {
          equals: 'value',
        },
      }
    },
  },
  versions: {
    drafts: true,
  },
  1. Go to that admin dashboard and click on the resource to get a list.
  2. Observe that the page is broken.

Payload Version

2.18.3

Adapters and Plugins

No response

mckinley avatar May 27 '24 03:05 mckinley

Hey @mckinley — this is an interesting find. We do indeed restrict querying on fields unless you have public access to the field because this prevents security vulnerabilities. But I guess that if field queries are passed via access control, querying on those fields does not present a security vuln because your backend code is what's responsible for filtering on them and you define them.

It's possible that we could resolve this. @kendelljoseph let's look into this!

jmikrut avatar Jun 10 '24 13:06 jmikrut

@mckinley we've dug into this a bit and here is what we're thinking.

We don't want to introduce any security risks by adding a flag that would allow for a field that is hidden to behave differently via the API and the local API. Any flag that partially change access behavior is too subtle and could have catastrophic behaviors.

Options we think could be used as an alternative to this approach:

  • use an afterRead hook.
  • the doc is available in the read access function where data can be passed and used by the to determine access conditions.

kendelljoseph avatar Jul 23 '24 18:07 kendelljoseph

Thanks so much @kendelljoseph. Ok I had put my payload project on hold for a bit, but when I jump back in Ill continue to see if there is anything I can do to work around the problem.

Thanks again!

mckinley avatar Jul 25 '24 16:07 mckinley

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Sep 06 '24 23:09 github-actions[bot]