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-route-delegation

Explicit Cross-Namespace ReferenceGrant and Route Delegation Boundaries in Gateway API v1.2

Enforces explicit, zero-trust cross-namespace routing and route delegation boundaries across multi-tenant clusters via Kubernetes Gateway API v1.2 ReferenceGrant resources. Prevents cross-tenant service hijacking, unauthorized host-collision takeovers, and silent traffic misdirection by mandating precise Group, Kind, and Namespace authorization tuples on all cross-namespace parent-child route attachments and backend service references.

Prohibited Anti-Pattern: Unbounded Cross-Namespace Routing and Missing ReferenceGrants Permitting Tenant Service Hijacking

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Explicit Cross-Namespace ReferenceGrant and Route Delegation Boundaries in Gateway API v1.2 - Enforces explicit, zero-trust cross-namespace routing and route delegation boundaries across multi-tenant clusters via Kubernetes Gateway API v1.2 ReferenceGrant resources. Prevents cross-tenant service hijacking, unauthorized host-collision takeovers, and silent traffic misdirection by mandating precise Group, Kind, and Namespace authorization tuples on all cross-namespace parent-child route attachments and backend service references.
globs: **/*.yaml, **/*.yml, deploy/**/*.yaml, k8s/**/*.yaml, helm/**/*.yaml, manifests/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-v1-2-referencegrant-route-delegation"
---

# Explicit Cross-Namespace ReferenceGrant and Route Delegation Boundaries in Gateway API v1.2

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unbounded Cross-Namespace Routing and Missing ReferenceGrants Permitting Tenant Service Hijacking

## System Constraints
You are a Principal DevOps and Platform Security Architect enforcing Kubernetes Gateway API v1.2 multi-tenant boundary specifications. Enforce immutable requirements around cross-namespace routing and Route Delegation. Under Gateway API v1.2, any cross-namespace reference—whether a Gateway attaching to an HTTPRoute, a parent HTTPRoute delegating to a child HTTPRoute in another namespace, or an HTTPRoute forwarding backend traffic to a Service in an adjacent namespace—MUST be secured by an explicit, non-promiscuous ReferenceGrant located in the target (resource-owning) namespace. Reject all implicit trust models, wildcard namespace selectors without label constraints, and missing ReferenceGrant declarations.

## Strict Guidelines
- Engineers MUST ALWAYS deploy a `ReferenceGrant` (group: `gateway.networking.k8s.io`, version: `v1beta1`) into the target namespace whenever an `HTTPRoute` forwards to a `Service` or delegates to a child `HTTPRoute` across namespace boundaries.
- Engineers MUST NOT configure `Gateway` listeners with `allowedRoutes.namespaces.from: All` without enforcing strict cryptographic TLS termination boundaries and distinct `allowedRoutes.namespaces.selector` label constraints.
- The `from` block of every `ReferenceGrant` MUST explicitly declare the exact `group` (`gateway.networking.k8s.io`), `kind` (`HTTPRoute` or `Gateway`), and originating `namespace` of the caller.
- The `to` block of every `ReferenceGrant` MUST scope permissions strictly to the exact target `group` (`core` for Services, `gateway.networking.k8s.io` for Route delegation) and MUST declare explicit `name` constraints wherever possible.
- Parent `HTTPRoute` resources delegating execution to child routes via `rules.backendRefs` with `kind: HTTPRoute` MUST NOT cross namespace boundaries without an accompanying `ReferenceGrant` in the child route's namespace granting delegation permissions.
- Route definitions MUST NEVER rely on default or implicit cross-namespace visibility; absent a validated `ReferenceGrant`, cross-namespace backend references MUST be treated as programmatic build errors.
- Teams MUST NOT share hostnames across tenant `HTTPRoute` manifests unless explicit parent-child route delegation rules and ownership checks are declared to eliminate route hijacking and hostname squatting.

## Bad vs Good Implementations
### Cross-Namespace Route Forwarding with ReferenceGrant Enforcement

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The HTTPRoute in namespace 'frontend-public' attempts to forward traffic directly to 'checkout-svc' in 'billing-prod' without a ReferenceGrant in 'billing-prod'. In conforming Gateway API implementations, this fails silently or results in invalid route status; in misconfigured environments, it exposes backend domains to unauthorized path-squatting.
```yaml
# INSECURE: HTTPRoute references Service in 'billing-prod' without target namespace authorization
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: store-route
  namespace: frontend-public
spec:
  parentRefs:
    - name: shared-edge-gw
      namespace: infra-ingress
  hostnames:
    - "shop.example.com"
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/checkout
      backendRefs:
        - name: checkout-svc
          namespace: billing-prod # Hijack risk: Billing domain has not authorized this ingress path
          port: 8443
```

#### Production Standard (Correct)
> **Standard**: The 'billing-prod' namespace explicitly creates a ReferenceGrant allowing the 'HTTPRoute' from 'frontend-public' to reference the exact 'checkout-svc' Service. Access is strictly scoped to the designated kind, group, namespace, and resource name.
```yaml
# SECURE: Explicit ReferenceGrant deployed inside target 'billing-prod' namespace
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-frontend-checkout-routing
  namespace: billing-prod
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: frontend-public
  to:
    - group: ""
      kind: Service
      name: checkout-svc
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: store-route
  namespace: frontend-public
spec:
  parentRefs:
    - name: shared-edge-gw
      namespace: infra-ingress
  hostnames:
    - "shop.example.com"
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/checkout
      backendRefs:
        - name: checkout-svc
          namespace: billing-prod
          port: 8443
```

