Skip to content

feat(policy): match GraphQL arguments and nested fields - #5996

Draft
stevenlee-oai wants to merge 1 commit into
ai-openshell:mainfrom
stevenlee-oai:feat/5986-graphql-field-rules/stevenlee-oai
Draft

stevenlee-oai wants to merge 1 commit into
ai-openshell:mainfrom
stevenlee-oai:feat/5986-graphql-field-rules/stevenlee-oai

Conversation

@stevenlee-oai

@stevenlee-oai stevenlee-oai commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add field_rules for GraphQL over HTTP. Policies can require exact string arguments and restrict nested schema fields. This lets an operator distinguish repository queries and mutations that share one HTTP endpoint.

Related Issue

Closes #5986

Changes

  • Resolve inline arguments, variables, and defaults. Expand fragments and check each selected field occurrence, using the schema name even when a request uses an alias.
  • Require each operation in a batch to pass before forwarding. Fine-grained rules refuse requests when required selections or arguments cannot be established, including persisted operations.
  • Reject scoped policies on Gateways and Supervisors that cannot enforce them. Policies can still use fields for root-field matching.

Testing

  • Focused parser, schema, and Regorus checks passed. They cover correlated occurrences, nested argument matching, missing facts, batch limits, and deny behavior.

  • Formatting, protobuf descriptor inventories, and docs navigation passed.

  • The conformance test compiled with its checked-in lockfile. It verifies that the upstream recorder sees credentials only on the admitted queries and mutation. The sandbox conformance run remains pending.

  • The local registry does not provide the workspace's locked serde_with 3.25.0. The Gateway, Supervisor, CLI, and policy test targets passed type checking with 3.24.0 in a disposable checkout. The PR retains the checked-in lockfile.

  • Checks appropriate to the affected code and behavior pass

  • Unit tests added or updated

  • Conformance test added

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Policy schema documentation updated

Signed-off-by: Steven Lee <213687449+stevenlee-oai@users.noreply.github.com>
@github-actions github-actions Bot added the state:accepted A maintainer decided OpenShell should pursue this issue label Nov 26, 2026

@mrunalp mrunalp left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. The matching model looks right to me and is consistent with the existing L7 rules:

  • Allow semantics extend fields naturally (every selected occurrence must be covered by one allow rule), and deny semantics match today's "any match denies".
  • Arguments are correlated per occurrence, so aliased calls can't borrow each other's arguments.
  • Ambiguous or unresolvable input fails closed (duplicate keys/fragments/operations, cycles, depth and size budgets, persisted markers, missing variables), and deny treats unresolved arguments as potentially matching.
  • The supervisor guard via supported_capabilities mirrors POLICY_CEDAR_V1, and the Cedar token-grant translation drops/widens rather than granting.

Main design concern: the downgrade guard in NetworkEnforcementMode

I don't think enforcement mode is the right place to signal feature support:

  • It conflates two orthogonal things: what to do on a violation (deny vs log) and which selectors the endpoint uses. The next feature that needs a guard would need another pair of values.
  • GRAPHQL_FIELD_RULES_AUDIT protects nothing: audit allows the traffic anyway, so an older gateway dropping field_rules only changes logging. The widening risk exists only under enforce.
  • YAML and wire forms diverge. YAML authors write enforce, while direct protobuf/SDK clients must remember to set the marker, and reads return values older SDKs don't recognize.
  • The equivalence leaks into merge (effective_enforcement, merge_endpoint), ambiguity.rs, normalize_endpoint, and a marker⇔field_rules bijection check.
  • It relies on validate_endpoint_mode_values incidentally rejecting unknown values, and it attaches at the endpoint while the feature is per rule.

Suggested shape for this PR:

  • Leave NetworkEnforcementMode unchanged and drop the marker validation and the merge/ambiguity/normalize special cases.
  • Keep the supervisor check through GetSandboxConfigRequest.supported_capabilities as is. That covers the common upgrade order (gateway first, older supervisors still running).
  • Add a note in schema.mdx that field_rules require a gateway and supervisor at or above the release that introduces them, because an older gateway ignores them and the allow rule becomes broader.

