supabase: Cannot Mint Custom JWT in Python

Bug report

  • [ x] I confirm this is a bug with Supabase, not with my own application.
  • [ x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Supabase does not accept any modifications to the tokens

To Reproduce

See these snippets, using your own configuration:

import jwt # pyjwt library
from supabase import create client

key =  # insert service key from supabase UI
secrete = # insert jwt secret from supabase UI
url = # insert url

This key works just fine with the client.

# works!
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()

And we can inspect the claims using pyjwt:

claims = jwt.decode(key,secret, algorithms=["HS256"]) 
{'iss': 'supabase',
'ref':  '<redacted>',
'role': 'service_role',
'iat': 1702682963,
'exp': 2018258963}

Pyjwt can take these exact same claims to create an identical key, which validates that it is working just fine.

my_key =  jwt.encode(claims, secret, algorithm="HS256")
assert key == my_key

# works just fine
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()

But even the most minor change to the claims invalidates this process. For example, if we round off the exp a bit (which should not impact auth at all), the key no longer works.

claims = {'iss': 'supabase',
 'ref': '<redacted>',
 'role': 'service_role',
 'iat': 1702682963,
 'exp': 2018258900} # note the change to the 'exp'
}

my_key =  jwt.encode(claims, secret, algorithm="HS256")

# Does NOT work. 
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()
# APIError: {'message': 'Invalid API key', 'hint': 'Double check your Supabase `anon` or `service_role` API key.'}

Other small changes (I tried several), also break the key. In this case, I just added a ‘test’ claim.

# now a small change
claims = {'iss': 'supabase',
 'ref': '<redacted>',
 'role': 'service_role',
 'iat': 1702682963,
 'exp': 2018258963},
'test': 1. # new key
}

my_key =  jwt.encode(claims, secret, algorithm="HS256")

# Does NOT work. 
supabase.create_client(my_key).table("mytable").select("*").limit(5).execute()
# APIError: {'message': 'Invalid API key', 'hint': 'Double check your Supabase `anon` or `service_role` API key.'}

This problem arises with really any change made to the key’s claims. So it is not possible to create custom JWTs.

Expected behavior

Expected behavior, consistent with documentation, custom claims can be added to keys and still work on the API.

System information

  • OS: macOS
  • Version of supabase-js: [e.g. 2.4.5]

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

If this is the case, then I’d swap “perfectly fine” with “unneeded” and use the anon key. I’d say best practice is to only use the service role key when needed.

I checked. Turns out that even if you use the service_role API-key, it will NOT bypass RLS, and instead use just the header key for RLS with role=“authenticated” or “anon”, ignoring service key. So that means it’s perfectly fine to use the service_role API-key server-side in this way.

Yeah, this is expected; as the role is important on the backend. As you’ve discovered, you can create your own service role JWTs.

Remarkably, you can also use the anon-key as the API-key, but if the header-key has role=“service_role”, then even with the anon key, you will bypass RLS. You don’t actually need the service_role API-key to bypass RLS.

Agreed.

Of course, you wouldn’t want to reveal your service key publicly. Instead, for example, in the browser you’d pass along the anon-key. The JWT secret is the key that unlocks everything, it seems.

Looks like you’ve got a solution! One thing to keep your eye on is this PR. I forgot about it, until someone randomly approved it this morning. I’m not sure if/when it’ll drop.

Hi @swamidass - Just checking in here, did you get this all working?

Awesome!

I don’t believe you’d want to use the service role key in these situations, as it bypasses RLS - only use the anon key.

Okay, it seems that I now have some working code. In python (or nodejs), first make the key.

my_key =  jwt.encode(claims, secret, algorithm="HS256")

Then make a supabase client that has this key in the auth header, but uses either the anon or service_role key,

supabase = create_client(
    url,
    service_key,
    ClientOptions(headers={"Authorization": f"Bearer {my_key}"}),
)

Now postgres see the claims just fine. These claims can be really anything you like. Moreover,

  1. Supabase will respect the “role” claim, which you can set to authenticated, service_role, anon, or anything else.
  2. Supabase will respect the “exp” claim, which must be in the future and you can either leave out for a non-expiring key.
  3. Supabase will respect the “iat” claim, which must be in the past, or you can leave it out.

This works painlessly. I just wish it was documented somewhere!!!

I’m not familiar with the Python implementation of Supabase. It looks like create_client has a headers option though, so you’d set the Authorization header to Bearer <jwt-value>. This is enough to make Supabase calls as the user. However, things like get_session(), get_user(), and refresh_session() will not work because the Supabase client is not aware of any session - that is, if the Python implementation works similarly to the Javascript one.

This initializing page has a “With timeout option” tab, in case you’re not familiar with how to attach the headers option to the client.