Refuse API token creation from restricted actors

This commit is contained in:
Alex Garcia 2026-09-09 08:38:40 -07:00 committed by GitHub
commit c6ba7b3298
2 changed files with 32 additions and 0 deletions

View file

@ -797,6 +797,8 @@ class CreateTokenView(BaseView):
raise Forbidden(
"Token authentication cannot be used to create additional tokens"
)
if "_r" in request.actor:
raise Forbidden("Restricted actors cannot create API tokens")
async def shared(self, request):
self.check_permission(request)

View file

@ -1,4 +1,5 @@
import time
from unittest.mock import AsyncMock
import pytest
from bs4 import BeautifulSoup as Soup
@ -237,6 +238,35 @@ def test_auth_create_token(
assert response3.json["actor"]["id"] == "test"
@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["GET", "POST"])
@pytest.mark.parametrize(
"restrictions",
[
{},
{"a": ["vi"]},
{"d": {"db": ["vd"]}},
{"r": {"db": {"t1": ["vt"]}}},
],
ids=["empty", "instance", "database", "table"],
)
async def test_auth_create_token_not_allowed_for_restricted_actors(
bare_ds, monkeypatch, method, restrictions
):
create_token = AsyncMock()
monkeypatch.setattr(bare_ds, "create_token", create_token)
response = await bare_ds.client.request(
method,
"/-/create-token",
actor={"id": "test", "_r": restrictions},
)
assert response.status_code == 403
assert "Restricted actors cannot create API tokens" in response.text
create_token.assert_not_called()
@pytest.mark.asyncio
async def test_auth_create_token_not_allowed_for_tokens(ds_client):
ds_tok = ds_client.ds.sign(