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.

13.5 KB
Raw Blame
from __future__ import annotations

import time
import threading
from urllib.parse import parse_qs, urlsplit

import jwt
import pytest
from cryptography.hazmat.primitives.asymmetric import rsa

from core.show_pages import (
    ACCESS_ENTRY_KINDS,
    SHOW_ACCESS_ENTRY_VALUE_MAX_LENGTH,
    ShowAccessEntry,
)
from tests.ui_server_test_helpers import _save_config
from vibe import show_identity


def _identity_config(monkeypatch, tmp_path):
    monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
    config = _save_config(tmp_path)
    cloud = config.remote_access.vibe_cloud
    cloud.backend_url = "https://backend.test"
    cloud.issuer = "https://backend.test"
    cloud.jwks_uri = "https://backend.test/oauth/jwks.json"
    config.save()
    return config


def _signed_assertion(monkeypatch, claims):
    private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
    assertion = jwt.encode(
        claims,
        private_key,
        algorithm="RS256",
        headers={"typ": "JWT", "kid": "current"},
    )

    class JwkClientStub:
        def __init__(self, uri, *, timeout):
            assert uri == "https://backend.test/oauth/jwks.json"
            assert timeout == 5

        def get_signing_key_from_jwt(self, token):
            assert token == assertion
            return type("SigningKey", (), {"key": private_key.public_key()})()

    monkeypatch.setattr(show_identity, "PyJWKClient", JwkClientStub)
    return assertion


def test_show_identity_state_round_trips_only_for_its_callback_origin(monkeypatch, tmp_path):
    config = _identity_config(monkeypatch, tmp_path)
    authorization_url = show_identity.begin_show_identity_authorization(
        config,
        callback_origin="https://alex.avibe.bot",
        share_id="shared-page",
        return_target="/p/shared-page/reports/daily?tab=1",
        now=100,
    )
    parsed = urlsplit(authorization_url)
    query = parse_qs(parsed.query)

    assert parsed.path == ("/api/v1/instances/inst_123/show-identity/authorize")
    assert query["redirect_uri"] == ["https://alex.avibe.bot/auth/show-identity/callback"]
    state = show_identity.read_show_identity_state(
        config,
        query["state"][0],
        callback_origin="https://alex.avibe.bot",
        now=101,
    )
    assert state.share_id == "shared-page"
    assert state.return_target == "/p/shared-page/reports/daily?tab=1"
    assert query["nonce"] == [state.nonce]

    with pytest.raises(show_identity.ShowIdentityError):
        show_identity.read_show_identity_state(
            config,
            query["state"][0],
            callback_origin="https://other.example",
            now=101,
        )


def test_show_identity_state_rejects_tampering_and_expiry(monkeypatch, tmp_path):
    config = _identity_config(monkeypatch, tmp_path)
    authorization_url = show_identity.begin_show_identity_authorization(
        config,
        callback_origin="https://alex.avibe.bot",
        share_id="shared-page",
        return_target="/p/shared-page/",
        now=100,
    )
    state = parse_qs(urlsplit(authorization_url).query)["state"][0]

    with pytest.raises(show_identity.ShowIdentityError):
        show_identity.read_show_identity_state(
            config,
            f"{state[:-1]}{'A' if state[-1] != 'A' else 'B'}",
            callback_origin="https://alex.avibe.bot",
            now=101,
        )
    with pytest.raises(show_identity.ShowIdentityError, match="expired_state"):
        show_identity.read_show_identity_state(
            config,
            state,
            callback_origin="https://alex.avibe.bot",
            now=100 + show_identity.STATE_TTL_SECONDS,
        )


