# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 package openshell.sandbox default allow_network = false # --- Static policy data passthrough (queried at sandbox startup) --- filesystem_policy := data.filesystem_policy landlock_policy := data.landlock process_policy := data.process # --- Network access decision (queried per-CONNECT request) --- allow_network if { network_policy_for_request } binary_identity_required if { object.get(object.get(data, "runtime", {}), "require_binary_identity", true) } # --- Deny reasons (specific diagnostics for debugging policy denials) --- deny_reason := "missing input.network" if { not input.network } deny_reason := "missing input.exec" if { input.network not input.exec } deny_reason := reason if { input.network input.exec not network_policy_for_request not endpoint_policy_for_request count(data.network_policies) > 0 reason := sprintf("endpoint %s:%d is not allowed by any policy", [input.network.host, input.network.port]) } deny_reason := reason if { input.network input.exec not network_policy_for_request endpoint_policy_for_request ancestors_str := concat(" -> ", input.exec.ancestors) cmdline_str := concat(", ", input.exec.cmdline_paths) binary_misses := [r | some name policy := data.network_policies[name] endpoint_allowed(policy, input.network) not binary_allowed(policy, input.exec) r := sprintf("binary '%s' not allowed in policy '%s' (ancestors: [%s], cmdline: [%s]). SYMLINK HINT: the binary path is the kernel-resolved target from /proc//exe, not the symlink. If your policy specifies a symlink (e.g., /usr/bin/python3) but the actual binary is /usr/bin/python3.11, either: (1) use the canonical path in your policy (run 'readlink -f /usr/bin/python3' inside the sandbox), or (2) ensure symlink resolution is working (check sandbox logs for 'Cannot access container filesystem')", [input.exec.path, name, ancestors_str, cmdline_str]) ] count(binary_misses) > 0 reason := concat("; ", binary_misses) } deny_reason := "network connections not allowed by policy" if { input.network input.exec not network_policy_for_request count(data.network_policies) == 0 } # --- Matched policy name (for audit logging) --- # # Collects all matching policy names into a set, then deterministically picks # the lexicographically smallest. This avoids a "complete rule conflict" when # multiple policies cover the same endpoint (e.g. after draft approval adds an # overlapping rule). _matching_policy_names contains name if { some name policy := data.network_policies[name] endpoint_allowed(policy, input.network) binary_allowed(policy, input.exec) } matched_network_policy := min(_matching_policy_names) if { count(_matching_policy_names) > 0 } # --- Core matching logic --- # True when at least one network policy matches the request (endpoint + binary). # Expressed as a boolean so that multiple matching policies don't cause a # "complete rule conflict". network_policy_for_request if { some name data.network_policies[name] endpoint_allowed(data.network_policies[name], input.network) binary_allowed(data.network_policies[name], input.exec) } endpoint_policy_for_request if { some name data.network_policies[name] endpoint_allowed(data.network_policies[name], input.network) } # Endpoint matching: exact host (case-insensitive) + port in ports list. endpoint_allowed(policy, network) if { some endpoint endpoint := policy.endpoints[_] not contains(endpoint.host, "*") lower(endpoint.host) == lower(network.host) endpoint.ports[_] == network.port } # Endpoint matching: glob host pattern + port in ports list. # Uses "." as delimiter so "*" matches a single DNS label and "**" matches # across label boundaries — consistent with TLS certificate wildcard semantics. endpoint_allowed(policy, network) if { some endpoint endpoint := policy.endpoints[_] contains(endpoint.host, "*") glob.match(lower(endpoint.host), ["."], lower(network.host)) endpoint.ports[_] == network.port } # Endpoint matching: hostless with allowed_ips — match any host on port. # When an endpoint has allowed_ips but no host, it matches any hostname on the # given port. The actual IP validation happens in Rust post-DNS-resolution. endpoint_allowed(policy, network) if { some endpoint endpoint := policy.endpoints[_] object.get(endpoint, "host", "") == "" count(object.get(endpoint, "allowed_ips", [])) > 0 endpoint.ports[_] == network.port } # Binary matching can be relaxed by trusted runtime configuration. In that # mode, network policies are endpoint/L7 scoped and ignore policy.binaries. binary_allowed(_, _) if { not binary_identity_required } # Binary matching: exact path. # SHA256 integrity is enforced in Rust via trust-on-first-use (TOFU) cache, # not in Rego. The proxy computes and caches binary hashes at runtime. binary_allowed(policy, exec) if { some b b := policy.binaries[_] not contains(b.path, "*") b.path == exec.path } # Binary matching: ancestor exact path (e.g., claude spawns node). binary_allowed(policy, exec) if { some b b := policy.binaries[_] not contains(b.path, "*") ancestor := exec.ancestors[_] b.path == ancestor } # Binary matching: glob pattern against exe path or any ancestor. # NOTE: cmdline_paths are intentionally excluded — argv[0] is trivially # spoofable via execve and must not be used as a grant-access signal. binary_allowed(policy, exec) if { some b in policy.binaries contains(b.path, "*") all_paths := array.concat([exec.path], exec.ancestors) some p in all_paths glob.match(b.path, ["/"], p) } # --- Network action (allow / deny) --- # # These rules are mutually exclusive by construction: # - "allow" requires `network_policy_for_request` (binary+endpoint matched) # - default is "deny" when no policy matches. default network_action := "deny" # Explicitly allowed: endpoint + binary match in a network policy → allow. network_action := "allow" if { network_policy_for_request } # --- Authoritative egress authorization snapshot --- # # Rust evaluates this rule once per admitted connection. Keeping the action, # matched policy, endpoint metadata, and exact-host signal in one result makes # them an atomic view of one policy generation. default _egress_matched_policy := "" _egress_matched_policy := matched_network_policy if { matched_network_policy } default _egress_deny_reason := "" _egress_deny_reason := deny_reason if { network_action == "deny" } default _egress_exact_declared_endpoint_host := false _egress_exact_declared_endpoint_host := true if { exact_declared_endpoint_host } egress_authorization := { "action": network_action, "deny_reason": _egress_deny_reason, "matched_policy": _egress_matched_policy, "endpoint_configs": _matching_endpoint_configs, "matched_endpoints": _matching_endpoint_records, "exact_declared_endpoint_host": _egress_exact_declared_endpoint_host, } # =========================================================================== # L7 request evaluation (queried per-request within a tunnel) # =========================================================================== default allow_request = false # Per-policy helper: endpoints whose selectors and L7 rules permit the request. # Isolating the endpoint iteration inside a function avoids the regorus # "duplicated definition of local variable" error that occurs when the # outer `some name` iterates over multiple policies that share a host:port. _policy_allowed_l7_endpoints(policy) := [ep | some ep ep := policy.endpoints[_] endpoint_matches_l7_request(ep, input.network, input.request) request_allowed_for_endpoint(input.request, ep) ] # Forwarding and credential selection share endpoint admission. A deny rule # on any matching endpoint removes every otherwise admitted endpoint. _admitted_l7_endpoints := {ep | some name policy := data.network_policies[name] binary_allowed(policy, input.exec) eps := _policy_allowed_l7_endpoints(policy) ep := eps[_] not deny_request } allow_request if { count(_admitted_l7_endpoints) > 0 } # Credentials require the admission of their own endpoint. A sibling endpoint's # allow can authorize forwarding, but cannot authorize this endpoint's grant. # Keep every admitting owner so overlapping allows retain union semantics, and # preserve the global deny decision before any caller selects a credential. # Missing owner metadata grants no credential authority. Sorting a set gives # consumers a deterministic array without duplicate owner identifiers. allowed_token_grant_owners := sort({owner | some endpoint in _admitted_l7_endpoints owner := object.get(endpoint, "token_grant_owner", "") is_string(owner) owner != "" }) # --- L7 deny rules --- # # Deny rules are evaluated after allow rules and take precedence. # If a request matches any deny rule on any matching endpoint, it is blocked # even if it would otherwise be allowed. default deny_request = false # Per-policy helper: true when this policy has at least one endpoint matching # the L4 request whose deny_rules also match the specific L7 request. _policy_denies_l7(policy) if { some ep ep := policy.endpoints[_] endpoint_matches_l7_request(ep, input.network, input.request) request_denied_for_endpoint(input.request, ep) } deny_request if { some name policy := data.network_policies[name] endpoint_allowed(policy, input.network) binary_allowed(policy, input.exec) _policy_denies_l7(policy) } # --- L7 deny rule matching: REST method + path + query --- request_denied_for_endpoint(request, endpoint) if { not jsonrpc_family_endpoint(endpoint) some deny_rule deny_rule := endpoint.deny_rules[_] deny_rule.method method_matches(request.method, deny_rule.method) path_matches(request.path, deny_rule.path) deny_query_params_match(request, deny_rule) } # --- L7 deny rule matching: SQL command --- request_denied_for_endpoint(request, endpoint) if { some deny_rule deny_rule := endpoint.deny_rules[_] deny_rule.command command_matches(request.command, deny_rule.command) } # --- L7 deny rule matching: JSON-RPC method --- request_denied_for_endpoint(request, endpoint) if { jsonrpc_family_endpoint(endpoint) request.method == "POST" some deny_rule deny_rule := endpoint.deny_rules[_] deny_rule.method jsonrpc_rule_matches(request, endpoint, deny_rule) } request_denied_for_endpoint(request, endpoint) if { endpoint.protocol == "json-rpc" request.method == "POST" jsonrpc_response_frame_present(request) } # --- L7 deny rule matching: GraphQL operation --- request_denied_for_endpoint(request, endpoint) if { graphql_request_has_operations(request) some deny_rule deny_rule := endpoint.deny_rules[_] deny_rule.operation_type op := request.graphql.operations[_] graphql_deny_rule_matches_operation(op, deny_rule, endpoint) } # A GraphQL endpoint path is authoritative once it matches. If the parsed # GraphQL request is malformed, hash-only without a trusted registry entry, or # contains an operation outside the GraphQL allow rules, a broader REST rule on # the same host:port must not allow it through. request_denied_for_endpoint(request, endpoint) if { endpoint.protocol == "graphql" is_object(request.graphql) not graphql_request_allowed(request, endpoint) } # The same authority applies when a WebSocket endpoint opts into GraphQL # operation policy. Once the relay classifies a client text message as a # GraphQL-over-WebSocket operation, generic WEBSOCKET_TEXT rules must not bypass # operation_type / operation_name / fields policy. request_denied_for_endpoint(request, endpoint) if { endpoint.protocol == "websocket" is_object(request.graphql) not graphql_request_allowed(request, endpoint) } # Deny query matching: fail-closed semantics. # If no query rules on the deny rule, match unconditionally (any query params). # If query rules present, trigger the deny if ANY value for a configured key # matches the matcher. This is the inverse of allow-side semantics where ALL # values must match. For deny logic, a single matching value is enough to block. deny_query_params_match(request, deny_rule) if { deny_query_rules := object.get(deny_rule, "query", {}) count(deny_query_rules) == 0 } deny_query_params_match(request, deny_rule) if { deny_query_rules := object.get(deny_rule, "query", {}) count(deny_query_rules) > 0 not deny_query_key_missing(request, deny_query_rules) not deny_query_value_mismatch_all(request, deny_query_rules) } # A configured deny query key is missing from the request entirely. # Missing key means the deny rule doesn't apply (fail-open on absence). deny_query_key_missing(request, query_rules) if { some key query_rules[key] request_query := object.get(request, "query_params", {}) values := object.get(request_query, key, null) values == null } # ALL values for a configured key fail to match the matcher. # If even one value matches, deny fires. This rule checks the opposite: # true when NO value matches (i.e., every value is a mismatch). deny_query_value_mismatch_all(request, query_rules) if { some key matcher := query_rules[key] request_query := object.get(request, "query_params", {}) values := object.get(request_query, key, []) count(values) > 0 not deny_any_value_matches(values, matcher) } # True if at least one value in the list matches the matcher. deny_any_value_matches(values, matcher) if { some i query_value_matches(values[i], matcher) } # --- L7 deny reason --- request_deny_reason := reason if { input.request graphql_request_error(input.request) reason := sprintf("GraphQL request rejected: %s", [input.request.graphql.error]) } request_deny_reason := reason if { input.request not graphql_request_error(input.request) graphql_request_has_unregistered_persisted_query(input.request, matched_endpoint_config) reason := "GraphQL persisted query is not registered" } request_deny_reason := reason if { input.request deny_request graphql_request_has_operations(input.request) not graphql_request_has_unregistered_persisted_query(input.request, matched_endpoint_config) reason := "GraphQL operation blocked by endpoint policy" } request_deny_reason := reason if { input.request not deny_request not allow_request graphql_request_has_operations(input.request) not graphql_request_has_unregistered_persisted_query(input.request, matched_endpoint_config) reason := "GraphQL operation not permitted by policy" } request_deny_reason := reason if { input.request jsonrpc_response_frame_present(input.request) matched_endpoint_config.protocol == "json-rpc" reason := "JSON-RPC response frames are not permitted from client to server" } # Explain only parsed MCP calls on an endpoint that matches this request path. # The relay evaluates batch members separately. Response frames and protocol # errors keep their own diagnostics, and a sibling endpoint must not select # the explanation merely because it shares the connection's host and port. mcp_policy_request if { input.request.method == "POST" not is_object(object.get(input.request, "graphql", null)) not jsonrpc_response_frame_present(input.request) jsonrpc := object.get(input.request, "jsonrpc", null) is_object(jsonrpc) jsonrpc_no_parse_error(jsonrpc) method := object.get(jsonrpc, "method", "") is_string(method) method != "" object.get(jsonrpc, "mcp_method_classification", "") in {"available", "extension"} endpoint := _matching_endpoint_configs[_] endpoint.protocol == "mcp" endpoint_path_matches_request(endpoint, input.request) } # These reasons use fixed text because method names and tool parameters can # contain caller data. Deny rules take precedence over missing allow rules. request_deny_reason := reason if { mcp_policy_request deny_request reason := "MCP request blocked by a deny rule; ask the policy owner to review deny_rules and tool selectors" } request_deny_reason := reason if { mcp_policy_request not deny_request not allow_request input.request.jsonrpc.mcp_method_classification == "extension" reason := "MCP extension method has no matching exact allow rule; ask the policy owner to review rules with an exact method name and any parameter restrictions; allow_all_known_mcp_methods does not allow extensions" } request_deny_reason := reason if { mcp_policy_request not deny_request not allow_request input.request.jsonrpc.mcp_method_classification == "available" input.request.jsonrpc.method == "tools/call" reason := "MCP tool call has no matching allow rule; ask the policy owner to review rules and tool selectors" } request_deny_reason := reason if { mcp_policy_request not deny_request not allow_request input.request.jsonrpc.mcp_method_classification == "available" input.request.jsonrpc.method != "tools/call" reason := "MCP core method is not permitted by policy; ask the policy owner to review rules for this method in the selected MCP revision" } request_deny_reason := reason if { input.request deny_request not graphql_request_has_operations(input.request) not jsonrpc_response_frame_present(input.request) not mcp_policy_request reason := sprintf("%s %s blocked by deny rule", [input.request.method, input.request.path]) } request_deny_reason := reason if { input.request not deny_request not allow_request not graphql_request_has_operations(input.request) not jsonrpc_response_frame_present(input.request) not mcp_policy_request reason := sprintf("%s %s not permitted by policy", [input.request.method, input.request.path]) } # --- L7 rule matching: REST method + path --- request_allowed_for_endpoint(request, endpoint) if { not jsonrpc_family_endpoint(endpoint) some rule rule := endpoint.rules[_] rule.allow.method method_matches(request.method, rule.allow.method) path_matches(request.path, rule.allow.path) query_params_match(request, rule) } # --- L7 rule matching: SQL command --- request_allowed_for_endpoint(request, endpoint) if { some rule rule := endpoint.rules[_] rule.allow.command command_matches(request.command, rule.allow.command) } # --- L7 rule matching: JSON-RPC method --- request_allowed_for_endpoint(request, endpoint) if { jsonrpc_family_endpoint(endpoint) request.method == "POST" some rule rule := endpoint.rules[_] rule.allow.method not jsonrpc_response_frame_present(request) jsonrpc_rule_matches(request, endpoint, rule.allow) jsonrpc := object.get(request, "jsonrpc", null) method := object.get(jsonrpc, "method", "") rule_method := object.get(rule.allow, "method", "") jsonrpc_allow_rule_classification_allowed(jsonrpc, endpoint, method, rule_method) } # MCP can allow the method layer by endpoint option while still using # tool-specific rules to narrow tools/call params.name. request_allowed_for_endpoint(request, endpoint) if { endpoint.protocol == "mcp" mcp_allow_all_known_mcp_methods(endpoint) request.method == "POST" not jsonrpc_response_frame_present(request) jsonrpc := object.get(request, "jsonrpc", null) is_object(jsonrpc) jsonrpc_no_parse_error(jsonrpc) method := object.get(jsonrpc, "method", "") is_string(method) method != "" object.get(jsonrpc, "mcp_method_classification", "") == "available" not mcp_tool_call_narrowed_by_policy(endpoint, method) } # MCP Streamable HTTP allows client-to-server JSON-RPC response frames for # server-originated requests such as elicitation/create. Generic JSON-RPC keeps # response frames denied because it has no MCP request-correlation semantics. request_allowed_for_endpoint(request, endpoint) if { endpoint.protocol == "mcp" request.method == "POST" jsonrpc_response_frame_present(request) jsonrpc := object.get(request, "jsonrpc", null) is_object(jsonrpc) jsonrpc_no_parse_error(jsonrpc) } jsonrpc_family_endpoint(endpoint) if { endpoint.protocol == "json-rpc" } jsonrpc_family_endpoint(endpoint) if { endpoint.protocol == "mcp" } mcp_allow_all_known_mcp_methods(endpoint) if { object.get(endpoint, "mcp_allow_all_known_mcp_methods", false) } mcp_tool_call_narrowed_by_policy(endpoint, method) if { method == "tools/call" some rule rule := endpoint.rules[_] params := object.get(rule.allow, "params", {}) is_object(params) params.name } # MCP Streamable HTTP uses GET on the JSON-RPC-family endpoint as a receive # stream for server-to-client messages. The stream itself has no # client-to-server JSON-RPC request body to inspect; allow it once the endpoint # path and binary matched. request_allowed_for_endpoint(request, endpoint) if { endpoint.protocol == "mcp" request.method == "GET" jsonrpc := object.get(request, "jsonrpc", null) is_object(jsonrpc) object.get(jsonrpc, "receive_stream", false) jsonrpc_no_parse_error(jsonrpc) object.get(jsonrpc, "method", null) == null not object.get(jsonrpc, "has_response", false) } # --- L7 rule matching: GraphQL operation --- request_allowed_for_endpoint(request, endpoint) if { graphql_request_allowed(request, endpoint) } graphql_request_allowed(request, endpoint) if { graphql_request_has_operations(request) not graphql_request_error(request) not graphql_request_has_unregistered_persisted_query(request, endpoint) not graphql_request_has_unallowed_operation(request, endpoint) } graphql_request_has_operations(request) if { is_object(request.graphql) operations := object.get(request.graphql, "operations", []) count(operations) > 0 } graphql_request_error(request) if { is_object(request.graphql) error := object.get(request.graphql, "error", "") error != "" } graphql_request_has_unallowed_operation(request, endpoint) if { op := request.graphql.operations[_] not graphql_operation_allowed(op, endpoint) } graphql_operation_allowed(op, endpoint) if { rule := endpoint.rules[_] rule.allow.operation_type graphql_allow_rule_matches_operation(op, rule.allow, endpoint) } graphql_request_has_unregistered_persisted_query(request, endpoint) if { op := request.graphql.operations[_] graphql_operation_needs_registry(op) not graphql_registered_operation(op, endpoint) } graphql_operation_needs_registry(op) if { object.get(op, "persisted_query", false) == true object.get(op, "operation_type", "") == "" } graphql_registered_operation(op, endpoint) if { object.get(endpoint, "persisted_queries", "deny") == "allow_registered" id := graphql_operation_registry_key(op) endpoint.graphql_persisted_queries[id] } graphql_operation_registry_key(op) := key if { key := object.get(op, "persisted_query_hash", "") key != "" } graphql_operation_registry_key(op) := key if { object.get(op, "persisted_query_hash", "") == "" key := object.get(op, "persisted_query_id", "") key != "" } graphql_effective_operation(op, endpoint) := registered if { graphql_operation_needs_registry(op) key := graphql_operation_registry_key(op) registered := endpoint.graphql_persisted_queries[key] } graphql_effective_operation(op, _) := op if { not graphql_operation_needs_registry(op) } graphql_allow_rule_matches_operation(op, rule, endpoint) if { effective := graphql_effective_operation(op, endpoint) graphql_operation_type_matches(effective, rule) graphql_operation_name_matches(effective, rule) graphql_allow_fields_match(effective, rule) } graphql_deny_rule_matches_operation(op, rule, endpoint) if { effective := graphql_effective_operation(op, endpoint) graphql_operation_type_matches(effective, rule) graphql_operation_name_matches(effective, rule) graphql_deny_fields_match(effective, rule) } graphql_operation_type_matches(_, rule) if { object.get(rule, "operation_type", "") == "*" } graphql_operation_type_matches(op, rule) if { expected := object.get(rule, "operation_type", "") expected != "" expected != "*" lower(object.get(op, "operation_type", "")) == lower(expected) } graphql_operation_name_matches(_, rule) if { object.get(rule, "operation_name", "") == "" } graphql_operation_name_matches(op, rule) if { pattern := object.get(rule, "operation_name", "") pattern != "" name := object.get(op, "operation_name", "") glob.match(pattern, [], name) } # Allow-side field constraints are intentionally all-selected-fields semantics: # if a rule declares fields, every root field selected by the operation must # match one of the rule patterns. This prevents mixed-operation requests from # allowing an unlisted field because one safe field also appeared. graphql_allow_fields_match(_, rule) if { count(object.get(rule, "fields", [])) == 0 } graphql_allow_fields_match(op, rule) if { count(object.get(rule, "fields", [])) > 0 count(object.get(op, "fields", [])) > 0 not graphql_operation_has_unmatched_field(op, rule) } graphql_operation_has_unmatched_field(op, rule) if { field := object.get(op, "fields", [])[_] not graphql_field_matches_any(field, object.get(rule, "fields", [])) } graphql_deny_fields_match(_, rule) if { count(object.get(rule, "fields", [])) == 0 } graphql_deny_fields_match(op, rule) if { field := object.get(op, "fields", [])[_] graphql_field_matches_any(field, object.get(rule, "fields", [])) } graphql_field_matches_any(field, patterns) if { pattern := patterns[_] glob.match(pattern, [], field) } # Wildcard "*" matches any method; otherwise case-insensitive exact match. # RFC 9110 §9.3.2: HEAD is semantically identical to GET except no response body. method_matches(_, "*") if true method_matches(actual, expected) if { expected != "*" upper(actual) == upper(expected) } method_matches(actual, expected) if { upper(actual) == "HEAD" upper(expected) == "GET" } # Path matching: "**" matches everything; otherwise glob.match with "/" delimiter. # # INVARIANT: `input.request.path` is canonicalized by the sandbox before # policy evaluation — percent-decoded, dot-segments resolved, doubled # slashes collapsed, `;params` stripped, `%2F` rejected (unless an # endpoint opts in). Patterns here must therefore match canonical paths; # do not attempt defensive matching against `..` or `%2e%2e` — those # inputs are rejected at the L7 parser boundary before this rule runs. path_matches(_, "**") if true path_matches(actual, pattern) if { pattern != "**" glob.match(pattern, ["/"], actual) } # Query matching: # - If no query rules are configured, allow any query params. # - For configured keys, all request values for that key must match. # - Matcher shape supports either `glob` or `any`. query_params_match(request, rule) if { query_rules := object.get(rule.allow, "query", {}) not query_mismatch(request, query_rules) } query_mismatch(request, query_rules) if { some key matcher := query_rules[key] not query_key_matches(request, key, matcher) } query_key_matches(request, key, matcher) if { request_query := object.get(request, "query_params", {}) values := object.get(request_query, key, null) values != null count(values) > 0 not query_value_mismatch(values, matcher) } query_value_mismatch(values, matcher) if { some i value := values[i] not query_value_matches(value, matcher) } query_value_matches(value, matcher) if { is_string(matcher) glob.match(matcher, [], value) } query_value_matches(value, matcher) if { is_object(matcher) glob_pattern := object.get(matcher, "glob", "") glob_pattern != "" glob.match(glob_pattern, [], value) } query_value_matches(value, matcher) if { is_object(matcher) any_patterns := object.get(matcher, "any", []) count(any_patterns) > 0 some i glob.match(any_patterns[i], [], value) } # JSON-RPC-family method matching. Generic JSON-RPC policies match only method. # MCP policies may also match params.name from tool aliases. jsonrpc_rule_matches(request, endpoint, rule) if { jsonrpc := object.get(request, "jsonrpc", null) is_object(jsonrpc) method := object.get(jsonrpc, "method", "") is_string(method) method != "" rule_method := object.get(rule, "method", "") is_string(rule_method) rule_method != "" jsonrpc_rule_method_matches(endpoint, method, rule_method) jsonrpc_rule_params_match_for_protocol(jsonrpc, endpoint, rule) } jsonrpc_allow_rule_classification_allowed(_, endpoint, _, _) if { endpoint.protocol == "json-rpc" } jsonrpc_allow_rule_classification_allowed(jsonrpc, endpoint, _, _) if { endpoint.protocol == "mcp" object.get(jsonrpc, "mcp_method_classification", "") == "available" } # Extension methods remain addressable, but only by an exact policy literal. # A wildcard must not silently authorize methods outside the selected core profile. jsonrpc_allow_rule_classification_allowed(jsonrpc, endpoint, method, rule_method) if { endpoint.protocol == "mcp" object.get(jsonrpc, "mcp_method_classification", "") == "extension" rule_method == method } jsonrpc_rule_method_matches(endpoint, _, rule_method) if { endpoint.protocol == "json-rpc" rule_method == "*" } jsonrpc_rule_method_matches(endpoint, method, rule_method) if { endpoint.protocol == "json-rpc" rule_method != "*" rule_method == method } jsonrpc_rule_method_matches(endpoint, method, rule_method) if { endpoint.protocol == "mcp" glob.match(rule_method, [], method) } jsonrpc_rule_params_match_for_protocol(_, endpoint, _) if { endpoint.protocol == "json-rpc" } jsonrpc_rule_params_match_for_protocol(jsonrpc, endpoint, rule) if { endpoint.protocol == "mcp" jsonrpc_params_match(jsonrpc, rule) } jsonrpc_response_frame_present(request) if { jsonrpc := object.get(request, "jsonrpc", null) is_object(jsonrpc) object.get(jsonrpc, "has_response", false) } jsonrpc_no_parse_error(jsonrpc) if { is_object(jsonrpc) object.get(jsonrpc, "error", null) == null } jsonrpc_no_parse_error(jsonrpc) if { is_object(jsonrpc) object.get(jsonrpc, "error", "") == "" } jsonrpc_params_match(jsonrpc, rule) if { is_object(jsonrpc) param_rules := object.get(rule, "params", {}) is_object(param_rules) not jsonrpc_param_mismatch(jsonrpc, param_rules) } jsonrpc_param_mismatch(jsonrpc, param_rules) if { some key matcher := param_rules[key] not jsonrpc_param_key_matches(jsonrpc, key, matcher) } jsonrpc_param_key_matches(jsonrpc, key, matcher) if { is_object(jsonrpc) params := object.get(jsonrpc, "params", {}) is_object(params) value := object.get(params, key, null) value != null is_string(value) query_value_matches(value, matcher) } # SQL command matching: "*" matches any; otherwise case-insensitive. command_matches(_, "*") if true command_matches(actual, expected) if { expected != "*" upper(actual) == upper(expected) } # --- Matched endpoint config (for L7 and allowed_ips extraction) --- # Returns the raw endpoint object for the matched policy + host:port. # Used by Rust to extract L7 config (protocol, tls, enforcement, # allow_encoded_slash) and/or allowed_ips for SSRF allowlist validation. # Per-policy helper: returns matching endpoint configs for a single policy. _policy_endpoint_configs(policy) := [ep | some ep ep := policy.endpoints[_] endpoint_matches_request(ep, input.network) endpoint_has_extended_config(ep) ] _matching_endpoint_configs := [cfg | some pname _matching_policy_names[pname] cfgs := _policy_endpoint_configs(data.network_policies[pname]) cfg := cfgs[_] endpoint_has_extended_config(cfg) ] # Full matched endpoint records are kept separate from the legacy # endpoint-config list, which intentionally contains only connection/L7 # metadata. The policy name and array index identify the endpoint within this # policy generation while the complete endpoint preserves protocol markers # needed by later policy-DNS correlation. _policy_endpoint_records(policy_name, policy) := [record | some endpoint_index, ep in policy.endpoints endpoint_matches_request(ep, input.network) record := { "policy_name": policy_name, "endpoint_index": endpoint_index, "endpoint": ep, } ] _matching_endpoint_records := [record | some pname _matching_policy_names[pname] records := _policy_endpoint_records(pname, data.network_policies[pname]) record := records[_] ] # Endpoints eligible for policy DNS are a policy-data snapshot, not an # authorization decision. In particular, they do not depend on input.exec or # grant access to any process. Every supported endpoint protocol is carried by # TCP, and an omitted protocol is the default L4 TCP form. Endpoints with a # resolvable host plus concrete ports are therefore materialized regardless of # whether later stream handling is L4, HTTP, WebSocket, or another L7 adapter. policy_dns_eligible_endpoint_records := [record | some policy_name, policy in data.network_policies some endpoint_index, ep in policy.endpoints protocol := lower(object.get(ep, "protocol", "tcp")) protocol in {"tcp", "rest", "websocket", "graphql", "sql", "json-rpc", "mcp"} object.get(ep, "host", "") != "" ports := object.get(ep, "ports", []) count(ports) > 0 every port in ports { is_number(port) port >= 1 port <= 65535 } record := { "policy_name": policy_name, "endpoint_index": endpoint_index, "endpoint": ep, } ] matched_endpoint_config := _matching_endpoint_configs[0] if { count(_matching_endpoint_configs) > 0 } # --- Credential provenance view (credential gating only) --- # Deliberately separate from `_matching_endpoint_configs`. The credential guard # must also see L4-only endpoints, which carry no extended config. Widening # `endpoint_has_extended_config` instead would let such an endpoint become # element [0] of the shared list and shadow the TLS mode, SSRF allowlist, and # L7 protocol of an inspected endpoint on the same host:port. _policy_credential_guards(policy) := [ep | some ep ep := policy.endpoints[_] endpoint_matches_request(ep, input.network) ] endpoint_credential_guards := [cfg | some pname _matching_policy_names[pname] cfgs := _policy_credential_guards(data.network_policies[pname]) cfg := cfgs[_] ] # Expose middleware policy data to Rust. Selection and validation stay in Rust; # Rego does not evaluate middleware selectors. network_middlewares := object.get(data, "network_middlewares", {}) _policy_has_exact_declared_endpoint(policy) if { some ep ep := policy.endpoints[_] not object.get(ep, "advisor_proposed", false) not contains(ep.host, "*") lower(ep.host) == lower(input.network.host) ep.ports[_] == input.network.port } exact_declared_endpoint_host if { some pname policy := data.network_policies[pname] binary_allowed(policy, input.exec) _policy_has_exact_declared_endpoint(policy) } # Hosted endpoint: exact host match + port in ports list. endpoint_matches_request(ep, network) if { not contains(ep.host, "*") lower(ep.host) == lower(network.host) ep.ports[_] == network.port } # Hosted endpoint: glob host match + port in ports list. endpoint_matches_request(ep, network) if { contains(ep.host, "*") glob.match(lower(ep.host), ["."], lower(network.host)) ep.ports[_] == network.port } # Hostless endpoint with allowed_ips: match on port only. endpoint_matches_request(ep, network) if { object.get(ep, "host", "") == "" count(object.get(ep, "allowed_ips", [])) > 0 ep.ports[_] == network.port } endpoint_matches_l7_request(ep, network, request) if { endpoint_matches_request(ep, network) endpoint_path_matches_request(ep, request) } endpoint_path_matches_request(ep, request) if { object.get(ep, "path", "") == "" } endpoint_path_matches_request(ep, request) if { path := object.get(ep, "path", "") path != "" # Registered by the embedded engine; use the same Rust matcher as route # selection. Rule paths retain the distinct path_matches semantics above. openshell.endpoint_path_matches(path, request.path) } # An endpoint has extended config if it specifies an L7 protocol, allowed_ips, # or an explicit tls mode (e.g. tls: skip). Explicit protocol "tcp" is the # authored spelling of plain L4 behavior and does not select an L7 config. endpoint_has_extended_config(ep) if { protocol := object.get(ep, "protocol", "") protocol != "" lower(protocol) != "tcp" } endpoint_has_extended_config(ep) if { count(object.get(ep, "allowed_ips", [])) > 0 } endpoint_has_extended_config(ep) if { ep.tls }