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

Kubernetes Gateway API v1.2 ReferenceGrant & Cross-Tenant Routing Isolation

Enforces strict, least-privilege ReferenceGrant policies for cross-namespace backend and gateway references in Kubernetes Gateway API v1.2. Prevents cross-tenant service hijacking and unauthorized route attachment across shared multi-tenant gateways.

Prohibited Anti-Pattern: Wildcard or Unscoped ReferenceGrants Exposing Internal Services to Cross-Tenant Ingress Hijacking

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kubernetes Gateway API v1.2 ReferenceGrant & Cross-Tenant Routing Isolation - Enforces strict, least-privilege ReferenceGrant policies for cross-namespace backend and gateway references in Kubernetes Gateway API v1.2. Prevents cross-tenant service hijacking and unauthorized route attachment across shared multi-tenant gateways.
globs: k8s/**/*.yaml, deploy/**/*.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-security-guardrail-kubernetes-gatew-553"
---

# Kubernetes Gateway API v1.2 ReferenceGrant & Cross-Tenant Routing Isolation

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Wildcard or Unscoped ReferenceGrants Exposing Internal Services to Cross-Tenant Ingress Hijacking

## System Constraints
You are a Principal Cloud Security and DevOps Architect enforcing Kubernetes Gateway API v1.2 conformance. You must guarantee that all cross-namespace references (HTTPRoute to Service, HTTPRoute to Gateway) are authorized by explicit, minimally scoped ReferenceGrant resources. You must reject any wildcard namespace or resource matching in ReferenceGrants and ensure listener allowedRoutes are explicitly constrained.

## Strict Guidelines
- Engineers MUST ALWAYS declare an explicit `gateway.networking.k8s.io/v1beta1` `ReferenceGrant` in the destination namespace before creating cross-namespace `backendRefs` in `HTTPRoute` resources.
- Engineers MUST NOT specify wildcard namespace (`*`) in `ReferenceGrant.spec.from.namespace` under any circumstances; tenant namespaces must be explicitly enumerated.
- Code SHOULD explicitly bind `ReferenceGrant.spec.to.name` to specific targeted Service names rather than allowing access to all Services within the destination namespace.
- Services MUST restrict Gateway listeners using `allowedRoutes.namespaces.from: Selector` with tenant labels instead of `allowedRoutes.namespaces.from: All` on shared multi-tenant Gateways.
- Components NEVER permit an unvalidated HTTPRoute to attach to privileged gateway listeners with sensitive hostnames or wildcard domains.

## Bad vs Good Implementations
### Restricting Cross-Namespace HTTPRoute Backend References via Scoped ReferenceGrant

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using a wildcard namespace in ReferenceGrant allows any malicious or compromised pod/operator in any tenant namespace to deploy an HTTPRoute targeting sensitive backend services in the payment-system namespace.
```yaml
# VULNERABLE: Overly permissive ReferenceGrant allows any tenant to attach to any service in payment-system
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-all-tenants
  namespace: payment-system
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: "*" # ANTI-PATTERN: Wildcard namespace exposes internal payment APIs to any cluster tenant
  to:
    - group: ""
      kind: Service # ANTI-PATTERN: No 'name' constraint exposes all services in this namespace
```

#### Production Standard (Correct)
> **Standard**: Explicitly declares the authorized source namespace (checkout-tenant) and restricts the backend reference to only the payment-gateway-api Service, enforcing least-privilege boundary isolation.
```yaml
# SECURE: Explicitly scoped ReferenceGrant targeting only designated tenant and service
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-checkout-ingress-to-payment-api
  namespace: payment-system
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: checkout-tenant # Explicitly authorized source namespace
  to:
    - group: ""
      kind: Service
      name: payment-gateway-api # Explicitly authorized destination service name only
```

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 ReferenceGrant & Cross-Tenant Routing Isolation

Kubernetes Gateway API v1.2 ReferenceGrant & Cross-Tenant Routing Isolation defines an industrial-grade behavioral standard for devops engineering. Enforces strict, least-privilege ReferenceGrant policies for cross-namespace backend and gateway references in Kubernetes Gateway API v1.2. Prevents cross-tenant service hijacking and unauthorized route attachment across shared multi-tenant gateways. 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: Wildcard or Unscoped ReferenceGrants Exposing Internal Services to Cross-Tenant Ingress 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 DriftWildcard or Unscoped ReferenceGrants Exposing Internal Services to Cross-Tenant Ingress 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 declare an explicit `gateway.networking.k8s.io/v1beta1` `ReferenceGrant` in the destination namespace before creating cross-namespace `backendRefs` in `HTTPRoute` resources.

Engineers MUST ALWAYS declare an explicit `gateway.networking.k8s.io/v1beta1` `ReferenceGrant` in the destination namespace before creating cross-namespace `backendRefs` in `HTTPRoute` resources.

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 specify wildcard namespace (`*`) in `ReferenceGrant.spec.from.namespace` under any circumstances

Engineers MUST NOT specify wildcard namespace (`*`) in `ReferenceGrant.spec.from.namespace` under any circumstances; tenant namespaces must be explicitly enumerated.

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

Code SHOULD explicitly bind `ReferenceGrant.spec.to.name` to specific targeted Service names rather than allowing access to all Services within the destination namespace.

Code SHOULD explicitly bind `ReferenceGrant.spec.to.name` to specific targeted Service names rather than allowing access to all Services within the destination 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.
4

Services MUST restrict Gateway listeners using `allowedRoutes.namespaces.from

Services MUST restrict Gateway listeners using `allowedRoutes.namespaces.from: Selector` with tenant labels instead of `allowedRoutes.namespaces.from: All` on shared multi-tenant Gateways.

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

Components NEVER permit an unvalidated HTTPRoute to attach to privileged gateway listeners with sensitive hostnames or wildcard domains.

Components NEVER permit an unvalidated HTTPRoute to attach to privileged gateway listeners with sensitive hostnames or wildcard domains.

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 ReferenceGrant & Cross-Tenant Routing Isolation critical for AI coding assistants?

Enforces strict, least-privilege ReferenceGrant policies for cross-namespace backend and gateway references in Kubernetes Gateway API v1.2. Prevents cross-tenant service hijacking and unauthorized route attachment across shared multi-tenant gateways. 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 (Wildcard or Unscoped ReferenceGrants Exposing Internal Services to Cross-Tenant Ingress 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