That leaves a newer CLI or SDK talking to an older gateway unprotected, which is the same exposure accepted when MCP params and GraphQL fields were added. How gateways should advertise policy features to clients, the supported CLI/gateway/supervisor version skew, and whether gateways should reject unknown policy fields are general API questions. I'd rather settle them in a separate issue than introduce a new mechanism here; I'll open one.

Correctness and usability

  • "variables": null yields no field facts, so every field_rules policy denies it. Many clients send null for no variables (inline comment).
  • "extensions": null counts as a persisted-query marker, which also withholds facts and denies. This is a new false denial compared with the existing hash detection (inline comment).
  • Inline enum literals resolve to Null while the same value passed as a variable is a string, so states: [OPEN] and {"states": ["OPEN"]} behave differently. Treating an inline enum as its name string is safe because the server applies the same coercion, and it would satisfy the "inline and equivalent variables" acceptance criterion.
  • Type conditions are dropped, so ... on Repository { name } and ... on User { name } both become node.name / search.nodes.name. Fine to defer, but please document it alongside the existing isolation caveat.
  • __typename must be listed at every path. Apollo/Relay-style clients inject it; consider allowing it implicitly (keeping __schema/__type explicit).
  • Only string arguments can match (first: 100 never does). Worth a sentence in the docs.

Smaller points

  • Consider reusing L7QueryMatcher (adding optional string exact) instead of a third matcher type, so REST query, MCP params and GraphQL arguments share one shape, with glob/any rejected for GraphQL for now. If the separate type is deliberate, a sentence explaining why would help.
  • The sandbox conformance run is still listed as pending.

Comment thread proto/sandbox.proto
NETWORK_ENFORCEMENT_MODE_AUDIT = 2;
// Same behavior as ENFORCE, but required when field_rules is present. An
// older gateway rejects this unknown enum instead of dropping field_rules.
NETWORK_ENFORCEMENT_MODE_GRAPHQL_FIELD_RULES_ENFORCE = 3;

@mrunalp mrunalp Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the review summary: I'd keep NetworkEnforcementMode limited to deny/log behaviour and drop these variants. The supervisor supported_capabilities check already guards enforcement, and client-to-gateway feature signalling is better designed separately, together with a version skew policy. The _AUDIT variant in particular guards nothing, because audit allows the traffic regardless.

let empty_variables = Map::new();
let variables = match obj.get("variables") {
None => Some(&empty_variables),
Some(value) => value.as_object(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"variables": null lands here as None, so no field occurrences are produced and every field_rules policy denies the request. Could null be treated like an omitted variables (an empty map)?

fn has_persisted_marker(extensions: &Value) -> bool {
extensions
.as_object()
.is_none_or(|extensions| extensions.contains_key("persistedQuery"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_none_or makes "extensions": null (or any non-object) count as a persisted-query marker, which withholds field facts and denies. null should probably be treated as absent; a non-null non-object could still fail closed.

}
// Enum literals are distinct from strings. The string matcher treats
// both enum and null literals as non-string values.
cst::Value::EnumValue(_) | cst::Value::NullValue(_) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapping inline enum literals to Null means states: [OPEN] never matches, while the same value supplied through a variable is the string "OPEN" and does. Resolving an enum literal to its name string seems safe, since the server coerces both forms identically, and it would make inline and variable forms equivalent as the issue's acceptance criteria ask.

self.collect(children, &path, depth + 1)?;
}
}
cst::Selection::InlineFragment(fragment) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type condition is dropped here, so selections on polymorphic fields (node, nodes, search.nodes) merge across types. Not necessarily for this PR, but worth documenting next to the repository-isolation caveat in schema.mdx.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state:accepted A maintainer decided OpenShell should pursue this issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support GraphQL argument matching and nested field restrictions in network policies

2 participants