This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: k8s-gateway-api-v1-2-referencegrant-boundary

Enforce Gateway API v1.2 Explicit ReferenceGrant Isolation for Cross-Namespace Routing

Enforces strict tenant boundary isolation across Kubernetes namespaces by requiring explicit ReferenceGrant resources for all cross-namespace route-to-service and gateway-to-route attachments. Prevents unauthorized lateral traffic escalation and cross-tenant route hijacking by blocking wildcard selectors and unvalidated backend references in Gateway API v1.2 resources.

Prohibited Anti-Pattern: Implicit or Wildcard Cross-Namespace Route Attachment Without Target ReferenceGrant

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Enforce Gateway API v1.2 Explicit ReferenceGrant Isolation for Cross-Namespace Routing - Enforces strict tenant boundary isolation across Kubernetes namespaces by requiring explicit ReferenceGrant resources for all cross-namespace route-to-service and gateway-to-route attachments. Prevents unauthorized lateral traffic escalation and cross-tenant route hijacking by blocking wildcard selectors and unvalidated backend references in Gateway API v1.2 resources.
globs: deploy/**/*.{yaml,yml}, k8s/**/*.{yaml,yml}, infra/**/*.{yaml,yml}, helm/**/*.{yaml,yml}, **/*gateway*.{yaml,yml}, **/*route*.{yaml,yml}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-v1-2-referencegrant-boundary"
---

# Enforce Gateway API v1.2 Explicit ReferenceGrant Isolation for Cross-Namespace Routing

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Implicit or Wildcard Cross-Namespace Route Attachment Without Target ReferenceGrant

## System Constraints
You are a Principal Cloud Native Architect enforcing Kubernetes Gateway API v1.2 specification compliance and zero-trust cross-namespace isolation. Cross-namespace references (such as HTTPRoutes referencing Services in external namespaces, or Gateways attaching routes across namespaces) are hard security boundaries that MUST be explicitly allowed by a ReferenceGrant defined in the target resource's namespace. Never generate cross-namespace backendRefs or Gateway listener attachments without the corresponding, least-privilege ReferenceGrant in the provider namespace.

## Strict Guidelines
- Engineers MUST ALWAYS define a ReferenceGrant (`gateway.networking.k8s.io/v1beta1`) in the target/destination namespace whenever an HTTPRoute references a Service across namespaces.
- Engineers MUST NOT use wildcard values (`*`) in `ReferenceGrant.spec.from.namespace` or `ReferenceGrant.spec.to.name` within multi-tenant production clusters.
- Engineers MUST ALWAYS configure Gateway `listeners[].allowedRoutes.namespaces.from: Selector` with strict label matching instead of `from: All` when exposing gateway listeners to external namespaces.
- Services MUST remain namespace-isolated by default; cross-tenant backends MUST declare explicit `to[].kind: Service` and `to[].name` in the target namespace ReferenceGrant.
- HTTPRoute objects MUST NEVER assume backend resolution across namespace boundaries without asserting `ResolvedRefs: True` status conditions through deterministic reconciliation.

## Bad vs Good Implementations
### Cross-Namespace BackendRef via ReferenceGrant Boundary Enforcement

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Kubernetes Gateway API v1.2, routing across namespaces without an explicit ReferenceGrant located in the target namespace (tenant-b) triggers a ResolvedRefs=False condition, leaving the route unattached or enabling unauthorized traffic injection if arbitrary namespaces can reference tenant-b services.
```yaml
# INSECURE: HTTPRoute references backend in tenant-b namespace without ReferenceGrant in tenant-b
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: tenant-a-api-route
  namespace: tenant-a
spec:
  parentRefs:
    - name: shared-gateway
      namespace: ingress-infra
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /shared-api
      backendRefs:
        - name: payment-service
          namespace: tenant-b # Cross-namespace reference will be rejected or hijacked
          port: 8080
```

#### Production Standard (Correct)
> **Standard**: The target namespace (tenant-b) declares an explicit ReferenceGrant permitting ONLY HTTPRoutes originating from the specific source namespace (tenant-a) targeting the exact named Service (payment-service), satisfying Gateway API v1.2 boundary requirements.
```yaml
# SECURE: Explicit ReferenceGrant deployed in tenant-b permitting only tenant-a HTTPRoute
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-tenant-a-payment-read
  namespace: tenant-b
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: tenant-a
  to:
    - group: ""
      kind: Service
      name: payment-service
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: tenant-a-api-route
  namespace: tenant-a
spec:
  parentRefs:
    - name: shared-gateway
      namespace: ingress-infra
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /shared-api
      backendRefs:
        - name: payment-service
          namespace: tenant-b
          port: 8080
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Enforce Gateway API v1.2 Explicit ReferenceGrant Isolation for Cross-Namespace Routing

Enforce Gateway API v1.2 Explicit ReferenceGrant Isolation for Cross-Namespace Routing defines an industrial-grade behavioral standard for devops engineering. Enforces strict tenant boundary isolation across Kubernetes namespaces by requiring explicit ReferenceGrant resources for all cross-namespace route-to-service and gateway-to-route attachments. Prevents unauthorized lateral traffic escalation and cross-tenant route hijacking by blocking wildcard selectors and unvalidated backend references in Gateway API v1.2 resources. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Implicit or Wildcard Cross-Namespace Route Attachment Without Target ReferenceGrant Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftImplicit or Wildcard Cross-Namespace Route Attachment Without Target ReferenceGrantCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS define a ReferenceGrant (`gateway.networking.k8s.io/v1beta1`) in the target/destination namespace whenever an HTTPRoute references a Service across namespaces.

Engineers MUST ALWAYS define a ReferenceGrant (`gateway.networking.k8s.io/v1beta1`) in the target/destination namespace whenever an HTTPRoute references a Service across namespaces.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT use wildcard values (`*`) in `ReferenceGrant.spec.from.namespace` or `ReferenceGrant.spec.to.name` within multi-tenant production clusters.

Engineers MUST NOT use wildcard values (`*`) in `ReferenceGrant.spec.from.namespace` or `ReferenceGrant.spec.to.name` within multi-tenant production clusters.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST ALWAYS configure Gateway `listeners[].allowedRoutes.namespaces.from

Engineers MUST ALWAYS configure Gateway `listeners[].allowedRoutes.namespaces.from: Selector` with strict label matching instead of `from: All` when exposing gateway listeners to external namespaces.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST remain namespace-isolated by default

Services MUST remain namespace-isolated by default; cross-tenant backends MUST declare explicit `to[].kind: Service` and `to[].name` in the target namespace ReferenceGrant.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

HTTPRoute objects MUST NEVER assume backend resolution across namespace boundaries without asserting `ResolvedRefs

HTTPRoute objects MUST NEVER assume backend resolution across namespace boundaries without asserting `ResolvedRefs: True` status conditions through deterministic reconciliation.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Enforce Gateway API v1.2 Explicit ReferenceGrant Isolation for Cross-Namespace Routing critical for AI coding assistants?

Enforces strict tenant boundary isolation across Kubernetes namespaces by requiring explicit ReferenceGrant resources for all cross-namespace route-to-service and gateway-to-route attachments. Prevents unauthorized lateral traffic escalation and cross-tenant route hijacking by blocking wildcard selectors and unvalidated backend references in Gateway API v1.2 resources. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Implicit or Wildcard Cross-Namespace Route Attachment Without Target ReferenceGrant) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Enforce