|
| 1 | +""" |
| 2 | +Resource comparison functions for post-execution verification. |
| 3 | +
|
| 4 | +These functions compare authorized resources against actual resources, |
| 5 | +handling path normalization and glob pattern matching. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import re |
| 11 | +from fnmatch import fnmatch |
| 12 | + |
| 13 | +from predicate_contracts import normalize_path |
| 14 | + |
| 15 | + |
| 16 | +def normalize_resource(resource: str) -> str: |
| 17 | + """ |
| 18 | + Normalize a resource path for comparison. |
| 19 | +
|
| 20 | + Applies the following transformations: |
| 21 | + - Expands ~ to home directory |
| 22 | + - Collapses multiple slashes |
| 23 | + - Removes ./ segments |
| 24 | + - Removes trailing slashes |
| 25 | + - Resolves . and .. |
| 26 | +
|
| 27 | + Args: |
| 28 | + resource: Resource path to normalize |
| 29 | +
|
| 30 | + Returns: |
| 31 | + Normalized path |
| 32 | + """ |
| 33 | + # Use existing normalize_path for filesystem paths |
| 34 | + if resource.startswith("/") or resource.startswith("~") or resource.startswith("."): |
| 35 | + normalized = normalize_path(resource) |
| 36 | + # normalize_path doesn't strip trailing slashes, so we do it here |
| 37 | + if len(normalized) > 1 and normalized.endswith("/"): |
| 38 | + normalized = normalized[:-1] |
| 39 | + return normalized |
| 40 | + |
| 41 | + # For URLs, handle protocol specially |
| 42 | + url_match = re.match(r"^([a-zA-Z][a-zA-Z0-9+.-]*://)", resource) |
| 43 | + if url_match: |
| 44 | + protocol = url_match.group(1) # e.g., "https://" |
| 45 | + rest = resource[len(protocol) :] |
| 46 | + |
| 47 | + # Normalize the rest (collapse slashes, remove ./, remove trailing /) |
| 48 | + normalized = re.sub(r"/+", "/", rest) # Collapse multiple slashes |
| 49 | + normalized = re.sub(r"/\./", "/", normalized) # Remove ./ |
| 50 | + normalized = re.sub(r"/$", "", normalized) # Remove trailing slash |
| 51 | + |
| 52 | + return protocol + normalized |
| 53 | + |
| 54 | + # For other non-path resources, do basic cleanup |
| 55 | + normalized = re.sub(r"/+", "/", resource) # Collapse multiple slashes |
| 56 | + normalized = re.sub(r"/\./", "/", normalized) # Remove ./ |
| 57 | + normalized = re.sub(r"/$", "", normalized) # Remove trailing slash |
| 58 | + return normalized |
| 59 | + |
| 60 | + |
| 61 | +def resources_match( |
| 62 | + authorized: str, |
| 63 | + actual: str, |
| 64 | + *, |
| 65 | + allow_glob: bool = True, |
| 66 | +) -> bool: |
| 67 | + """ |
| 68 | + Check if an actual resource matches an authorized resource. |
| 69 | +
|
| 70 | + Handles: |
| 71 | + - Path normalization (~ expansion, . and .., etc.) |
| 72 | + - Optional glob pattern matching (* wildcards) |
| 73 | +
|
| 74 | + Args: |
| 75 | + authorized: Resource from the mandate (may contain glob patterns) |
| 76 | + actual: Resource that was actually accessed |
| 77 | + allow_glob: Enable glob pattern matching for authorized resource |
| 78 | +
|
| 79 | + Returns: |
| 80 | + True if resources match |
| 81 | + """ |
| 82 | + # Normalize both resources |
| 83 | + normalized_auth = normalize_resource(authorized) |
| 84 | + normalized_actual = normalize_resource(actual) |
| 85 | + |
| 86 | + # Exact match after normalization |
| 87 | + if normalized_auth == normalized_actual: |
| 88 | + return True |
| 89 | + |
| 90 | + # Glob pattern match (if enabled and authorized resource contains wildcards) |
| 91 | + if allow_glob and "*" in authorized: |
| 92 | + return fnmatch(normalized_actual, authorized) |
| 93 | + |
| 94 | + return False |
| 95 | + |
| 96 | + |
| 97 | +def actions_match(authorized: str, actual: str) -> bool: |
| 98 | + """ |
| 99 | + Check if an actual action matches an authorized action. |
| 100 | +
|
| 101 | + Actions are compared case-sensitively after trimming whitespace. |
| 102 | + Supports glob patterns in the authorized action. |
| 103 | +
|
| 104 | + Args: |
| 105 | + authorized: Action from the mandate (may contain glob patterns) |
| 106 | + actual: Action that was actually performed |
| 107 | +
|
| 108 | + Returns: |
| 109 | + True if actions match |
| 110 | + """ |
| 111 | + normalized_auth = authorized.strip() |
| 112 | + normalized_actual = actual.strip() |
| 113 | + |
| 114 | + # Exact match |
| 115 | + if normalized_auth == normalized_actual: |
| 116 | + return True |
| 117 | + |
| 118 | + # Glob pattern match (e.g., "fs.*" matches "fs.read") |
| 119 | + if "*" in authorized: |
| 120 | + return fnmatch(normalized_actual, authorized) |
| 121 | + |
| 122 | + return False |
0 commit comments