def test_show_identity_assertion_verifies_paired_issuer_instance_and_nonce(
    monkeypatch,
    tmp_path,
):
    config = _identity_config(monkeypatch, tmp_path)
    private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
    issued_at = int(time.time())
    assertion = jwt.encode(
        {
            "iss": "https://backend.test",
            "aud": "avibe-show-identity:vr_client_123",
            "sub": "user-1",
            "iat": issued_at,
            "exp": issued_at + 300,
            "jti": "assertion-1",
            "nonce": "nonce-1",
            "instance_id": "inst_123",
            "verified_email": " [email protected] ",
        },
        private_key,
        algorithm="RS256",
        headers={"typ": "JWT", "kid": "current"},
    )

    class JwkClientStub:
        def __init__(self, uri, *, timeout):
            assert uri == "https://backend.test/oauth/jwks.json"
            assert timeout == 5

        def get_signing_key_from_jwt(self, token):
            assert token == assertion
            return type("SigningKey", (), {"key": private_key.public_key()})()

    monkeypatch.setattr(show_identity, "PyJWKClient", JwkClientStub)

    identity = show_identity.verify_show_identity_assertion(
        config,
        assertion,
        expected_nonce="nonce-1",
    )
    assert identity.subject == "user-1"
    assert identity.normalized_email == "[email protected]"
    assert identity.assertion_id == "assertion-1"
    assert identity.expires_at == issued_at + 300
    assert identity.organization is None

    with pytest.raises(show_identity.ShowIdentityError, match="invalid_assertion"):
        show_identity.verify_show_identity_assertion(
            config,
            assertion,
            expected_nonce="other-nonce",
        )


def test_show_identity_assertion_reports_jwks_outage(monkeypatch, tmp_path):
    config = _identity_config(monkeypatch, tmp_path)
    private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
    issued_at = int(time.time())
    assertion = jwt.encode(
        {
            "iss": "https://backend.test",
            "aud": "avibe-show-identity:vr_client_123",
            "sub": "user-1",
            "iat": issued_at,
            "exp": issued_at + 300,
            "jti": "unavailable-assertion",
            "nonce": "nonce-1",
            "instance_id": "inst_123",
            "verified_email": "[email protected]",
        },
        private_key,
        algorithm="RS256",
        headers={"typ": "JWT", "kid": "current"},
    )

    class UnavailableJwkClient:
        def __init__(self, _uri, *, timeout):
            assert timeout == 5

        def get_signing_key_from_jwt(self, _token):
            raise show_identity.PyJWKClientConnectionError("offline")

    monkeypatch.setattr(show_identity, "PyJWKClient", UnavailableJwkClient)

    with pytest.raises(show_identity.ShowIdentityError, match="identity_unavailable"):
        show_identity.verify_show_identity_assertion(
            config,
            assertion,
            expected_nonce="nonce-1",
        )


def test_verified_show_identity_is_consumed_atomically_once():
    identity = show_identity.VerifiedShowIdentity(
        subject="user-1",
        normalized_email="[email protected]",
        assertion_id=f"atomic-{time.time_ns()}",
        expires_at=int(time.time()) + 300,
    )
    barrier = threading.Barrier(3)
    outcomes: list[str] = []

    def consume():
        barrier.wait()
        try:
            show_identity.consume_verified_show_identity(identity)
        except show_identity.ShowIdentityError as exc:
            outcomes.append(exc.reason)
        else:
            outcomes.append("accepted")

    threads = [threading.Thread(target=consume) for _ in range(2)]
    for thread in threads:
        thread.start()
    barrier.wait()
    for thread in threads:
        thread.join()

    assert sorted(outcomes) == ["accepted", "replayed_assertion"]


def test_show_guest_lease_is_page_and_share_bound_without_live_membership_state(
    monkeypatch,
    tmp_path,
):
    config = _identity_config(monkeypatch, tmp_path)
    token = show_identity.make_show_guest_lease(
        config,
        page_id="page-1",
        share_id="shared-page",
        normalized_email="[email protected]",
    )
    config.remote_access.vibe_cloud.enabled = False
    config.remote_access.vibe_cloud.backend_url = ""
    config.remote_access.vibe_cloud.issuer = ""
    config.remote_access.vibe_cloud.jwks_uri = ""

    lease = show_identity.read_show_guest_lease(
        config,
        token,
        expected_share_id="shared-page",
    )
    assert lease.page_id == "page-1"
    assert lease.normalized_email == "[email protected]"

    with pytest.raises(show_identity.ShowIdentityError):
        show_identity.read_show_guest_lease(
            config,
            token,
            expected_share_id="other-page",
        )


def _base_assertion_claims(*, issued_at: int, extra: dict | None = None) -> dict:
    claims = {
        "iss": "https://backend.test",
        "aud": "avibe-show-identity:vr_client_123",
        "sub": "user-1",
        "iat": issued_at,
        "exp": issued_at + 300,
        "jti": "assertion-org",
        "nonce": "nonce-1",
        "instance_id": "inst_123",
        "verified_email": "[email protected]",
    }
    if extra:
        claims.update(extra)
    return claims


