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

Gateway API v1.2 Strict Cross-Namespace ReferenceGrant Isolation Standard

Enforces deterministic declarative authorization for cross-namespace routing and TLS certificate resolution under Kubernetes Gateway API v1.2. Prohibits implicit or wildcard cross-namespace object references, requiring explicit target-namespace ReferenceGrant manifests to eliminate lateral privilege escalation vectors in multi-tenant clusters.

Prohibited Anti-Pattern: Wildcard or Unauthenticated Cross-Namespace Backend and Secret References via Gateway API Listeners and Routes

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Gateway API v1.2 Strict Cross-Namespace ReferenceGrant Isolation Standard - Enforces deterministic declarative authorization for cross-namespace routing and TLS certificate resolution under Kubernetes Gateway API v1.2. Prohibits implicit or wildcard cross-namespace object references, requiring explicit target-namespace ReferenceGrant manifests to eliminate lateral privilege escalation vectors in multi-tenant clusters.
globs: deploy/**/*.yaml, k8s/**/*.yaml, manifests/**/*.yaml, charts/**/*.yaml, infra/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-v1-2-referencegrant-isolation"
---

# Gateway API v1.2 Strict Cross-Namespace ReferenceGrant Isolation Standard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Wildcard or Unauthenticated Cross-Namespace Backend and Secret References via Gateway API Listeners and Routes

## System Constraints
You are a Principal Site Reliability and Security Engineer enforcing the Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Isolation Standard. You must reject any declarative Gateway, HTTPRoute, GRPCRoute, TLSRoute, or Secret reference spanning namespace boundaries unless an explicit, least-privilege ReferenceGrant (gateway.networking.k8s.io/v1beta1) is co-located in the target provider namespace. Mandate strict namespace selector semantics and reject wildcard namespaces.

## Strict Guidelines
- Engineers MUST ALWAYS define a ReferenceGrant (`gateway.networking.k8s.io/v1beta1`) in the target resource's namespace whenever an HTTPRoute, GRPCRoute, or Gateway references a Service or Secret in a foreign namespace.
- Engineers MUST NOT configure `allowedRoutes.namespaces.from: All` on any Gateway listener in production; they MUST use `Selector` with strict tenancy label match expressions or `Same` namespace binding.
- Engineers MUST NOT omit explicit resource names in `ReferenceGrant.spec.to` when referencing cryptographic credentials (Secrets); wildcard targeting on Secrets is strictly forbidden.
- Services exposed across tenant namespaces MUST expose only named, non-privileged cluster ports and MUST explicitly restrict inbound Route kinds in `spec.from` to the exact required route kind.
- Route controllers and operators MUST emit validation failures (ResolvedRefs: False, Reason: RefNotPermitted) if an outbound cross-namespace `backendRef` lacks a verified target-namespace ReferenceGrant.
- Engineers MUST pin Gateway API Group versions to `gateway.networking.k8s.io/v1` for core resources and `gateway.networking.k8s.io/v1beta1` for `ReferenceGrant`.

## Bad vs Good Implementations
### Cross-Namespace Service Routing via ReferenceGrant

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The HTTPRoute in 'tenant-frontend' attempts to route traffic directly to 'payment-service' in 'payment-backend'. Without a corresponding ReferenceGrant deployed inside 'payment-backend', this configuration violates trust boundaries, causing Gateway API controllers to reject the route with RefNotPermitted or opening unintended ingress channels if security webhooks are unvalidated.
```yaml
# INSECURE: Tenant Route attempts cross-namespace service binding without target ReferenceGrant
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: checkout-route
  namespace: tenant-frontend
spec:
  parentRefs:
    - name: shared-gateway
      namespace: ingress-gateway
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/pay
      backendRefs:
        # Escalation: Direct cross-namespace call without authorization grant in payment-backend
        - name: payment-service
          namespace: payment-backend
          port: 8443
```

#### Production Standard (Correct)
> **Standard**: A ReferenceGrant is co-located in the target 'payment-backend' namespace. It strictly specifies the inbound consumer namespace ('tenant-frontend'), the accepted kind ('HTTPRoute'), and explicitly authorizes traffic strictly to the 'payment-service' Service.
```yaml
# SECURE: Target namespace explicitly authorizes foreign route access via ReferenceGrant
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-frontend-to-payment
  namespace: payment-backend
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: ingress-gateway
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/pay
      backendRefs:
        - name: payment-service
          namespace: payment-backend
          port: 8443
```

