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

Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Authorization Boundary

Enforces strict cross-namespace isolation boundaries under Kubernetes Gateway API v1.2 by requiring explicit ReferenceGrant resources for all cross-namespace references. Prohibits unauthorized route-to-service attachments and cross-namespace TLS secret references to eliminate ingress route hijacking, unauthorized backend routing, and certificate exfiltration.

Prohibited Anti-Pattern: Implicit Cross-Namespace Backend and Secret References Without Cryptographic ReferenceGrant Verification

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Authorization Boundary - Enforces strict cross-namespace isolation boundaries under Kubernetes Gateway API v1.2 by requiring explicit ReferenceGrant resources for all cross-namespace references. Prohibits unauthorized route-to-service attachments and cross-namespace TLS secret references to eliminate ingress route hijacking, unauthorized backend routing, and certificate exfiltration.
globs: deploy/**/*.yaml, k8s/**/*.yaml, charts/**/*.yaml, manifests/**/*.yaml, infra/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-referencegrant-isolation-standard-kubernetes-gatew-404"
---

# Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Authorization Boundary

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Implicit Cross-Namespace Backend and Secret References Without Cryptographic ReferenceGrant Verification

## System Constraints
You are a Principal Software Architect and Rules Author enforcing Kubernetes Gateway API v1.2 security invariants. Mandate that every cross-namespace reference—whether an HTTPRoute referencing a backend Service in a foreign namespace, or a Gateway referencing a TLS Secret in a separate namespace—is coupled with a granular, least-privilege `ReferenceGrant` resource residing in the target (resource-owning) namespace. Reject wildcards in `ReferenceGrant.spec.from.namespace` and ensure target resources enforce RBAC-aligned tenant isolation.

## Strict Guidelines
- Engineers MUST ALWAYS create a `ReferenceGrant` (group: `gateway.networking.k8s.io/v1beta1`) located within the target namespace whenever an `HTTPRoute`, `GRPCRoute`, or `Gateway` crosses namespace boundaries.
- Engineers MUST NOT reference cross-namespace `Service` or `Secret` resources without defining explicitly bounded `from` (group, kind, namespace) and `to` (group, kind, name) specs.
- Code MUST explicitly specify exact target resource names in `ReferenceGrant.spec.to[].name` where supported, avoiding ambient cluster-wide exposure of non-target services in the owner namespace.
- Services MUST enforce namespace-level tenant isolation, ensuring that Gateway listeners specify `allowedRoutes.namespaces.from: Selector` or `Same` rather than unbounded `All` unless gated by verified policy controllers.
- Components NEVER bundle TLS certificates in tenant namespaces to share with global Gateways without mutual explicit ReferenceGrant handshakes and validation.

## Bad vs Good Implementations
### Cross-Namespace Route Attachment to Core Service Without ReferenceGrant

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The HTTPRoute in namespace 'tenant-alpha' references 'payment-service' in 'core-finance'. In Gateway API v1.2, this cross-namespace backend reference is invalid and insecure by default; without a corresponding ReferenceGrant in 'core-finance', the route fails quietly or exposes a route hijacking vulnerability if validation webhooks are misconfigured.
```yaml
# Anti-Pattern: HTTPRoute references a Service across namespaces without target namespace consent
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: tenant-route
  namespace: tenant-alpha
spec:
  parentRefs:
    - name: shared-gateway
      namespace: gateway-infra
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/v1
      backendRefs:
        - name: payment-service
          namespace: core-finance
          port: 8443
```

#### Production Standard (Correct)
> **Standard**: A ReferenceGrant is placed in 'core-finance' (the target namespace). It explicitly grants 'tenant-alpha' HTTPRoutes permission to route traffic strictly to 'payment-service', preserving strict multi-tenant boundaries.
```yaml
# Compliant: HTTPRoute paired with an explicit ReferenceGrant in the owner namespace
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: tenant-route
  namespace: tenant-alpha
spec:
  parentRefs:
    - name: shared-gateway
      namespace: gateway-infra
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/v1
      backendRefs:
        - name: payment-service
          namespace: core-finance
          port: 8443
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-alpha-payment-access
  namespace: core-finance
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: tenant-alpha
  to:
    - group: ""
      kind: Service
      name: payment-service
```

### Gateway Cross-Namespace TLS Secret Reference

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: A Gateway in 'edge-system' directly attempts to load a TLS Secret from 'cert-store'. Without a ReferenceGrant inside 'cert-store', this operation violates secret boundary constraints and will be rejected by compliant Gateway API controllers.
```yaml
# Anti-Pattern: Shared Gateway references a Secret in cert-store without a ReferenceGrant
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: edge-gateway
  namespace: edge-system
spec:
  gatewayClassName: istio-gateway
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      tls:
        mode: Terminate
        certificateRefs:
          - group: ""
            kind: Secret
            name: wildcard-tls
            namespace: cert-store
```

#### Production Standard (Correct)
> **Standard**: The 'cert-store' namespace includes a ReferenceGrant that explicitly authorizes the 'edge-gateway' in 'edge-system' to bind and consume 'wildcard-tls', maintaining least-privilege cryptographic boundaries.
```yaml
# Compliant: Gateway with corresponding ReferenceGrant in cert-store namespace
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: edge-gateway
  namespace: edge-system
spec:
  gatewayClassName: istio-gateway
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      tls:
        mode: Terminate
        certificateRefs:
          - group: ""
            kind: Secret
            name: wildcard-tls
            namespace: cert-store
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-edge-gateway-certs
  namespace: cert-store
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: Gateway
      namespace: edge-system
  to:
    - group: ""
      kind: Secret
      name: wildcard-tls
```

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: Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Authorization Boundary

Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Authorization Boundary defines an industrial-grade behavioral standard for devops engineering. Enforces strict cross-namespace isolation boundaries under Kubernetes Gateway API v1.2 by requiring explicit ReferenceGrant resources for all cross-namespace references. Prohibits unauthorized route-to-service attachments and cross-namespace TLS secret references to eliminate ingress route hijacking, unauthorized backend routing, and certificate exfiltration. 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: Implicit Cross-Namespace Backend and Secret References Without Cryptographic ReferenceGrant Verification 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 DriftImplicit Cross-Namespace Backend and Secret References Without Cryptographic ReferenceGrant VerificationCritical
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` (group

Engineers MUST ALWAYS create a `ReferenceGrant` (group: `gateway.networking.k8s.io/v1beta1`) located within the target namespace whenever an `HTTPRoute`, `GRPCRoute`, or `Gateway` crosses 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 reference cross-namespace `Service` or `Secret` resources without defining explicitly bounded `from` (group, kind, namespace) and `to` (group, kind, name) specs.

Engineers MUST NOT reference cross-namespace `Service` or `Secret` resources without defining explicitly bounded `from` (group, kind, namespace) and `to` (group, kind, name) specs.

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 MUST explicitly specify exact target resource names in `ReferenceGrant.spec.to[].name` where supported, avoiding ambient cluster-wide exposure of non-target services in the owner namespace.

Code MUST explicitly specify exact target resource names in `ReferenceGrant.spec.to[].name` where supported, avoiding ambient cluster-wide exposure of non-target services in the owner 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 enforce namespace-level tenant isolation, ensuring that Gateway listeners specify `allowedRoutes.namespaces.from

Services MUST enforce namespace-level tenant isolation, ensuring that Gateway listeners specify `allowedRoutes.namespaces.from: Selector` or `Same` rather than unbounded `All` unless gated by verified policy controllers.

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 bundle TLS certificates in tenant namespaces to share with global Gateways without mutual explicit ReferenceGrant handshakes and validation.

Components NEVER bundle TLS certificates in tenant namespaces to share with global Gateways without mutual explicit ReferenceGrant handshakes and validation.

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 ReferenceGrant Authorization Boundary critical for AI coding assistants?

Enforces strict cross-namespace isolation boundaries under Kubernetes Gateway API v1.2 by requiring explicit ReferenceGrant resources for all cross-namespace references. Prohibits unauthorized route-to-service attachments and cross-namespace TLS secret references to eliminate ingress route hijacking, unauthorized backend routing, and certificate exfiltration. 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 (Implicit Cross-Namespace Backend and Secret References Without Cryptographic ReferenceGrant Verification) 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