@@ -33,7 +33,32 @@ def decode(byte_string: bytes) -> str:
3333 encoding = charset_normalizer .detect (byte_string )["encoding" ]
3434 return byte_string .decode (encoding )
3535
36+
3637def is_exempt_request_path (path : str , exempt : set ) -> bool :
38+ """
39+ Check if a request path should be exempt from authentication based on prefix matching.
40+
41+ This function performs prefix matching with path separator boundary checking to prevent
42+ false positives. A path matches an exempt prefix only if it starts with the exempt path
43+ followed by a path separator ('/').
44+
45+ :param str path: The request path to check (e.g., '/health/live', '/api/users')
46+ :param set exempt: Set of exempt path prefixes (e.g., {'/health', '/metrics'})
47+ :return: True if the path matches any exempt prefix, False otherwise
48+ :rtype: bool
49+
50+ Examples:
51+ Matching cases (returns True):
52+ - path='/health/live', exempt={'/health'} -> True
53+ - path='/health/ready', exempt={'/health'} -> True
54+ - path='/metrics/prometheus', exempt={'/metrics'} -> True
55+
56+ Non-matching cases (returns False):
57+ - path='/health', exempt={'/health'} -> False (exact match without trailing slash)
58+ - path='/api-admin', exempt={'/api'} -> False (not a path separator boundary)
59+ - path='/app_status_admin', exempt={'/app_status'} -> False (underscore, not separator)
60+ - path='/healthcare', exempt={'/health'} -> False (different path)
61+ """
3762 for exempt_path in exempt :
3863 # Exact match or prefix match with path separator
3964 # For instance this prevents /api matching /api-admin or /app_status matching /app_status_admin
0 commit comments