def test_show_identity_assertion_accepts_complete_organization_block(monkeypatch, tmp_path):
    config = _identity_config(monkeypatch, tmp_path)
    issued_at = int(time.time())
    assertion = _signed_assertion(
        monkeypatch,
        _base_assertion_claims(
            issued_at=issued_at,
            extra={
                "organization_id": "org-1",
                "organization_member_id": "mem-1",
                "organization_role": "member",
                "group_ids": ["group-7", "group-8"],
            },
        ),
    )

    identity = show_identity.verify_show_identity_assertion(
        config,
        assertion,
        expected_nonce="nonce-1",
    )
    assert identity.organization is not None
    assert identity.organization.organization_id == "org-1"
    assert identity.organization.organization_member_id == "mem-1"
    assert identity.organization.organization_role == "member"
    assert identity.organization.group_ids == frozenset({"group-7", "group-8"})
    visitor = identity.visitor()
    assert visitor.has_organization_block
    assert visitor.group_ids == frozenset({"group-7", "group-8"})

    # The lease records the entry that admitted the visitor, never the
    # membership list that matched it.
    grant = ShowAccessEntry(kind="group", value="group-7", organization_id="org-1")
    token = show_identity.make_show_guest_lease(
        config,
        page_id="page-1",
        share_id="shared-page",
        normalized_email=identity.normalized_email,
        grant=grant,
    )
    lease = show_identity.read_show_guest_lease(
        config,
        token,
        expected_share_id="shared-page",
    )
    assert lease.grant == grant


def test_every_mintable_guest_lease_stays_readable(monkeypatch, tmp_path):
    """Whatever ``make_show_guest_lease`` writes, ``read_show_guest_lease`` reads.

    The lease is a cookie with a hard reader budget, so any field the writer
    can grow past that budget is an admission loop: the callback sets the
    cookie and the very next request rejects it. Seed every bounded field at
    its maximum instead of listing the sizes that happen to fit today, so a
    field widened later fails here rather than in a visitor's browser.
    """

    config = _identity_config(monkeypatch, tmp_path)
    longest_value = "g" * SHOW_ACCESS_ENTRY_VALUE_MAX_LENGTH
    longest_email = "e" * (SHOW_ACCESS_ENTRY_VALUE_MAX_LENGTH - len("@example.com")) + "@example.com"
    grants = [None] + [
        ShowAccessEntry(
            kind=kind,
            value=longest_email if kind == "email" else longest_value,
            organization_id=None if kind == "email" else longest_value,
        )
        for kind in ACCESS_ENTRY_KINDS
    ]

    for grant in grants:
        token = show_identity.make_show_guest_lease(
            config,
            page_id="s" * 128,
            share_id="shared-page",
            normalized_email=longest_email,
            grant=grant,
        )
        assert len(token.encode("utf-8")) <= show_identity.MAX_STATE_BYTES
        lease = show_identity.read_show_guest_lease(
            config,
            token,
            expected_share_id="shared-page",
        )
        assert lease.grant == (grant or ShowAccessEntry(kind="email", value=longest_email))


def test_show_identity_assertion_rejects_a_partial_organization_block(monkeypatch, tmp_path):
    config = _identity_config(monkeypatch, tmp_path)
    issued_at = int(time.time())
    assertion = _signed_assertion(
        monkeypatch,
        _base_assertion_claims(
            issued_at=issued_at,
            extra={"organization_id": "org-1", "organization_role": "member"},
        ),
    )

    with pytest.raises(show_identity.ShowIdentityError, match="invalid_assertion"):
        show_identity.verify_show_identity_assertion(
            config,
            assertion,
            expected_nonce="nonce-1",
        )


def test_show_identity_assertion_rejects_an_unknown_organization_role(monkeypatch, tmp_path):
    config = _identity_config(monkeypatch, tmp_path)
    issued_at = int(time.time())
    assertion = _signed_assertion(
        monkeypatch,
        _base_assertion_claims(
            issued_at=issued_at,
            extra={
                "organization_id": "org-1",
                "organization_member_id": "mem-1",
                "organization_role": "guest",
                "group_ids": [],
            },
        ),
    )

    with pytest.raises(show_identity.ShowIdentityError, match="invalid_assertion"):
        show_identity.verify_show_identity_assertion(
            config,
            assertion,
            expected_nonce="nonce-1",
        )