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

Mandatory ReferenceGrant Handshake for Cross-Namespace Gateway API Route Delegation

Enforces explicit two-way cryptographic and administrative trust boundaries when delegating Gateway API routes or referencing cross-namespace backend services and secrets. In Kubernetes Gateway API v1.2+, any cross-namespace reference between Gateways, HTTPRoutes, and Services must have a co-located ReferenceGrant in the target namespace to prevent route hijacking and unauthorized traffic ingress.

Prohibited Anti-Pattern: Unvalidated Cross-Namespace Route Attachment or Backend Forwarding Without Explicit Target-Namespace ReferenceGrant

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Mandatory ReferenceGrant Handshake for Cross-Namespace Gateway API Route Delegation - Enforces explicit two-way cryptographic and administrative trust boundaries when delegating Gateway API routes or referencing cross-namespace backend services and secrets. In Kubernetes Gateway API v1.2+, any cross-namespace reference between Gateways, HTTPRoutes, and Services must have a co-located ReferenceGrant in the target namespace to prevent route hijacking and unauthorized traffic ingress.
globs: deploy/**/*.yaml, k8s/**/*.yaml, helm/**/*.yaml, infrastructure/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-referencegrant-delegation"
---

# Mandatory ReferenceGrant Handshake for Cross-Namespace Gateway API Route Delegation

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unvalidated Cross-Namespace Route Attachment or Backend Forwarding Without Explicit Target-Namespace ReferenceGrant

## System Constraints
You are a Principal Cloud Native Infrastructure Architect enforcing Kubernetes Gateway API v1.2 networking security. You must ensure that all cross-namespace references between Gateways, HTTPRoutes, Services, and Secrets mandate an explicit, granular ReferenceGrant resource residing inside the target namespace. Flag any cross-namespace parentRef or backendRef that lacks an authoritative ReferenceGrant handshake.

## Strict Guidelines
- Engineers MUST ALWAYS create a `ReferenceGrant` in the target namespace whenever an `HTTPRoute` references a `Service` or parent `Gateway` residing in an external namespace.
- Engineers MUST NOT configure Gateways with `allowedRoutes.namespaces.from: All` without enforcing strict hostname collision prevention and explicit tenant ReferenceGrants.
- Engineers MUST scope `ReferenceGrant` rules to explicit `from` kinds (`HTTPRoute`, `Gateway`) and specific source namespaces, never using permissive wildcard selectors.
- Services and Secrets referenced across namespace boundaries MUST reside in the same namespace as the authorizing `ReferenceGrant` resource.
- Route delegation chains (parent HTTPRoute to child HTTPRoute) MUST establish reciprocal validation, ensuring the parent explicitly matches child rule selectors and the child includes valid `parentRefs`.

## Bad vs Good Implementations
### Cross-Namespace Backend Routing Without ReferenceGrant (Vulnerable) vs With ReferenceGrant (Secure)

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The HTTPRoute in 'tenant-frontend' attempts to forward traffic directly into 'tenant-payments' without an administrative permit. Without a ReferenceGrant inside 'tenant-payments', Gateway controllers will drop this route, or in misconfigured environments, allow unauthorized cross-tenant service exposure.
```yaml
# INSECURE: HTTPRoute in tenant-frontend attempts to route traffic to payment-service in tenant-payments
# Gateway API controllers will reject this silent failure or allow hostile hijacking if RBAC is loose.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: checkout-route
  namespace: tenant-frontend
spec:
  parentRefs:
    - name: shared-gateway
      namespace: infra-ingress
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/pay
      backendRefs:
        - name: payment-service
          namespace: tenant-payments
          port: 8443
```

#### Production Standard (Correct)
> **Standard**: The ReferenceGrant resource in 'tenant-payments' establishes an authoritative handshake, explicitly granting HTTPRoutes from 'tenant-frontend' permission to forward traffic to the 'payment-service' Service.
```yaml
# SECURE: Target namespace explicitly authorizes the cross-namespace backend reference
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-checkout-to-payments
  namespace: tenant-payments
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: tenant-frontend
  to:
    - group: ""
      kind: Service
      name: payment-service
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: checkout-route
  namespace: tenant-frontend
spec:
  parentRefs:
    - name: shared-gateway
      namespace: infra-ingress
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/pay
      backendRefs:
        - name: payment-service
          namespace: tenant-payments
          port: 8443
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Mandatory ReferenceGrant Handshake for Cross-Namespace Gateway API Route Delegation

Mandatory ReferenceGrant Handshake for Cross-Namespace Gateway API Route Delegation defines an industrial-grade behavioral standard for devops engineering. Enforces explicit two-way cryptographic and administrative trust boundaries when delegating Gateway API routes or referencing cross-namespace backend services and secrets. In Kubernetes Gateway API v1.2+, any cross-namespace reference between Gateways, HTTPRoutes, and Services must have a co-located ReferenceGrant in the target namespace to prevent route hijacking and unauthorized traffic ingress. 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: Unvalidated Cross-Namespace Route Attachment or Backend Forwarding Without Explicit Target-Namespace 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 DriftUnvalidated Cross-Namespace Route Attachment or Backend Forwarding Without Explicit Target-Namespace 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 create a `ReferenceGrant` in the target namespace whenever an `HTTPRoute` references a `Service` or parent `Gateway` residing in an external namespace.

Engineers MUST ALWAYS create a `ReferenceGrant` in the target namespace whenever an `HTTPRoute` references a `Service` or parent `Gateway` residing in an external namespace.

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

Engineers MUST NOT configure Gateways with `allowedRoutes.namespaces.from: All` without enforcing strict hostname collision prevention and explicit tenant ReferenceGrants.

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 scope `ReferenceGrant` rules to explicit `from` kinds (`HTTPRoute`, `Gateway`) and specific source namespaces, never using permissive wildcard selectors.

Engineers MUST scope `ReferenceGrant` rules to explicit `from` kinds (`HTTPRoute`, `Gateway`) and specific source namespaces, never using permissive wildcard selectors.

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 and Secrets referenced across namespace boundaries MUST reside in the same namespace as the authorizing `ReferenceGrant` resource.

Services and Secrets referenced across namespace boundaries MUST reside in the same namespace as the authorizing `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.
5

Route delegation chains (parent HTTPRoute to child HTTPRoute) MUST establish reciprocal validation, ensuring the parent explicitly matches child rule selectors and the child includes valid `parentRefs`.

Route delegation chains (parent HTTPRoute to child HTTPRoute) MUST establish reciprocal validation, ensuring the parent explicitly matches child rule selectors and the child includes valid `parentRefs`.

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 Mandatory ReferenceGrant Handshake for Cross-Namespace Gateway API Route Delegation critical for AI coding assistants?

Enforces explicit two-way cryptographic and administrative trust boundaries when delegating Gateway API routes or referencing cross-namespace backend services and secrets. In Kubernetes Gateway API v1.2+, any cross-namespace reference between Gateways, HTTPRoutes, and Services must have a co-located ReferenceGrant in the target namespace to prevent route hijacking and unauthorized traffic ingress. 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 (Unvalidated Cross-Namespace Route Attachment or Backend Forwarding Without Explicit Target-Namespace 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

Mandatory