Skip to content

Instantly share code, notes, and snippets.

@gensart-x
Created October 15, 2025 03:51
Show Gist options
  • Save gensart-x/91588d1f515d8b7c1e8107bba7e17878 to your computer and use it in GitHub Desktop.
Save gensart-x/91588d1f515d8b7c1e8107bba7e17878 to your computer and use it in GitHub Desktop.
Python utility helper to whitelist described routes. I used this in my FastAPI
def is_whitelisted(path: str, whitelisted_paths: list[str]) -> bool:
for pattern in whitelisted_paths:
# Exact match for "/"
if pattern == "/" and path == "/":
return True
# Simple wildcard match: /api/v1/* matches anything under /api/v1/
if pattern.endswith("/*"):
base = pattern[:-1] # keep the trailing slash
if path.startswith(base):
return True
# Exact match
if path == pattern:
return True
return False
# Examples
is_whitelisted('/api/v1/users', ['/api/v1/users']) # True
is_whitelisted('/api/v1/users', ['/api/v1/*']) # True
is_whitelisted('/api/v1/users', ['/api/*']) # True
is_whitelisted('/api/v1/profiles', ['/api/*']) # True
is_whitelisted('/api/v1/profiles', ['/api/v1/users', '/api/v1/profiles']) # True
is_whitelisted('/api/v1/profiles', ['/api/v1/users']) # False
is_whitelisted('/web', ['/api/*']) # False
is_whitelisted('/api/v1/users/profile', ['/api/v1/users']) # False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment