Project: pygitweb

a2948bf """
FastAPI dependencies for shared request validation (exported repositories, etc.).
"""
from __future__ import annotations
1e48deb import ipaddress
a2948bf from typing import Annotated, Any
1e48deb from fastapi import Depends, Form, HTTPException, Query, Request
3558586 from pygittools.validation import is_valid_project
a2948bf
fbd311b from pygitweb.auth import (
73c9c30 access_token_from_request,
ensure_read_project,
ensure_write_project,
fbd311b )
a2948bf from pygitweb.config import settings
from pygitweb.projects import git_get_projects_list, project_in_list
def _visible_projects_list() -> list[dict[str, Any]]:
73c9c30 return git_get_projects_list(
filter_path="",
paranoid=settings.STRICT_EXPORT,
export_ok=settings.EXPORT_OK,
)
a2948bf
def project_visible_in_list(project: str) -> bool:
73c9c30 """True if ``project`` appears in the same listing as the project picker."""
return project_in_list(project, _visible_projects_list)
a2948bf
def require_valid_project(project: str) -> str:
73c9c30 """Reject empty or unknown project paths (export_ok, strict list, etc.)."""
if not project or project == "":
raise HTTPException(status_code=400, detail="Project needed")
if not is_valid_project(
project,
settings.PROJECTROOT,
settings.EXPORT_OK,
settings.STRICT_EXPORT,
project_visible_in_list,
):
raise HTTPException(status_code=404, detail="No such project")
return project
a2948bf
def require_valid_project_name(name: str) -> str:
73c9c30 """Same as ``require_valid_project`` for a path parameter named ``name``."""
return require_valid_project(name)
a2948bf
def require_valid_project_from_query(
73c9c30 project: Annotated[str, Query(..., description="Project path")],
a2948bf ) -> str:
73c9c30 return require_valid_project(project)
a2948bf
def require_valid_project_form(project: Annotated[str, Form()]) -> str:
73c9c30 return require_valid_project(project)
a2948bf
fbd311b def require_readable_project(
73c9c30 project: str,
token: Annotated[str | None, Depends(access_token_from_request)],
fbd311b ) -> str:
73c9c30 validated = require_valid_project(project)
ensure_read_project(validated, token)
return validated
fbd311b
def require_readable_project_from_query(
73c9c30 project: Annotated[str, Query(..., description="Project path")],
token: Annotated[str | None, Depends(access_token_from_request)],
fbd311b ) -> str:
73c9c30 return require_readable_project(project, token)
fbd311b
def require_readable_project_form(
73c9c30 project: Annotated[str, Form()],
token: Annotated[str | None, Depends(access_token_from_request)],
fbd311b ) -> str:
73c9c30 return require_readable_project(project, token)
fbd311b
def require_readable_project_name(
73c9c30 name: str,
token: Annotated[str | None, Depends(access_token_from_request)],
fbd311b ) -> str:
73c9c30 return require_readable_project(name, token)
fbd311b
def require_writable_project_name(
73c9c30 name: str,
token: Annotated[str | None, Depends(access_token_from_request)],
fbd311b ) -> str:
73c9c30 validated = require_valid_project(name)
ensure_write_project(validated, token)
return validated
fbd311b
1e48deb def is_loopback_host(host: str | None) -> bool:
73c9c30 if not host:
return False
if host == "localhost":
return True
try:
return ipaddress.ip_address(host).is_loopback
except ValueError:
return False
1e48deb
def require_loopback_client(request: Request) -> None:
73c9c30 """Reject non-local callers (hooks should POST from the same host)."""
client = request.client
if client is not None and is_loopback_host(client.host):
return
raise HTTPException(status_code=403, detail="Forbidden")
1e48deb
a2948bf def require_notify_project(
73c9c30 project: Annotated[str | None, Query(alias="project", description="Repository path")] = None,
a2948bf ) -> str:
73c9c30 """Internal notify: project must appear in the visible list (hook / long-poll contract)."""
if not project or not project.strip():
raise HTTPException(status_code=400, detail="project query parameter required")
p = project.strip()
if not project_visible_in_list(p):
raise HTTPException(status_code=404, detail="No such project")
return p
a2948bf
def board_create_resolved_project(
73c9c30 token: Annotated[str | None, Depends(access_token_from_request)],
p: Annotated[str | None, Query(alias="p")] = None,
project: Annotated[str | None, Query(alias="project")] = None,
a2948bf ) -> str:
73c9c30 """Resolve ``?p=`` / ``?project=`` then validate as a normal project path."""
chosen = (project or p or "").strip()
if not chosen:
raise HTTPException(status_code=400, detail="Project needed (use ?project= or ?p=)")
return require_readable_project(chosen, token)
fbd311b
def filter_projects_by_read_access(
73c9c30 projects: list[dict[str, Any]],
token: str | None,
fbd311b ) -> list[dict[str, Any]]:
73c9c30 from pygitweb.auth import can_read_project
fbd311b
73c9c30 return [p for p in projects if can_read_project(p.get("path", ""), token)]
a2948bf
ValidatedPathProject = Annotated[str, Depends(require_valid_project)]
fbd311b ValidatedReadableProject = Annotated[str, Depends(require_readable_project)]
a2948bf ValidatedQueryProject = Annotated[str, Depends(require_valid_project_from_query)]
fbd311b ValidatedReadableQueryProject = Annotated[str, Depends(require_readable_project_from_query)]
a2948bf ValidatedSettingsProject = Annotated[str, Depends(require_valid_project_name)]
fbd311b ValidatedReadableSettingsProject = Annotated[str, Depends(require_readable_project_name)]
ValidatedWritableSettingsProject = Annotated[str, Depends(require_writable_project_name)]
a2948bf ValidatedBoardCreateProject = Annotated[str, Depends(board_create_resolved_project)]
ValidatedNotifyProject = Annotated[str, Depends(require_notify_project)]
ValidatedFormProject = Annotated[str, Depends(require_valid_project_form)]
fbd311b ValidatedReadableFormProject = Annotated[str, Depends(require_readable_project_form)]