### Hierarchical Multi-Tenant Parent-to-Child Route Delegation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Configuring allowedRoutes.namespaces.from: All on a shared Gateway enables any tenant in any namespace to claim hostnames, collide with existing routes, and intercept production traffic.
```yaml
# INSECURE: Parent route attempts delegation across namespace with wildcard Gateway attachment
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-edge-gw
  namespace: infra-ingress
spec:
  gatewayClassName: eg-gateway
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      allowedRoutes:
        namespaces:
          from: All # Permissive wildcard permits unvetted route hijacking across entire cluster
      tls:
        mode: Terminate
        certificateRefs:
          - name: wildcard-cert
```

#### Production Standard (Correct)
> **Standard**: The Gateway restricts route attachment using namespace label selectors, preventing arbitrary tenants from binding to listeners. The tenant namespace 'tenant-orders' uses an explicit ReferenceGrant allowing the ingress Gateway to attach to its routes.
```yaml
# SECURE: Scoped Gateway attachment with selector and matching ReferenceGrant for delegated route
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-edge-gw
  namespace: infra-ingress
spec:
  gatewayClassName: eg-gateway
  listeners:
    - name: https-app
      protocol: HTTPS
      port: 443
      hostname: "*.example.com"
      allowedRoutes:
        namespaces:
          from: Selector
          selector:
            matchLabels:
              gateway-access: "authorized"
      tls:
        mode: Terminate
        certificateRefs:
          - name: wildcard-cert
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-gateway-child-delegation
  namespace: tenant-orders
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: Gateway
      namespace: infra-ingress
  to:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Explicit Cross-Namespace ReferenceGrant and Route Delegation Boundaries in Gateway API v1.2

Explicit Cross-Namespace ReferenceGrant and Route Delegation Boundaries in Gateway API v1.2 defines an industrial-grade behavioral standard for devops engineering. Enforces explicit, zero-trust cross-namespace routing and route delegation boundaries across multi-tenant clusters via Kubernetes Gateway API v1.2 ReferenceGrant resources. Prevents cross-tenant service hijacking, unauthorized host-collision takeovers, and silent traffic misdirection by mandating precise Group, Kind, and Namespace authorization tuples on all cross-namespace parent-child route attachments and backend service references. 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: Unbounded Cross-Namespace Routing and Missing ReferenceGrants Permitting Tenant Service 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 DriftUnbounded Cross-Namespace Routing and Missing ReferenceGrants Permitting Tenant Service 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 deploy a `ReferenceGrant` (group

Engineers MUST ALWAYS deploy a `ReferenceGrant` (group: `gateway.networking.k8s.io`, version: `v1beta1`) into the target namespace whenever an `HTTPRoute` forwards to a `Service` or delegates to a child `HTTPRoute` 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.
2

Engineers MUST NOT configure `Gateway` listeners with `allowedRoutes.namespaces.from

Engineers MUST NOT configure `Gateway` listeners with `allowedRoutes.namespaces.from: All` without enforcing strict cryptographic TLS termination boundaries and distinct `allowedRoutes.namespaces.selector` label constraints.

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

The `from` block of every `ReferenceGrant` MUST explicitly declare the exact `group` (`gateway.networking.k8s.io`), `kind` (`HTTPRoute` or `Gateway`), and originating `namespace` of the caller.

The `from` block of every `ReferenceGrant` MUST explicitly declare the exact `group` (`gateway.networking.k8s.io`), `kind` (`HTTPRoute` or `Gateway`), and originating `namespace` of the caller.

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

The `to` block of every `ReferenceGrant` MUST scope permissions strictly to the exact target `group` (`core` for Services, `gateway.networking.k8s.io` for Route delegation) and MUST declare explicit `name` constraints wherever possible.

The `to` block of every `ReferenceGrant` MUST scope permissions strictly to the exact target `group` (`core` for Services, `gateway.networking.k8s.io` for Route delegation) and MUST declare explicit `name` constraints wherever possible.

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

Parent `HTTPRoute` resources delegating execution to child routes via `rules.backendRefs` with `kind

Parent `HTTPRoute` resources delegating execution to child routes via `rules.backendRefs` with `kind: HTTPRoute` MUST NOT cross namespace boundaries without an accompanying `ReferenceGrant` in the child route's namespace granting delegation permissions.

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.
6

Route definitions MUST NEVER rely on default or implicit cross-namespace visibility

Route definitions MUST NEVER rely on default or implicit cross-namespace visibility; absent a validated `ReferenceGrant`, cross-namespace backend references MUST be treated as programmatic build errors.

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.
7

Teams MUST NOT share hostnames across tenant `HTTPRoute` manifests unless explicit parent-child route delegation rules and ownership checks are declared to eliminate route hijacking and hostname squatting.

Teams MUST NOT share hostnames across tenant `HTTPRoute` manifests unless explicit parent-child route delegation rules and ownership checks are declared to eliminate route hijacking and hostname squatting.

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

Enforces explicit, zero-trust cross-namespace routing and route delegation boundaries across multi-tenant clusters via Kubernetes Gateway API v1.2 ReferenceGrant resources. Prevents cross-tenant service hijacking, unauthorized host-collision takeovers, and silent traffic misdirection by mandating precise Group, Kind, and Namespace authorization tuples on all cross-namespace parent-child route attachments and backend service references. 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 (Unbounded Cross-Namespace Routing and Missing ReferenceGrants Permitting Tenant Service 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

Explicit