Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributes in array and object format get stringified #1115

Open
HerrSpeck opened this issue Mar 8, 2025 · 0 comments
Open

Attributes in array and object format get stringified #1115

HerrSpeck opened this issue Mar 8, 2025 · 0 comments

Comments

@HerrSpeck
Copy link

HerrSpeck commented Mar 8, 2025

Hi!

I've been using this library for a while now and just ran into an issue where my rules are not working in my application, but the Cerbos tests are working fine.
The Principal and Resource have exactly the same values in both the tests and my real world situation, but for some reason when I pass my principal to the isAllowed-function of the Cerbos client, attributes that are not simple types (like numbers, booleans or strings) appear stringified in the decision audit logs.
I'm pretty sure the real-world case fails, because the rules cannot be correctly evaluated based on the stringified values, instead of the expected Objects/Arrays.

For Example:
I'm requesting a resource, that requires my user to have certain claims/attributes. The rule might look something like this:

- actions: [update]
  effect: EFFECT_ALLOW
  roles: [USER, ADMIN]
  condition:
    match:
      expr: "CLAIM_ADMINISTRATION" in P.attr.claims

As I said the Cerbos tests all pass. Principal for my Cerbos tests looks like this:

user#2:
  id: user#2
  roles:
    - USER
  attr:
    {
      claims: [CLAIM_ADMINISTRATION, SOME_OTHER_CLAIM],
    }

Now, when I use it in my real world application, I have a helper function that transforms the requesting users Identity to the principal format:

export const identityToPrincipal = (identity: Identity): any => ({
  id: identity.id,
  roles: [identity.role],
  attr: {
    claims: identity.claims,
  },
});

I printed the identity that is returned from said function and I pass this value as-is to the isAllowed-function:

{
  id: '0612ecc1-8057-455e-821f-140a6c6b0842',
  roles: [ 'ADMIN' ],
  attr: {
    claims: [
      'CLAIM_ADMINISTRATION',
    ]
  }
}

As we can see the claims are an array of strings, as they should be.

But when I check the decision logs of Cerbos, I get the following:

{
  "resource": {
    "kind": "Permission",
    "id":"c5435a6c-4fbb-406b-896f-ce556515fd0c",
    "attr": {
      "beginDate": "2025-03-08T00:00:00.000Z",
      "endDate":"2025-03-14T00:00:00.000Z",
      "originalEntity": "{\"beginDate\":\"2025-03-08T00:00:00.000Z\",\"endDate\":\"2025-03-14T00:00:00.000Z\"}"
    }
  },
  "principal": {
    "id":"0612ecc1-8057-455e-821f-140a6c6b0842",
    "roles":["ADMIN"],
    "attr":{
      "claims": "[\"CLAIM_ADMINISTRATION\",\"SOME_OTHER_CLAIM\"]"
    }
}

We can see that the claims have been stringified and all "-characters have been escaped inside the array. It works for the roles, so it seems to be an issue with Object or Array type fields inside the attributes. I included the resource in the last payload as well, because the same issue exists for Object or Array type attributes in the Resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant