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-cross-namespace-delegation-invariants

Kubernetes Gateway API v1.2 Cross-Namespace Route Delegation and ReferenceGrant Guardrails

Enforces strict organizational boundary isolation, deterministic route attachment, and explicit ReferenceGrant declarations for Gateway API v1.2 in shared multi-tenant enterprise clusters. Prevents unauthorized cross-namespace route hijacking, unintended hostname collisions, and rogue backend referencing by mandating bidirectional consent between infrastructure owners and application development teams. Requires precise `allowedRoutes` scoping on Gateway listeners and explicit `ReferenceGrant` objects for cross-namespace service forwarding.

Prohibited Anti-Pattern: Permissive Gateway AllowedRoutes and Unguarded Cross-Namespace Service Referencing Allowing Route Hijacking

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kubernetes Gateway API v1.2 Cross-Namespace Route Delegation and ReferenceGrant Guardrails - Enforces strict organizational boundary isolation, deterministic route attachment, and explicit ReferenceGrant declarations for Gateway API v1.2 in shared multi-tenant enterprise clusters. Prevents unauthorized cross-namespace route hijacking, unintended hostname collisions, and rogue backend referencing by mandating bidirectional consent between infrastructure owners and application development teams. Requires precise `allowedRoutes` scoping on Gateway listeners and explicit `ReferenceGrant` objects for cross-namespace service forwarding.
globs: **/*.{yaml,yml}, k8s/**/*.{yaml,yml}, deploy/**/*.{yaml,yml}, helm/**/*.{yaml,yml}, infra/**/*.{yaml,yml}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-v1-2-cross-namespace-delegation-invariants"
---

# Kubernetes Gateway API v1.2 Cross-Namespace Route Delegation and ReferenceGrant Guardrails

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Permissive Gateway AllowedRoutes and Unguarded Cross-Namespace Service Referencing Allowing Route Hijacking

## System Constraints
You are a Principal Platform Engineer and Kubernetes Security Architect enforcing Kubernetes Gateway API v1.2 specifications. Mandate bidirectional cross-namespace delegation invariants: Gateways MUST NOT use wildcard `allowedRoutes.namespaces.from: All` without strict selector constraints; cross-namespace service references in HTTPRoutes MUST be explicitly authorized by a matching `ReferenceGrant` in the target namespace; and hostnames MUST NOT overlap across disjoint administrative domains.

## Strict Guidelines
- Engineers MUST ALWAYS enforce bidirectional attachment consent: a Gateway must specify permitted namespaces via `allowedRoutes.namespaces.from: Selector` (or `Same`), and the target namespace MUST permit cross-namespace backend references via an explicit `ReferenceGrant` resource.
- Engineers MUST NOT configure `allowedRoutes.namespaces.from: All` on Gateway listeners in multi-tenant clusters, as this exposes the gateway to host-collision attacks and unauthorized route attachment.
- Engineers MUST NOT bind HTTPRoutes across namespaces without an accompanying `ReferenceGrant` in the target backend Service namespace, conforming strictly to the Gateway API v1.2 ReferenceGrant specification (`gateway.networking.k8s.io/v1beta1`).
- Route definitions MUST define deterministic, fully-qualified domain names (`hostnames`) rather than unbounded wildcards when attaching across namespace boundaries.
- Platform operators MUST configure Gateway listeners with distinct port/protocol/hostname tuples and explicit `allowedRoutes.kinds` filters to restrict which route types (e.g., HTTPRoute, GRPCRoute, TLSRoute) may attach.

## Bad vs Good Implementations
### Cross-Namespace Route Attachment and Backend Reference Security

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The Gateway listener allows any tenant (`from: All`) to claim high-privilege hostnames like `pay.enterprise.internal`. Furthermore, the HTTPRoute attempts to route directly to a backend service in `secure-payments` without a `ReferenceGrant`, risking unverified cross-tenant traffic leakage and delegation poisoning.
```yaml
# VULNERABLE: Permissive namespace attachment and cross-namespace backend hijacking
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-gateway
  namespace: infra-gateway
spec:
  gatewayClassName: enterprise-ingress
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      allowedRoutes:
        namespaces:
          from: All # CRITICAL: Allows ANY namespace to bind routes and claim hostnames
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: hijacked-payment-route
  namespace: untrusted-tenant
spec:
  parentRefs:
    - name: shared-gateway
      namespace: infra-gateway
  hostnames:
    - "pay.enterprise.internal" # Hijacks corporate payment domain
  rules:
    - backendRefs:
        - name: payment-backend
          namespace: secure-payments # Fails/hijacks cross-namespace backend without ReferenceGrant
          port: 8443
```

#### Production Standard (Correct)
> **Standard**: The Gateway establishes explicit boundary control via `allowedRoutes.namespaces.from: Selector` matching only authorized tenant namespaces and strictly bounding the hostname. The backend namespace `secure-payments` explicitly authorises the tenant HTTPRoute using a standard `ReferenceGrant` resource.
```yaml
# SECURE: Controlled route delegation with label selectors and explicit ReferenceGrant
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-gateway
  namespace: infra-gateway
spec:
  gatewayClassName: enterprise-ingress
  listeners:
    - name: https-payments
      protocol: HTTPS
      port: 443
      hostname: "pay.enterprise.internal"
      allowedRoutes:
        namespaces:
          from: Selector
          selector:
            matchLabels:
              gateway-access: "payments-domain"
        kinds:
          - group: gateway.networking.k8s.io
            kind: HTTPRoute
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-infra-gateway-routing
  namespace: secure-payments
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: tenant-payments
  to:
    - group: ""
      kind: Service
      name: payment-backend
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: payment-route
  namespace: tenant-payments
spec:
  parentRefs:
    - name: shared-gateway
      namespace: infra-gateway
      sectionName: https-payments
  hostnames:
    - "pay.enterprise.internal"
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /v1
      backendRefs:
        - name: payment-backend
          namespace: secure-payments
          port: 8443
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Kubernetes Gateway API v1.2 Cross-Namespace Route Delegation and ReferenceGrant Guardrails

Kubernetes Gateway API v1.2 Cross-Namespace Route Delegation and ReferenceGrant Guardrails defines an industrial-grade behavioral standard for devops engineering. Enforces strict organizational boundary isolation, deterministic route attachment, and explicit ReferenceGrant declarations for Gateway API v1.2 in shared multi-tenant enterprise clusters. Prevents unauthorized cross-namespace route hijacking, unintended hostname collisions, and rogue backend referencing by mandating bidirectional consent between infrastructure owners and application development teams. Requires precise `allowedRoutes` scoping on Gateway listeners and explicit `ReferenceGrant` objects for cross-namespace service forwarding. 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: Permissive Gateway AllowedRoutes and Unguarded Cross-Namespace Service Referencing Allowing Route Hijacking 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 DriftPermissive Gateway AllowedRoutes and Unguarded Cross-Namespace Service Referencing Allowing Route HijackingCritical
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 enforce bidirectional attachment consent

Engineers MUST ALWAYS enforce bidirectional attachment consent: a Gateway must specify permitted namespaces via `allowedRoutes.namespaces.from: Selector` (or `Same`), and the target namespace MUST permit cross-namespace backend references via an explicit `ReferenceGrant` resource.

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 configure `allowedRoutes.namespaces.from

Engineers MUST NOT configure `allowedRoutes.namespaces.from: All` on Gateway listeners in multi-tenant clusters, as this exposes the gateway to host-collision attacks and unauthorized route attachment.

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 NOT bind HTTPRoutes across namespaces without an accompanying `ReferenceGrant` in the target backend Service namespace, conforming strictly to the Gateway API v1.2 ReferenceGrant specification (`gateway.networking.k8s.io/v1beta1`).

Engineers MUST NOT bind HTTPRoutes across namespaces without an accompanying `ReferenceGrant` in the target backend Service namespace, conforming strictly to the Gateway API v1.2 ReferenceGrant specification (`gateway.networking.k8s.io/v1beta1`).

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

Route definitions MUST define deterministic, fully-qualified domain names (`hostnames`) rather than unbounded wildcards when attaching across namespace boundaries.

Route definitions MUST define deterministic, fully-qualified domain names (`hostnames`) rather than unbounded wildcards when attaching across namespace boundaries.

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

Platform operators MUST configure Gateway listeners with distinct port/protocol/hostname tuples and explicit `allowedRoutes.kinds` filters to restrict which route types (e.g., HTTPRoute, GRPCRoute, TLSRoute) may attach.

Platform operators MUST configure Gateway listeners with distinct port/protocol/hostname tuples and explicit `allowedRoutes.kinds` filters to restrict which route types (e.g., HTTPRoute, GRPCRoute, TLSRoute) may attach.

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 Kubernetes Gateway API v1.2 Cross-Namespace Route Delegation and ReferenceGrant Guardrails critical for AI coding assistants?

Enforces strict organizational boundary isolation, deterministic route attachment, and explicit ReferenceGrant declarations for Gateway API v1.2 in shared multi-tenant enterprise clusters. Prevents unauthorized cross-namespace route hijacking, unintended hostname collisions, and rogue backend referencing by mandating bidirectional consent between infrastructure owners and application development teams. Requires precise `allowedRoutes` scoping on Gateway listeners and explicit `ReferenceGrant` objects for cross-namespace service forwarding. 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 (Permissive Gateway AllowedRoutes and Unguarded Cross-Namespace Service Referencing Allowing Route Hijacking) 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

Kubernetes