Spaces:
Sleeping
Sleeping
Sync app from GitHub Actions (feature/project-state-security-privacy-review)
Browse files
docs/HF_PRIVATE_STATE_OAUTH_BATCHING_IMPLEMENTATION_PLAN.md
CHANGED
|
@@ -231,6 +231,17 @@ Changes:
|
|
| 231 |
Implemented.
|
| 232 |
5. Reject state validation submissions that would fall back from a signed-in
|
| 233 |
collaborator to an administrator token. Implemented.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
Tests:
|
| 236 |
|
|
|
|
| 231 |
Implemented.
|
| 232 |
5. Reject state validation submissions that would fall back from a signed-in
|
| 233 |
collaborator to an administrator token. Implemented.
|
| 234 |
+
6. Distinguish OAuth sessions from manual-token sessions and reject manual
|
| 235 |
+
token creation/proof attempts in the hosted Space. Implemented after the
|
| 236 |
+
first attempted test used manual tokens and therefore did not exercise
|
| 237 |
+
`contribute-repos`.
|
| 238 |
+
|
| 239 |
+
Test note:
|
| 240 |
+
|
| 241 |
+
- `upload_test5_state` was created and tested through manual-token sessions;
|
| 242 |
+
its failed collaborator write is not a valid OAuth permission proof.
|
| 243 |
+
- The next proof must create a new repository, such as `upload_test6_state`,
|
| 244 |
+
after the admin completes both OAuth login steps in the hosted Space.
|
| 245 |
|
| 246 |
Tests:
|
| 247 |
|
src/auth/auth_service.py
CHANGED
|
@@ -30,6 +30,7 @@ class Session:
|
|
| 30 |
created_at: datetime
|
| 31 |
last_activity: datetime
|
| 32 |
expires_at: datetime
|
|
|
|
| 33 |
|
| 34 |
def is_expired(self) -> bool:
|
| 35 |
"""Check if session has expired."""
|
|
@@ -216,6 +217,7 @@ class AuthService:
|
|
| 216 |
self,
|
| 217 |
username: str,
|
| 218 |
auto_promote_to_admin: bool = False,
|
|
|
|
| 219 |
) -> Optional[Session]:
|
| 220 |
if username not in self._user_access:
|
| 221 |
if not auto_promote_to_admin:
|
|
@@ -251,6 +253,7 @@ class AuthService:
|
|
| 251 |
created_at=now,
|
| 252 |
last_activity=now,
|
| 253 |
expires_at=now + timedelta(minutes=self.session_ttl_minutes),
|
|
|
|
| 254 |
)
|
| 255 |
|
| 256 |
self._sessions[session_id] = session
|
|
@@ -276,6 +279,7 @@ class AuthService:
|
|
| 276 |
username=username,
|
| 277 |
token=token_value,
|
| 278 |
email=email_value,
|
|
|
|
| 279 |
)
|
| 280 |
|
| 281 |
def login_with_verified_hf_identity(
|
|
@@ -284,6 +288,7 @@ class AuthService:
|
|
| 284 |
username: str,
|
| 285 |
token: str,
|
| 286 |
email: str | None = None,
|
|
|
|
| 287 |
) -> tuple[Optional[Session], str]:
|
| 288 |
"""Create a session from an identity already verified by Hugging Face OAuth."""
|
| 289 |
username_value = (username or "").strip()
|
|
@@ -299,6 +304,7 @@ class AuthService:
|
|
| 299 |
session = self.login_internal(
|
| 300 |
username=username_value,
|
| 301 |
auto_promote_to_admin=is_first_user,
|
|
|
|
| 302 |
)
|
| 303 |
if session is None:
|
| 304 |
return None, f"User '{username_value}' is not invited to any project yet"
|
|
|
|
| 30 |
created_at: datetime
|
| 31 |
last_activity: datetime
|
| 32 |
expires_at: datetime
|
| 33 |
+
authentication_method: str = "username"
|
| 34 |
|
| 35 |
def is_expired(self) -> bool:
|
| 36 |
"""Check if session has expired."""
|
|
|
|
| 217 |
self,
|
| 218 |
username: str,
|
| 219 |
auto_promote_to_admin: bool = False,
|
| 220 |
+
authentication_method: str = "username",
|
| 221 |
) -> Optional[Session]:
|
| 222 |
if username not in self._user_access:
|
| 223 |
if not auto_promote_to_admin:
|
|
|
|
| 253 |
created_at=now,
|
| 254 |
last_activity=now,
|
| 255 |
expires_at=now + timedelta(minutes=self.session_ttl_minutes),
|
| 256 |
+
authentication_method=authentication_method,
|
| 257 |
)
|
| 258 |
|
| 259 |
self._sessions[session_id] = session
|
|
|
|
| 279 |
username=username,
|
| 280 |
token=token_value,
|
| 281 |
email=email_value,
|
| 282 |
+
authentication_method="hf_token",
|
| 283 |
)
|
| 284 |
|
| 285 |
def login_with_verified_hf_identity(
|
|
|
|
| 288 |
username: str,
|
| 289 |
token: str,
|
| 290 |
email: str | None = None,
|
| 291 |
+
authentication_method: str = "oauth",
|
| 292 |
) -> tuple[Optional[Session], str]:
|
| 293 |
"""Create a session from an identity already verified by Hugging Face OAuth."""
|
| 294 |
username_value = (username or "").strip()
|
|
|
|
| 304 |
session = self.login_internal(
|
| 305 |
username=username_value,
|
| 306 |
auto_promote_to_admin=is_first_user,
|
| 307 |
+
authentication_method=authentication_method,
|
| 308 |
)
|
| 309 |
if session is None:
|
| 310 |
return None, f"User '{username_value}' is not invited to any project yet"
|
src/ui/app_factory.py
CHANGED
|
@@ -3371,6 +3371,28 @@ def create_app() -> gr.Blocks:
|
|
| 3371 |
return "Fill slug, name, and repo id.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), session
|
| 3372 |
if visibility_value not in {"private", "collaborative"}:
|
| 3373 |
return "Visibility must be private or collaborative.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), session
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3374 |
if admin_manager.get_project(slug) is not None:
|
| 3375 |
admin_projects = _admin_projects_for_session(session)
|
| 3376 |
return (
|
|
@@ -4387,6 +4409,12 @@ def create_app() -> gr.Blocks:
|
|
| 4387 |
def test_private_state_authorization(selected: str, session):
|
| 4388 |
if session is None:
|
| 4389 |
return "Login with your Hugging Face account before running the state authorization test."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4390 |
project_slug = (selected or "").strip()
|
| 4391 |
project = admin_manager.get_project(project_slug) if project_slug else None
|
| 4392 |
if project is None or project_slug not in session.authorized_projects:
|
|
|
|
| 3371 |
return "Fill slug, name, and repo id.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), session
|
| 3372 |
if visibility_value not in {"private", "collaborative"}:
|
| 3373 |
return "Visibility must be private or collaborative.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), session
|
| 3374 |
+
if (
|
| 3375 |
+
_is_running_in_hf_space()
|
| 3376 |
+
and runtime_config.hf_project_state_writes_enabled
|
| 3377 |
+
and getattr(session, "authentication_method", "") != "oauth"
|
| 3378 |
+
):
|
| 3379 |
+
return (
|
| 3380 |
+
"Private `_state` test projects must be created through OAuth. Return to Login, complete "
|
| 3381 |
+
"both Hugging Face sign-in steps, and create a new test project; a project created through "
|
| 3382 |
+
"manual token login cannot prove `contribute-repos` authorization.",
|
| 3383 |
+
_project_rows(),
|
| 3384 |
+
gr.update(),
|
| 3385 |
+
gr.update(),
|
| 3386 |
+
gr.update(),
|
| 3387 |
+
gr.update(),
|
| 3388 |
+
gr.update(),
|
| 3389 |
+
gr.update(),
|
| 3390 |
+
gr.update(),
|
| 3391 |
+
gr.update(),
|
| 3392 |
+
gr.update(),
|
| 3393 |
+
gr.update(),
|
| 3394 |
+
session,
|
| 3395 |
+
)
|
| 3396 |
if admin_manager.get_project(slug) is not None:
|
| 3397 |
admin_projects = _admin_projects_for_session(session)
|
| 3398 |
return (
|
|
|
|
| 4409 |
def test_private_state_authorization(selected: str, session):
|
| 4410 |
if session is None:
|
| 4411 |
return "Login with your Hugging Face account before running the state authorization test."
|
| 4412 |
+
if getattr(session, "authentication_method", "") != "oauth":
|
| 4413 |
+
return (
|
| 4414 |
+
"This authorization proof requires OAuth. Return to Login and complete both steps: "
|
| 4415 |
+
"**1. Sign in with Hugging Face** and **2. Enter workspace with OAuth authorization**. "
|
| 4416 |
+
"Manual token login does not test `contribute-repos`."
|
| 4417 |
+
)
|
| 4418 |
project_slug = (selected or "").strip()
|
| 4419 |
project = admin_manager.get_project(project_slug) if project_slug else None
|
| 4420 |
if project is None or project_slug not in session.authorized_projects:
|
src/ui/login_page.py
CHANGED
|
@@ -71,6 +71,7 @@ def perform_oauth_login(
|
|
| 71 |
username=username,
|
| 72 |
token=oauth_token.token,
|
| 73 |
email=email,
|
|
|
|
| 74 |
)
|
| 75 |
if session is None:
|
| 76 |
return "", message
|
|
@@ -103,10 +104,20 @@ def create_login_page(
|
|
| 103 |
|
| 104 |
oauth_continue_button = None
|
| 105 |
if enable_oauth_login:
|
| 106 |
-
gr.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
username_input = gr.Textbox(
|
| 111 |
label="Username",
|
| 112 |
placeholder="Enter your username" if allow_username_login else "Username login disabled in this deployment",
|
|
@@ -114,7 +125,7 @@ def create_login_page(
|
|
| 114 |
interactive=allow_username_login,
|
| 115 |
)
|
| 116 |
hf_token_input = gr.Textbox(
|
| 117 |
-
label="Hugging Face Token (
|
| 118 |
placeholder="hf_xxx...",
|
| 119 |
type="password",
|
| 120 |
lines=1,
|
|
|
|
| 71 |
username=username,
|
| 72 |
token=oauth_token.token,
|
| 73 |
email=email,
|
| 74 |
+
authentication_method="oauth",
|
| 75 |
)
|
| 76 |
if session is None:
|
| 77 |
return "", message
|
|
|
|
| 104 |
|
| 105 |
oauth_continue_button = None
|
| 106 |
if enable_oauth_login:
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"**OAuth access required for private collaborative state.** "
|
| 109 |
+
"Complete both steps below; signing into Hugging Face alone does not open an app session."
|
| 110 |
+
)
|
| 111 |
+
gr.LoginButton("1. Sign in with Hugging Face", logout_value="Sign out ({})")
|
| 112 |
+
oauth_continue_button = gr.Button(
|
| 113 |
+
"2. Enter workspace with OAuth authorization",
|
| 114 |
+
variant="primary",
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
gr.Markdown(
|
| 118 |
+
"Manual token access is available for development or legacy access, "
|
| 119 |
+
"but cannot be used for the private collaborative state authorization test."
|
| 120 |
+
)
|
| 121 |
username_input = gr.Textbox(
|
| 122 |
label="Username",
|
| 123 |
placeholder="Enter your username" if allow_username_login else "Username login disabled in this deployment",
|
|
|
|
| 125 |
interactive=allow_username_login,
|
| 126 |
)
|
| 127 |
hf_token_input = gr.Textbox(
|
| 128 |
+
label="Hugging Face Token (legacy/development only)",
|
| 129 |
placeholder="hf_xxx...",
|
| 130 |
type="password",
|
| 131 |
lines=1,
|
tests/unit/test_auth_service.py
CHANGED
|
@@ -211,6 +211,7 @@ class TestAuthService:
|
|
| 211 |
|
| 212 |
assert session is not None
|
| 213 |
assert "Welcome" in message
|
|
|
|
| 214 |
assert service.get_hf_token_for_user("hf_user") == "hf_test_token"
|
| 215 |
assert service.get_known_email_for_user("hf_user") == "hf_user@example.org"
|
| 216 |
|
|
@@ -225,6 +226,7 @@ class TestAuthService:
|
|
| 225 |
|
| 226 |
assert session is not None
|
| 227 |
assert session.username == "oauth_user"
|
|
|
|
| 228 |
assert "Welcome, oauth_user" in message
|
| 229 |
assert service.get_hf_token_for_user("oauth_user") == "oauth_access_token"
|
| 230 |
assert service.get_known_email_for_user("oauth_user") == "oauth_user@example.org"
|
|
|
|
| 211 |
|
| 212 |
assert session is not None
|
| 213 |
assert "Welcome" in message
|
| 214 |
+
assert session.authentication_method == "hf_token"
|
| 215 |
assert service.get_hf_token_for_user("hf_user") == "hf_test_token"
|
| 216 |
assert service.get_known_email_for_user("hf_user") == "hf_user@example.org"
|
| 217 |
|
|
|
|
| 226 |
|
| 227 |
assert session is not None
|
| 228 |
assert session.username == "oauth_user"
|
| 229 |
+
assert session.authentication_method == "oauth"
|
| 230 |
assert "Welcome, oauth_user" in message
|
| 231 |
assert service.get_hf_token_for_user("oauth_user") == "oauth_access_token"
|
| 232 |
assert service.get_known_email_for_user("oauth_user") == "oauth_user@example.org"
|
tests/unit/test_login_page.py
CHANGED
|
@@ -51,6 +51,7 @@ def test_perform_login_allows_hf_token_when_username_disabled(monkeypatch) -> No
|
|
| 51 |
|
| 52 |
assert session_id
|
| 53 |
assert "Welcome, hf_user" in message
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
def test_perform_oauth_login_uses_verified_profile_and_token() -> None:
|
|
@@ -67,6 +68,7 @@ def test_perform_oauth_login_uses_verified_profile_and_token() -> None:
|
|
| 67 |
|
| 68 |
assert session_id
|
| 69 |
assert "Welcome, oauth_user" in message
|
|
|
|
| 70 |
assert auth_service.get_hf_token_for_user("oauth_user") == "oauth_token"
|
| 71 |
assert auth_service.get_known_email_for_user("oauth_user") == "oauth_user@example.org"
|
| 72 |
|
|
|
|
| 51 |
|
| 52 |
assert session_id
|
| 53 |
assert "Welcome, hf_user" in message
|
| 54 |
+
assert auth_service.get_session(session_id).authentication_method == "hf_token"
|
| 55 |
|
| 56 |
|
| 57 |
def test_perform_oauth_login_uses_verified_profile_and_token() -> None:
|
|
|
|
| 68 |
|
| 69 |
assert session_id
|
| 70 |
assert "Welcome, oauth_user" in message
|
| 71 |
+
assert auth_service.get_session(session_id).authentication_method == "oauth"
|
| 72 |
assert auth_service.get_hf_token_for_user("oauth_user") == "oauth_token"
|
| 73 |
assert auth_service.get_known_email_for_user("oauth_user") == "oauth_user@example.org"
|
| 74 |
|