25 lines
900 B
Python
25 lines
900 B
Python
def normalize_policy_path(value:str) -> str:
|
|||
|
|
normalized = value.strip().replace("\\", "/").lstrip("/")
|
||
|
|
while "//" in normalized:
|
||
|
|
normalized = normalized.replace("//", "/")
|
||
|
|
return normalized
|
||
|
|
|
||
|
|
def is_path_allowed(path:str, allowed_patterns:tuple[str, ...]) -> bool:
|
||
|
|
normalized = normalize_policy_path(path)
|
||
|
|
|
||
|
|
for pattern in allowed_patterns:
|
||
|
|
pattern_normalized = normalize_policy_path(pattern)
|
||
|
|
if pattern_normalized == "*":
|
||
|
|
return True
|
||
|
|
if pattern_normalized.endswith("/**"):
|
||
|
|
prefix = pattern_normalized[:-3].rstrip("/")
|
||
|
|
if normalized == prefix or normalized.startswith(prefix + "/"):
|
||
|
|
return True
|
||
|
|
elif pattern_normalized.endswith("/"):
|
||
|
|
if normalized.startswith(pattern_normalized):
|
||
|
|
return True
|
||
|
|
elif normalized == pattern_normalized or normalized.startswith(pattern_normalized.rstrip("/") + "/"):
|
||
|
|
return True
|
||
|
|
|
||
|
|
return False
|