You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tooling.apache.org by ar...@apache.org on 2026/04/15 11:02:03 UTC

(tooling-trusted-releases) branch main updated: #987 - type-level validation for ASFUIDs

This is an automated email from the ASF dual-hosted git repository.

arm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-releases.git


The following commit(s) were added to refs/heads/main by this push:
     new 3919893a #987 - type-level validation for ASFUIDs
3919893a is described below

commit 3919893a1ceba650f881a9f44a429ad659f3e4a4
Author: Alastair McFarlane <ar...@apache.org>
AuthorDate: Wed Apr 15 12:01:35 2026 +0100

    #987 - type-level validation for ASFUIDs
---
 atr/admin/__init__.py |  4 ++--
 atr/models/safe.py    | 15 +++++++++++++++
 atr/principal.py      | 12 +++++-------
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/atr/admin/__init__.py b/atr/admin/__init__.py
index 5b33e6b7..a240e14a 100644
--- a/atr/admin/__init__.py
+++ b/atr/admin/__init__.py
@@ -180,9 +180,9 @@ async def browse_as_post(
     new_uid = browse_form.uid
 
     try:
-        committer = principal.Committer(new_uid)
+        committer = principal.Committer(safe.AsfUid(new_uid))
         await asyncio.to_thread(committer.verify)
-    except principal.CommitterError as exc:
+    except (principal.CommitterError, ValueError) as exc:
         await quart.flash(f"Unable to browse as '{new_uid}': {exc}", "error")
         return await session.redirect(browse_as_get)
 
diff --git a/atr/models/safe.py b/atr/models/safe.py
index 3e86f3f8..ab727024 100644
--- a/atr/models/safe.py
+++ b/atr/models/safe.py
@@ -25,6 +25,7 @@ from typing import Annotated, Any, Final
 import pydantic
 
 _ALPHANUM: Final = frozenset(string.ascii_letters + string.digits + "-")
+_ASF_UID_CHARS: Final = frozenset(string.ascii_lowercase + string.digits + "-_")
 _NUMERIC: Final = frozenset(string.digits)
 _PATH_CHARS: Final = frozenset(string.ascii_letters + string.digits + "-._+~/()")
 _VERSION_CHARS: Final = _ALPHANUM | frozenset(".+")
@@ -178,6 +179,20 @@ class Alphanumeric(SafeType):
         return _ALPHANUM
 
 
+class AsfUid(SafeType):
+    """An ASF user ID validated to contain only lowercase letters, digits, hyphens, and underscores."""
+
+    @classmethod
+    def _valid_chars(cls) -> frozenset[str]:
+        return _ASF_UID_CHARS
+
+    def _additional_validations(self, value: str) -> None:
+        if len(value) < 3:
+            raise ValueError("ASF UID must be at least 3 characters")
+        if value[0] not in string.ascii_lowercase:
+            raise ValueError("ASF UID must start with a lowercase letter")
+
+
 class CommitteeKey(Alphanumeric):
     pass
 
diff --git a/atr/principal.py b/atr/principal.py
index 4b8bf523..1e0bc8ff 100644
--- a/atr/principal.py
+++ b/atr/principal.py
@@ -18,7 +18,6 @@
 # Derived from apache/infrastructure-oauth/app/lib/ldap.py
 
 import asyncio
-import re
 import time
 from typing import Final
 
@@ -26,6 +25,7 @@ from typing import Final
 import atr.config as config
 import atr.ldap as ldap
 import atr.log as log
+import atr.models.safe as safe
 import atr.models.sql as sql
 import atr.sessions as sessions
 import atr.util as util
@@ -66,11 +66,9 @@ type UID = web.Committer | str | None | ArgumentNoneType
 class Committer:
     """Verifies and loads a committer's credentials via LDAP."""
 
-    def __init__(self, user: str) -> None:
-        if not re.match(r"^[-_a-z0-9]+$", user):
-            raise CommitterError("Invalid characters in User ID. Only lower-case alphanumerics, '-' and '_' allowed.")
+    def __init__(self, user: safe.AsfUid) -> None:
         self.user = user
-        self.uid = user
+        self.uid = str(user)
         self.dn = LDAP_DN % user
         self.email = f"{user}@apache.org"
         self.fullname: str = ""
@@ -304,7 +302,7 @@ class AuthoriserLDAP:
                 return
 
         try:
-            c = Committer(asf_uid)
+            c = Committer(safe.AsfUid(asf_uid))
             await asyncio.to_thread(c.verify)
 
             committees = frozenset(c.pmcs)
@@ -314,7 +312,7 @@ class AuthoriserLDAP:
             self.__cache.member_of[asf_uid] = committees
             self.__cache.participant_of[asf_uid] = projects
             self.__cache.last_refreshed[asf_uid] = int(time.time())
-        except CommitterError as e:
+        except (CommitterError, ValueError) as e:
             raise AuthenticationError(f"Failed to verify committer: {e}") from e
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org