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(permissions): reject unroutable project ids

Max3bbe6a84aa80
diff --git a/tests/test_permissions.py b/tests/test_permissions.py
index 3bb8de215..94e3227c0 100644
--- a/tests/test_permissions.py
+++ b/tests/test_permissions.py
@@ -138,6 +138,15 @@ NONEMPTY_PROJECTION_STRING_PATHS = [
),
]
+INVALID_PROJECT_ROUTE_IDS = [
+ pytest.param("project/child", id="forward-embedded"),
+ pytest.param("/project", id="forward-leading"),
+ pytest.param("project/", id="forward-trailing"),
+ pytest.param(r"project\child", id="backslash-embedded"),
+ pytest.param(r"\project", id="backslash-leading"),
+ pytest.param("project\\", id="backslash-trailing"),
+]
+
def _replace_nested(value: dict, path: tuple[str | int, ...], replacement: object) -> None:
parent: Any = value
@@ -683,6 +692,93 @@ def test_permissions_rejects_a_blank_matching_instance_identifier(instance_id: s
)
[email protected]("project_id", INVALID_PROJECT_ROUTE_IDS)
+def test_permissions_rejects_project_route_ids_before_caching(
+ monkeypatch,
+ project_id: str,
+) -> None:
+ malformed = _complete_projection()
+ malformed["projects"][0]["project_id"] = project_id
+ monkeypatch.setattr(
+ permissions.requests,
+ "request",
+ lambda *_args, **_kwargs: _Response(200, malformed),
+ )
+
+ with pytest.raises(permissions.PermissionsInvalidResponseError):
+ permissions.get_current_permissions(_config())
+
+ assert not permissions._cache_path().exists() # noqa: SLF001
+
+
[email protected]("project_id", INVALID_PROJECT_ROUTE_IDS)
+def test_permissions_ignores_cached_project_route_ids(
+ monkeypatch,
+ project_id: str,
+) -> None:
+ malformed = _complete_projection()
+ malformed["projects"][0]["project_id"] = project_id
+ permissions._write_cache("inst-123", malformed) # noqa: SLF001
+ monkeypatch.setattr(
+ permissions.requests,
+ "request",
+ lambda *_args, **_kwargs: (_ for _ in ()).throw(requests.ConnectionError()),
+ )
+
+ with pytest.raises(permissions.PermissionsUnavailableError):
+ permissions.get_current_permissions(_config())
+
+ assert permissions._read_cache("inst-123") is None # noqa: SLF001
+
+
[email protected]("project_id", INVALID_PROJECT_ROUTE_IDS)
+def test_permissions_rejects_project_route_ids_before_mutation(
+ monkeypatch,
+ project_id: str,
+) -> None:
+ monkeypatch.setattr(
+ permissions.requests,
+ "request",
+ lambda *_args, **_kwargs: pytest.fail("invalid Project ID reached Backend"),
+ )
+
+ with pytest.raises(
+ permissions.PermissionsInvalidResponseError,
+ match="invalid_project_id",
+ ):
+ permissions.update_project_access(
+ project_id,
+ {
+ "mode": "inherit",
+ "bindings": [],
+ "if_match_revision": 2,
+ "if_match_instance_id": "inst-123",
+ },
+ _config(),
+ )
+
+
[email protected]("project_id", INVALID_PROJECT_ROUTE_IDS)
+def test_permissions_rejects_project_route_ids_in_mutation_results(
+ project_id: str,
+) -> None:
+ valid = _complete_projection()
+ malformed_project = {
+ **valid["projects"][0],
+ "project_id": project_id,
+ }
+
+ with pytest.raises(permissions.PermissionsInvalidResponseError):
+ permissions._validated_project_result( # noqa: SLF001
+ {
+ "ok": True,
+ "project": malformed_project,
+ "authorization_revision": 4,
+ },
+ project_id,
+ )
+
+
def test_permissions_preserves_additive_backend_capabilities_live_and_offline(
monkeypatch,
) -> None:
diff --git a/vibe/permissions.py b/vibe/permissions.py
index c16839a06..81a0f1cc8 100644
--- a/vibe/permissions.py
+++ b/vibe/permissions.py
@@ -34,6 +34,7 @@ _PROJECT_ACCESS_MODES = frozenset({"inherit", "restricted"})
_PROJECT_SYNC_STATUSES = frozenset({"in_sync", "pending", "offline", "error", "deleted"})
_POLICY_SYNC_STATUSES = frozenset({"none", "in_sync", "applying", "offline", "error"})
_SYNC_COUNT_KEYS = ("active", "error", "offline", "applying", "in_sync")
+_PROJECT_ID_PATH_SEPARATORS = frozenset({"/", "\\"})
logger = logging.getLogger(__name__)
_CACHE_LOCK = threading.RLock()
@@ -136,6 +137,19 @@ def _require_nonempty_string(value: Any, *, nullable: bool = False) -> None:
_invalid_response()
+def _is_valid_project_id(value: Any) -> bool:
+ return (
+ isinstance(value, str)
+ and bool(value.strip())
+ and not any(separator in value for separator in _PROJECT_ID_PATH_SEPARATORS)
+ )
+
+
+def _require_project_id(value: Any) -> None:
+ if not _is_valid_project_id(value):
+ _invalid_response()
+
+
def _require_enum(value: Any, allowed: frozenset[str]) -> None:
if not isinstance(value, str) or value not in allowed:
_invalid_response()
@@ -164,7 +178,7 @@ def _validate_access_entries(value: Any) -> list[Any]:
def _validate_project(value: Any) -> dict[str, Any]:
project = _require_mapping(value)
_require_keys(project, "project_id", "organization_id", "display_name", "access", "sync")
- _require_nonempty_string(project["project_id"])
+ _require_project_id(project["project_id"])
_require_nonempty_string(project["organization_id"], nullable=True)
_require_string(project["display_name"])
@@ -572,7 +586,7 @@ def update_project_access(
payload: Mapping[str, Any],
config: V2Config | None = None,
) -> dict[str, Any]:
- if not isinstance(project_id, str) or not project_id or "/" in project_id:
+ if not _is_valid_project_id(project_id):
raise PermissionsInvalidResponseError("invalid_project_id")
config, load_current_config = _request_config(config)
expected_instance_id, backend_payload = _mutation_payload(payload)