fix(linear): reject stale member access updates after reauthorization
This commit is contained in:
1 parent
1535cb1b6b
commit
ae1eb02cd8
3 files changed
+88
-2
No files matched your search
@@ -233,7 +233,10 @@ class LinearConnectStore:
|
||||
if not any(member["id"] == user_id for member in members):
|
||||
raise ChannelConnectError("Member is not active in an accessible Linear team", status=404)
|
||||
try:
|
||||
state.set_member_access(config.client_id, organization_id, user_id, allowed=allowed)
|
||||
state.set_member_access(
|
||||
config.client_id, organization_id, user_id, allowed=allowed,
|
||||
expected_authorized_at=installation.authorized_at,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise ChannelConnectError(str(exc), status=409) from exc
|
||||
return {
|
||||
|
||||
@@ -263,15 +263,24 @@ class LinearStateStore:
|
||||
|
||||
def set_member_access(
|
||||
self, client_id: str, organization_id: str, user_id: str, *, allowed: bool,
|
||||
expected_authorized_at: float | None = None,
|
||||
) -> None:
|
||||
"""Save a choice, rejecting network results from an older authorization."""
|
||||
with self._guard, self._connect() as connection:
|
||||
connection.execute("BEGIN IMMEDIATE")
|
||||
installed = connection.execute(
|
||||
"SELECT 1 FROM installations WHERE oauth_client_id = ? AND organization_id = ?",
|
||||
"SELECT authorized_at FROM installations "
|
||||
"WHERE oauth_client_id = ? AND organization_id = ?",
|
||||
(client_id, organization_id),
|
||||
).fetchone()
|
||||
if installed is None:
|
||||
raise ValueError("Linear workspace is not connected")
|
||||
if (expected_authorized_at is not None
|
||||
and float(installed["authorized_at"]) != expected_authorized_at):
|
||||
raise ValueError(
|
||||
"Linear workspace authorization changed while updating member access. "
|
||||
"Refresh and try again."
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO member_access (oauth_client_id, organization_id, user_id, allowed) "
|
||||
"VALUES (?, ?, ?, ?) ON CONFLICT(oauth_client_id, organization_id, user_id) "
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
from dataclasses import replace
|
||||
from datetime import datetime, timezone
|
||||
@@ -17,6 +18,79 @@ from nanobot.channels.linear.state import LinearStateStore
|
||||
from nanobot.channels.linear.tests.test_linear import _config, _installation, _runtime
|
||||
|
||||
|
||||
@pytest.mark.parametrize("allowed", [False, True])
|
||||
@pytest.mark.parametrize("replacement", [
|
||||
"removed", "reauthorized", "same_tokens", "reconnected", "other_app", "refreshed",
|
||||
])
|
||||
async def test_delayed_member_choice_is_scoped_to_its_authorization(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, replacement: str, allowed: bool,
|
||||
) -> None:
|
||||
state = LinearStateStore(tmp_path / "linear.sqlite3")
|
||||
monkeypatch.setattr(time, "time", lambda: 1000.0)
|
||||
state.save_installation(_installation())
|
||||
original = state.installation("org-1")
|
||||
assert original is not None
|
||||
entered, release = asyncio.Event(), asyncio.Event()
|
||||
|
||||
async def handler(request: httpx.Request) -> httpx.Response:
|
||||
if "NanobotMemberTeams" in json.loads(request.content)["query"]:
|
||||
return httpx.Response(200, json={"data": {"teams": {
|
||||
"nodes": [{"id": "team-1", "name": "Team"}],
|
||||
"pageInfo": {"hasNextPage": False, "endCursor": None},
|
||||
}}})
|
||||
entered.set()
|
||||
await release.wait()
|
||||
return httpx.Response(200, json={"data": {"team": {"members": {
|
||||
"nodes": [{"id": "user-1", "name": "Member", "active": True, "app": False}],
|
||||
"pageInfo": {"hasNextPage": False, "endCursor": None},
|
||||
}}}})
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http:
|
||||
monkeypatch.setattr(linear_connect, "_load_linear_config", _config)
|
||||
monkeypatch.setattr(linear_connect, "LinearStateStore", lambda: LinearStateStore(state.path))
|
||||
monkeypatch.setattr(linear_connect, "LinearClient", lambda config, store: LinearClient(config, store, http))
|
||||
pending = asyncio.create_task(linear_connect.LinearConnectStore().handle("start", {
|
||||
"operation": ["member_access"], "organization_id": ["org-1"],
|
||||
"user_id": ["user-1"], "allowed": [str(allowed).lower()],
|
||||
}))
|
||||
try:
|
||||
await asyncio.wait_for(entered.wait(), 2)
|
||||
monkeypatch.setattr(time, "time", lambda: 2000.0)
|
||||
if replacement in {"removed", "reconnected"}:
|
||||
assert state.delete_installation("org-1")
|
||||
if replacement == "refreshed":
|
||||
assert state.refresh_installation(original, replace(
|
||||
original, access_token="rotated", refresh_token="rotated-refresh",
|
||||
))
|
||||
elif replacement != "removed":
|
||||
state.save_installation(replace(
|
||||
original,
|
||||
oauth_client_id="other-client" if replacement == "other_app" else "client-id",
|
||||
access_token=original.access_token if replacement == "same_tokens" else "new-grant",
|
||||
refresh_token=original.refresh_token if replacement == "same_tokens" else "new-refresh",
|
||||
), reauthorize=True)
|
||||
expected = state.installation("org-1")
|
||||
if expected is not None:
|
||||
assert (expected.authorized_at == original.authorized_at) is (replacement == "refreshed")
|
||||
state.set_member_access(expected.oauth_client_id, "org-1", "user-1", allowed=not allowed)
|
||||
release.set()
|
||||
if replacement == "refreshed":
|
||||
assert (await pending)["status"] == "member_access_saved"
|
||||
assert state.member_access("client-id", "org-1", "user-1") is allowed
|
||||
else:
|
||||
with pytest.raises(linear_connect.ChannelConnectError) as error:
|
||||
await pending
|
||||
assert error.value.status == 409
|
||||
if expected is not None:
|
||||
assert state.member_access(expected.oauth_client_id, "org-1", "user-1") is (not allowed)
|
||||
else:
|
||||
assert state.member_access("client-id", "org-1", "user-1") is None
|
||||
assert state.installation("org-1") == expected
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.gather(pending, return_exceptions=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("replacement", ["removed", "reauthorized", "same_tokens", "other_app"])
|
||||
async def test_member_read_cannot_restore_or_overwrite_a_changed_installation(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, replacement: str,
|
||||
|
||||
Reference in new issue
Block a user