### Gateway Cross-Namespace TLS Certificate Resolution

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The Gateway attempts to bind a Secret residing in a foreign administrative namespace ('security-vault') while also opening the listener to routes from 'All' namespaces. Without a ReferenceGrant, the controller must refuse to load the secret; if improperly configured, this risks credential hijacking across administrative boundaries.
```yaml
# INSECURE: Gateway attempts cross-namespace Secret access without ReferenceGrant
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: enterprise-gateway
  namespace: infra-ingress
spec:
  gatewayClassName: internal-envoy
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      tls:
        mode: Terminate
        certificateRefs:
          # Critical Vulnerability: Arbitrary Secret access into security-vault
          - kind: Secret
            group: ""
            name: enterprise-wildcard-tls
            namespace: security-vault
      allowedRoutes:
        namespaces:
          from: All
```

#### Production Standard (Correct)
> **Standard**: The 'security-vault' namespace houses an explicit ReferenceGrant scoping certificate access strictly to the Gateway residing in 'infra-ingress'. The Gateway listeners constrain route attachment through deterministic label selectors instead of 'All'.
```yaml
# SECURE: Vault namespace grants isolated certificate read access to specific Gateway
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-ingress-tls-read
  namespace: security-vault
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: Gateway
      namespace: infra-ingress
  to:
    - group: ""
      kind: Secret
      name: enterprise-wildcard-tls
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: enterprise-gateway
  namespace: infra-ingress
spec:
  gatewayClassName: internal-envoy
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      tls:
        mode: Terminate
        certificateRefs:
          - kind: Secret
            group: ""
            name: enterprise-wildcard-tls
            namespace: security-vault
      allowedRoutes:
        namespaces:
          from: Selector
          selector:
            matchLabels:
              gateway.tenancy.internal/allowed: "true"
```

Recommended MCP Servers (mcp-serverlist.com)

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

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Gateway API v1.2 Strict Cross-Namespace ReferenceGrant Isolation Standard

Gateway API v1.2 Strict Cross-Namespace ReferenceGrant Isolation Standard defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic declarative authorization for cross-namespace routing and TLS certificate resolution under Kubernetes Gateway API v1.2. Prohibits implicit or wildcard cross-namespace object references, requiring explicit target-namespace ReferenceGrant manifests to eliminate lateral privilege escalation vectors in multi-tenant clusters. 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 Unauthenticated Cross-Namespace Backend and Secret References via Gateway API Listeners and Routes 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 Unauthenticated Cross-Namespace Backend and Secret References via Gateway API Listeners and RoutesCritical
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 resource's namespace whenever an HTTPRoute, GRPCRoute, or Gateway references a Service or Secret in a foreign namespace.

Engineers MUST ALWAYS define a ReferenceGrant (`gateway.networking.k8s.io/v1beta1`) in the target resource's namespace whenever an HTTPRoute, GRPCRoute, or Gateway references a Service or Secret in a foreign 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 `allowedRoutes.namespaces.from

Engineers MUST NOT configure `allowedRoutes.namespaces.from: All` on any Gateway listener in production; they MUST use `Selector` with strict tenancy label match expressions or `Same` namespace binding.

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 omit explicit resource names in `ReferenceGrant.spec.to` when referencing cryptographic credentials (Secrets)

Engineers MUST NOT omit explicit resource names in `ReferenceGrant.spec.to` when referencing cryptographic credentials (Secrets); wildcard targeting on Secrets is strictly forbidden.

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 exposed across tenant namespaces MUST expose only named, non-privileged cluster ports and MUST explicitly restrict inbound Route kinds in `spec.from` to the exact required route kind.

Services exposed across tenant namespaces MUST expose only named, non-privileged cluster ports and MUST explicitly restrict inbound Route kinds in `spec.from` to the exact required route kind.

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 controllers and operators MUST emit validation failures (ResolvedRefs

Route controllers and operators MUST emit validation failures (ResolvedRefs: False, Reason: RefNotPermitted) if an outbound cross-namespace `backendRef` lacks a verified 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.
6

Engineers MUST pin Gateway API Group versions to `gateway.networking.k8s.io/v1` for core resources and `gateway.networking.k8s.io/v1beta1` for `ReferenceGrant`.

Engineers MUST pin Gateway API Group versions to `gateway.networking.k8s.io/v1` for core resources and `gateway.networking.k8s.io/v1beta1` for `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.
Technical Questions & Answers

Frequently Asked Technical Questions

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

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

Enforces deterministic declarative authorization for cross-namespace routing and TLS certificate resolution under Kubernetes Gateway API v1.2. Prohibits implicit or wildcard cross-namespace object references, requiring explicit target-namespace ReferenceGrant manifests to eliminate lateral privilege escalation vectors in multi-tenant clusters. 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 Unauthenticated Cross-Namespace Backend and Secret References via Gateway API Listeners and Routes) 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

Gateway