sangkyoonnam commented on code in PR #946:
URL: https://github.com/apache/burr/pull/946#discussion_r4206431195


##########
burr/tracking/common/identifiers.py:
##########
@@ -0,0 +1,92 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Validation for the identifiers (project names, application ids) that the 
tracking
+layer turns into filesystem paths, plus a helper that keeps the resulting 
paths inside
+the storage directory."""
+
+import os
+import re
+from typing import Optional
+
+from burr import system
+
+# Letters, digits, underscore, hyphen, colon and dot. This covers uuid4 (hex 
and
+# hyphens) as well as the session/conversation style ids applications tend to 
pass
+# through as app ids.
+_ALLOWED_CHARACTERS = r"A-Za-z0-9_\-:."
+IDENTIFIER_PATTERN = re.compile(f"^[{_ALLOWED_CHARACTERS}]+$")
+MAX_IDENTIFIER_LENGTH = 255
+
+
+def validate_identifier(value: str, what: str, *, on_windows: Optional[bool] = 
None) -> str:
+    """Checks that ``value`` can be used as a single path component under the 
storage directory.
+
+    The rule: a non-empty string of at most 255 characters drawn from letters, 
digits, ``_``,
+    ``-``, ``:`` and ``.``, and not the special directory names ``.`` or 
``..``. On Windows
+    ``:`` is also refused, as it is a drive/stream separator there.
+
+    :param value: the identifier to check
+    :param what: short label for the error message, e.g. ``"app_id"`` or 
``"project"``
+    :param on_windows: platform override, defaults to the current platform; 
exposed for tests
+    :return: ``value`` unchanged, so the call can be used inline
+    :raises ValueError: if the identifier does not meet the rule
+    """
+    if on_windows is None:
+        on_windows = system.IS_WINDOWS
+    if not isinstance(value, str):
+        raise ValueError(f"{what} must be a string, got 
{type(value).__name__}: {value!r}")
+    if not value:
+        raise ValueError(f"{what} must not be empty")
+    if len(value) > MAX_IDENTIFIER_LENGTH:
+        raise ValueError(
+            f"{what} must be at most {MAX_IDENTIFIER_LENGTH} characters, got 
{len(value)}"
+        )
+    if value in (".", ".."):
+        raise ValueError(f"{what} must not be '.' or '..', got {value!r}")
+    if not IDENTIFIER_PATTERN.match(value) or (on_windows and ":" in value):

Review Comment:
   `IDENTIFIER_PATTERN.match(value)` with a pattern ending in `$` accepts 
`"x\n"` and `"..\n"`; `$` matches before a final newline. 
`IDENTIFIER_PATTERN.fullmatch(value)` (or `\Z` in the pattern) rejects them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to