Prevent API key from being returned by API #4400
Replies: 4 comments 8 replies
-
Hey @javierlinked — this makes sense. What we could do is add a flag by default that hides the API key, meaning it is only ever exposed on creation - and after that point, there would be no way to retrieve it besides creating a custom script to retrieve it, decrypt it, and then provide it manually. I will move this to the roadmap and mark it as Priority 1. |
Beta Was this translation helpful? Give feedback.
-
I've experienced this now, too. I'm working on a SaaS app using Payload as the application backend. The Users collection is a straightforward way to store profile information. Other users should be able to read the document when they are a Member of the same organization to access profile information (and other application related details associated with the user). Payload has all the mechanisms to deal with this. The problem arises when fields are implicitly added to the Collection. They are inaccessible for custom field level access control and validation rules. The workarounds feel like an antipattern, and changing the implicit behavior would not address this underlying problem. I think it would make sense to allow explicitly setting these fields in the Collection configuration by adding "auth-*"-Field Types: e.g. |
Beta Was this translation helpful? Give feedback.
-
Any movement on this? Very very impactful behaviour to have access control built in, but no built-in way to not expose what are essentially passwords to all users. |
Beta Was this translation helpful? Give feedback.
-
For anyone reading this, not all the field options are supported AFAICT, but it is currently possible to specify access control rules for the the api key like this: const collectionConfig = {
{
name: "apiKey",
type: "text",
access: {
read: () => {...}),
create: () => {...},
update: () => {...},
},
},
} |
Beta Was this translation helpful? Give feedback.
-
Link to reproduction
No response
Describe the Bug
When using API keys with Payload, the actual API key value is returned in the response from the API. This is a security risk, as it allows anyone with access to the response to use the API key to make unauthorized requests, gaining other user account rights.
There are currently no documented ways to prevent the API key from being returned by the API. However, there are a couple of workarounds that can be used:
Create a custom access rule that prevents the apiKey field from being read.
Use an afterRead hook to overwrite the apiKey field with a placeholder value.
However, neither of these workarounds are ideal.
Possible solution:
Implement a way to prevent the API key from being returned by the API by default. This could be done by adding a new option to the auth configuration object that allows users to specify whether or not the API key should be returned. For example:
This would allow users to choose whether or not they want to expose the API key in the response from the API.
To Reproduce
Create users different users, enable API key. If logged in has access to READ access of user collection, it will also have access to each created user' api key.
Payload Version
2.x
Adapters and Plugins
db-mongodb, bundler-webpack
This issue is discussed on discord here.
Beta Was this translation helpful? Give feedback.
All reactions