Repository navigation
feat(policy): add operator host and hook URL allowlists and run options #852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
27cb854
feat(policy): add operator host and hook URL allowlists and run options
rng1995 a57940d
fix(policy): reject ambiguous allowlist hosts and test the new settings
rng1995 37804ed
fix(policy): reject wildcard URL prefixes and report ignored plugin s…
rng1995 5921a40
fix(policy): check wildcard spelling after host normalization
rng1995 2ff02e4
fix(policy): check host entry characters after fullwidth folding
rng1995 560ceaa
Merge branch 'main' into naren/policy-allowlists-run-options
rng1995 fb29571
feat(tier1): add a runner for the bounded static checks (#853)
rng1995 eef20d6
feat(tier3): add a workflow for prepared runtime packages (#854)
rng1995 d48827b
Merge main into naren/policy-allowlists-run-options
rng1995 28bb908
fix(policy): reject non-ASCII brackets in allowlist entries
rng1995 ea31b21
test(tier3): allow CPU variance in masking linearity checks
rng1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Bounded operator allowlists shared by component checks and runtime proof.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import ipaddress | ||
| import unicodedata | ||
| from urllib.parse import urlsplit | ||
|
|
||
|
|
||
| def normalize_plugin_allowlist(value: object, key: str, max_chars: int) -> tuple[str, ...]: | ||
| """Validate entries without DNS and preserve the existing host matcher rules.""" | ||
| if value is None: | ||
| return () | ||
| if not isinstance(value, (list, tuple)) or len(value) > 64: | ||
| raise ValueError(f"{key} must be a list of at most 64 strings") | ||
| if not value: | ||
| return () | ||
| # Default policies stay cheap for ordinary skills and base installations. | ||
| from skillevaluator.plugin_component_risk import hook_url_entry_problem | ||
| from skillevaluator.validators.mcp_static import classify_endpoint_host | ||
| from skillevaluator.validators.url_policy import url_ambiguities | ||
|
|
||
| normalized = [] | ||
| for raw in value: | ||
| if not isinstance(raw, str) or not raw.strip() or len(raw) > max_chars: | ||
| raise ValueError(f"{key} entries must be nonempty strings of at most {max_chars} characters") | ||
| if any(unicodedata.category(char) in {"Cc", "Cf", "Cs"} for char in raw): | ||
| raise ValueError(f"{key} entries cannot contain control or invisible characters") | ||
| entry = raw.strip() | ||
| if "://" in entry: | ||
| if ( | ||
| key != "hooks.allowed_urls" | ||
| or hook_url_entry_problem(entry) is not None | ||
| or url_ambiguities(entry, percent_in_host=True) | ||
| ): | ||
| raise ValueError(f"{key} contains an invalid URL prefix") | ||
| parsed = urlsplit(entry) | ||
| if parsed.scheme.lower() not in {"http", "https"}: | ||
| raise ValueError(f"{key} URL prefixes must use HTTP or HTTPS") | ||
| host = parsed.hostname | ||
| if host: | ||
| _check_host_pattern(host, key) | ||
| if "*" in host: | ||
| raise ValueError(f"{key} URL prefix host must be exact. Use a bare wildcard host pattern instead.") | ||
|
rng1995 marked this conversation as resolved.
Resolved
|
||
| else: | ||
| # The host matcher percent-decodes an entry, so '%' could spell any rejected character. | ||
| # It also folds fullwidth forms to ASCII, so check the folded spelling as well. | ||
| folded = unicodedata.normalize("NFKC", entry) | ||
| if any(char.isspace() for char in folded) or any(char in folded for char in "%@?#\\"): | ||
| raise ValueError(f"{key} contains an invalid host pattern") | ||
| if "/" in folded or ":" in folded: | ||
|
rng1995 marked this conversation as resolved.
Resolved
|
||
| try: | ||
| ipaddress.ip_network( | ||
| folded[1:-1] if folded.startswith("[") and folded.endswith("]") else folded, strict=False | ||
| ) | ||
| except ValueError as exc: | ||
| raise ValueError(f"{key} contains an invalid IP address or network") from exc | ||
| _check_host_pattern(entry, key) | ||
| host = entry | ||
| endpoint = classify_endpoint_host(host) if host else None | ||
| if endpoint is not None and endpoint.kind == "metadata": | ||
| raise ValueError(f"{key} cannot allow cloud metadata endpoints") | ||
| normalized.append(entry) | ||
| return tuple(dict.fromkeys(normalized)) | ||
|
rng1995 marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def _check_host_pattern(host: str, key: str) -> None: | ||
| """Reject a host that the matcher would keep as a name no real host can have.""" | ||
| from skillevaluator.validators.mcp_static import HostAllowlist | ||
|
|
||
| if "*" in host and (not host.startswith("*.") or "*" in host[2:]): | ||
| raise ValueError(f"{key} contains an invalid host wildcard") | ||
| hosts = HostAllowlist.from_entries((host,)) | ||
| if not (hosts.names or hosts.suffixes or hosts.networks): | ||
| raise ValueError(f"{key} contains an invalid host pattern") | ||
| if any(name.startswith(".") for name in hosts.names): | ||
| raise ValueError(f"{key} contains a host that starts with '.'; use the '*.suffix' form to allow subdomains") | ||
| names = (*hosts.names, *hosts.suffixes) | ||
| # Host patterns already had their wildcard removed by the matcher. A | ||
| # remaining asterisk is a literal introduced by normalization. | ||
| if any("*" in name for name in names): | ||
| raise ValueError(f"{key} contains an invalid host wildcard") | ||
| if any(".." in name for name in names): | ||
| raise ValueError(f"{key} contains a host with an empty label") | ||
| if not hosts.networks and any( | ||
| label.startswith("-") or label.endswith("-") for name in names for label in name.split(".") | ||
| ): | ||
| raise ValueError(f"{key} contains an invalid host label. Hyphens must be inside a label.") | ||
|
|
||
|
|
||
| def plugin_policy_block(data: dict, name: str, known: set[str]) -> dict: | ||
| """Reject misspelled controls instead of silently widening operator intent.""" | ||
| block = data.get(name) | ||
| if block is None: | ||
| return {} | ||
| if not isinstance(block, dict) or not all(isinstance(key, str) for key in block) or set(block) - known: | ||
| raise ValueError(f"{name} policy must be a mapping with only {', '.join(sorted(known))}") | ||
| return block | ||
|
|
||
|
|
||
| def overlay_sets(data: dict, block: str, key: str) -> bool: | ||
| value = data.get(block) | ||
| return isinstance(value, dict) and key in value | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.