hasura / hasura/graphql-engine

feature request: Conditional Permissions Dependent on Session Variables

Open
#6,901 1 comment 2 reactions 1 assignee Claimed by @0x777 View on GitHub
a/authz c/server k/enhancement p/product/high
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

Let's assume I have a table defined as follows:

```sql
CREATE TABLE cars(id TEXT, make TEXT, model TEXT, color TEXT);
```

Where column `color` can take the values `red`, `green`, or `blue`.

And let's assume we have two users with the following session variables/auth claims:

```json
{
"X-Hasura-User-Id": "user1",
"X-Hasura-Role": "user",
"X-Hasura-Color": "red",
"X-Hasura-Allow-Select-All-Colors": true
}
```

```json
{
"X-Hasura-User-Id": "user2",
"X-Hasura-Role": "user",
"X-Hasura-Color": "red",
"X-Hasura-Allow-Select-All-Colors": false
}
```

I want to allow `user1` to retrieve all rows from table `cars` regardless of color, but I want to ensure that `user2` can only select cars whose color is `red`.

Currently, there is no way for me to do this without creating an entirely distinct role for `user1`.

Ideally, I could create a custom permissions check for role `user` like so:

```json
{
"_or": [
{
"X-Hasura-Allow-Select-All-Colors": {
"_eq": true
},
},
{
"color": {
"_eq": "X-Hasura-Color"
}
}
]
}
```

I imagine this exact syntax may not work, as it would not be compatible with tables that have one or more columns whose names are prefixed by `X-Hasura-`.

Perhaps instead, there could be some `_`-prefixed operator that can extract a value from session variables, similar to how the `_exists` operator works. For example:

```json
{
"_or": [
{
"_sesh": {
"_name": "X-Hasura-Allow-Select-All-Colors",
"_eq": true
}
},
{
"color": {
"_eq": "X-Hasura-Color"
}
}
]
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.