mirror of
https://github.com/simonw/datasette.git
synced 2026-07-09 09:04:42 +02:00
Return 401 for invalid or expired bearer tokens
Invalid dstok_ tokens - bad signature, malformed payload, expired, or presented while allow_signed_tokens is off - previously degraded the request to anonymous, so clients saw a 403 permission error or worse, a 200 with anonymous-visible data. Token handlers can now raise TokenInvalid for tokens they recognize but reject; Datasette responds with 401, the canonical JSON error body and a WWW-Authenticate: Bearer error="invalid_token" header, even when a valid cookie is also present. Bearer tokens no registered handler recognizes are still ignored, so authentication plugins with their own token formats keep working. TokenInvalid is exported from the datasette package for use by plugin token handlers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GrHZSypDfMnym1tM5XJAFZ
This commit is contained in:
parent
b2cdc81d34
commit
aaaffe45b8
11 changed files with 214 additions and 45 deletions
|
|
@ -202,8 +202,8 @@ async def test_insert_rows(ds_write, return_rows):
|
|||
"/data/docs/-/insert",
|
||||
{"rows": [{"title": "Test"} for i in range(10)]},
|
||||
"bad_token",
|
||||
403,
|
||||
["Permission denied"],
|
||||
401,
|
||||
["Invalid token signature"],
|
||||
),
|
||||
(
|
||||
"/data/docs/-/insert",
|
||||
|
|
@ -410,12 +410,13 @@ async def test_insert_or_upsert_row_errors(
|
|||
},
|
||||
)
|
||||
|
||||
actor_response = (
|
||||
await ds_write.client.get("/-/actor.json", headers=kwargs["headers"])
|
||||
).json()
|
||||
assert set((actor_response["actor"] or {}).get("_r", {}).get("a") or []) == set(
|
||||
token_permissions
|
||||
)
|
||||
if special_case != "bad_token":
|
||||
actor_response = (
|
||||
await ds_write.client.get("/-/actor.json", headers=kwargs["headers"])
|
||||
).json()
|
||||
assert set((actor_response["actor"] or {}).get("_r", {}).get("a") or []) == set(
|
||||
token_permissions
|
||||
)
|
||||
|
||||
if special_case == "invalid_json":
|
||||
del kwargs["json"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue