Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(auth): expire persisted push claims

Maxvint917792806673
diff --git a/core/web_push_notifications.py b/core/web_push_notifications.py
index bc33de2c6..c9db7d675 100644
--- a/core/web_push_notifications.py
+++ b/core/web_push_notifications.py
@@ -119,6 +119,7 @@ def web_push_authorization_context_record(
or context is None
or not context.is_remote
or not context.subject
+ or context.claims_issued_at is None
or user_key != f"remote:{context.subject}"
):
return None
@@ -128,6 +129,7 @@ def web_push_authorization_context_record(
"vibe_instance_role": context.instance_role,
"vibe_instance_access_source": context.instance_access_source,
"vibe_group_ids": sorted(context.group_ids),
+ "claims_issued_at": context.claims_issued_at,
}
optional_claims = {
"email": context.email,
@@ -142,6 +144,8 @@ def web_push_authorization_context_record(
def _metadata_authorization_contexts(metadata: dict[str, Any]) -> dict[str, AuthorizationContext]:
+ from vibe import remote_access
+
raw_contexts = metadata.get(WEB_PUSH_AUTHORIZATION_CONTEXTS_METADATA)
if not isinstance(raw_contexts, list):
return {}
@@ -152,6 +156,8 @@ def _metadata_authorization_contexts(metadata: dict[str, Any]) -> dict[str, Auth
user_key = raw_context.get("user_key")
if not isinstance(user_key, str) or not user_key.startswith("remote:"):
continue
+ if remote_access.session_needs_authorization_refresh(raw_context):
+ continue
context = context_from_session_payload(raw_context)
if context.subject and user_key == f"remote:{context.subject}" and context.can_read_instance:
contexts[user_key] = context
@@ -245,6 +251,15 @@ def _filter_project_authorized_user_keys(
) -> list[str]:
"""Recheck delayed remote deliveries against the current Project policy."""
+ if not user_keys:
+ return user_keys
+
+ contexts = _metadata_authorization_contexts(metadata)
+ user_keys = [
+ user_key
+ for user_key in user_keys
+ if user_key == "local" or user_key in contexts
+ ]
if not session_id or not user_keys:
return user_keys
from storage import project_access_service
@@ -261,7 +276,6 @@ def _filter_project_authorized_user_keys(
):
return user_keys
- contexts = _metadata_authorization_contexts(metadata)
authorized: list[str] = []
for user_key in user_keys:
if user_key == "local":
diff --git a/tests/test_ui_project_access.py b/tests/test_ui_project_access.py
index 630b258e1..e64cfd582 100644
--- a/tests/test_ui_project_access.py
+++ b/tests/test_ui_project_access.py
@@ -504,6 +504,8 @@ def test_remote_message_persists_trusted_web_push_authorization_context(
persisted_metadata = json.loads(metadata_json)
assert persisted_metadata["_web_push_user_key"].startswith("remote:")
records = persisted_metadata["_web_push_authorization_contexts"]
+ claims_issued_at = records[0].pop("claims_issued_at")
+ assert isinstance(claims_issued_at, int) and claims_issued_at > 0
assert records == [
{
"email": "[email protected]",
diff --git a/tests/test_ui_session_stream.py b/tests/test_ui_session_stream.py
index 9f8ed545d..571f12bac 100644
--- a/tests/test_ui_session_stream.py
+++ b/tests/test_ui_session_stream.py
@@ -126,7 +126,7 @@ def test_route_fire_and_forgets_dispatch(isolated_state, tmp_path):
payload = response.get_json()
assert payload["author"] == "user"
assert payload["author_id"] == "remote:user-a"
- assert payload["metadata"]["_web_push_user_key"] == "remote:user-a"
+ assert "_web_push_user_key" not in payload["metadata"]
assert payload["text"] == "no stream"
# The turn was kicked off fire-and-forget with the session + text.
dispatch_mock.assert_awaited_once()
diff --git a/tests/test_web_push_notifications.py b/tests/test_web_push_notifications.py
index 339375ad1..ce3a8ec5a 100644
--- a/tests/test_web_push_notifications.py
+++ b/tests/test_web_push_notifications.py
@@ -6,9 +6,27 @@ from storage.db import create_sqlite_engine
from storage.importer import ensure_sqlite_state
from storage.models import agent_sessions
from storage.settings_service import upsert_scope
+from vibe import remote_access
from vibe.authorization import AuthorizationContext
+def _remote_authorization_record(user_key: str) -> dict:
+ subject = user_key.removeprefix("remote:")
+ record = web_push_notifications.web_push_authorization_context_record(
+ user_key,
+ AuthorizationContext(
+ instance_role="editor",
+ subject=subject,
+ email=f"{subject}@example.com",
+ instance_access_source="email",
+ claims_issued_at=int(web_push_notifications.time.time()),
+ is_remote=True,
+ ),
+ )
+ assert record is not None
+ return record
+
+
def test_maybe_notify_inbox_message_schedules_agent_result(monkeypatch):
calls = []
@@ -215,7 +233,12 @@ def test_send_to_enabled_subscriptions_waits_then_sends_to_owner_devices(monkeyp
author="user",
source="user",
author_id="remote:user-a",
- metadata={"_web_push_user_key": "remote:user-a"},
+ metadata={
+ "_web_push_user_key": "remote:user-a",
+ "_web_push_authorization_contexts": [
+ _remote_authorization_record("remote:user-a")
+ ],
+ },
message_type="user",
text="Please finish",
)
@@ -263,6 +286,35 @@ def test_send_to_enabled_subscriptions_waits_then_sends_to_owner_devices(monkeyp
"https://push.example.test/a",
]
+ with engine.begin() as conn:
+ expired_message = messages_service.append(
+ conn,
+ scope_id=scope_id,
+ session_id="ses_push",
+ platform="avibe",
+ author="agent",
+ source="agent",
+ message_type="result",
+ text="Done later",
+ )
+ issued_at = int(web_push_notifications.time.time())
+ monkeypatch.setattr(
+ web_push_notifications.time,
+ "time",
+ lambda: issued_at + remote_access.SESSION_AUTHORIZATION_REFRESH_SECONDS,
+ )
+ web_push_notifications._send_to_enabled_subscriptions(
+ {
+ "title": "Push",
+ "body": "Done later",
+ "session_id": "ses_push",
+ "message_id": expired_message["id"],
+ }
+ )
+ assert [send[0]["endpoint"] for send in sends] == [
+ "https://push.example.test/a",
+ ]
+
def test_send_to_enabled_subscriptions_rechecks_restricted_project_access(monkeypatch, tmp_path):
monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
@@ -274,6 +326,7 @@ def test_send_to_enabled_subscriptions_rechecks_restricted_project_access(monkey
subject="user-a",
instance_access_source="email",
+ claims_issued_at=int(web_push_notifications.time.time()),
is_remote=True,
)
authorization_record = web_push_notifications.web_push_authorization_context_record(
@@ -470,6 +523,7 @@ def test_send_to_enabled_subscriptions_sets_visible_badge_count(monkeypatch, tmp
subject="user-a",
instance_access_source="email",
+ claims_issued_at=int(web_push_notifications.time.time()),
is_remote=True,
)
authorization_record = web_push_notifications.web_push_authorization_context_record(
@@ -557,7 +611,7 @@ def test_send_to_enabled_subscriptions_sets_visible_badge_count(monkeypatch, tmp
assert [send[1]["badge_count"] for send in sends] == [2]
-def test_send_to_enabled_subscriptions_uses_legacy_session_owner(monkeypatch, tmp_path):
+def test_send_to_enabled_subscriptions_rejects_legacy_session_owner(monkeypatch, tmp_path):
monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
ensure_sqlite_state()
engine = create_sqlite_engine()
@@ -617,7 +671,7 @@ def test_send_to_enabled_subscriptions_uses_legacy_session_owner(monkeypatch, tm
}
)
- assert [send[0]["endpoint"] for send in sends] == ["https://push.example.test/remote:user-a"]
+ assert sends == []
def test_send_to_enabled_subscriptions_prefers_message_owner_over_legacy_session(monkeypatch, tmp_path):
@@ -651,7 +705,12 @@ def test_send_to_enabled_subscriptions_prefers_message_owner_over_legacy_session
author="user",
source="user",
author_id="remote:user-b",
- metadata={"_web_push_user_key": "remote:user-b"},
+ metadata={
+ "_web_push_user_key": "remote:user-b",
+ "_web_push_authorization_contexts": [
+ _remote_authorization_record("remote:user-b")
+ ],
+ },
message_type="user",
text="Please finish",
)
@@ -721,7 +780,13 @@ def test_send_to_enabled_subscriptions_sends_to_merged_prompt_owners(monkeypatch
source="user",
message_type="user",
text="u1\nu2",
- metadata={"_web_push_user_keys": ["remote:user-a", "remote:user-b"]},
+ metadata={
+ "_web_push_user_keys": ["remote:user-a", "remote:user-b"],
+ "_web_push_authorization_contexts": [
+ _remote_authorization_record("remote:user-a"),
+ _remote_authorization_record("remote:user-b"),
+ ],
+ },
)
message = messages_service.append(
conn,
@@ -1159,7 +1224,12 @@ def test_send_to_enabled_subscriptions_sends_terminal_error_with_owner(monkeypat
source="user",
message_type="user",
text="Run it",
- metadata={"_web_push_user_key": "remote:user-a"},
+ metadata={
+ "_web_push_user_key": "remote:user-a",
+ "_web_push_authorization_contexts": [
+ _remote_authorization_record("remote:user-a")
+ ],
+ },
)
message = messages_service.append(
conn,
diff --git a/vibe/authorization.py b/vibe/authorization.py
index c57dcc6b3..b4ccc3abb 100644
--- a/vibe/authorization.py
+++ b/vibe/authorization.py
@@ -43,6 +43,7 @@ class AuthorizationContext:
organization_role: str | None = None
group_ids: frozenset[str] = frozenset()
membership_version: str | None = None
+ claims_issued_at: int | None = None
is_remote: bool = False
is_trusted_local: bool = False
@@ -117,6 +118,16 @@ def _optional_string(value: Any, *, limit: int = 320) -> str | None:
return cleaned
+def _optional_positive_int(value: Any) -> int | None:
+ if isinstance(value, bool):
+ return None
+ try:
+ parsed = int(value)
+ except (TypeError, ValueError):
+ return None
+ return parsed if parsed > 0 else None
+
+
def context_from_session_payload(payload: Mapping[str, Any]) -> AuthorizationContext:
role = _optional_string(payload.get("vibe_instance_role"))
if role not in INSTANCE_ROLES:
@@ -144,6 +155,9 @@ def context_from_session_payload(payload: Mapping[str, Any]) -> AuthorizationCon
organization_role=organization_role,
group_ids=group_ids,
membership_version=_optional_string(payload.get("vibe_membership_version")),
+ claims_issued_at=_optional_positive_int(
+ payload.get("claims_issued_at", payload.get("iat"))
+ ),
is_remote=True,
)