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 Gateway API v1.2 Cross-Namespace ReferenceGrant Isolation

Enforces strict bidirectional explicit authorization across namespace boundaries within Kubernetes Gateway API v1.2 implementations. Prevents unauthorized multi-tenant route hijacking by ensuring that any cross-namespace reference from an HTTPRoute to a Service or from a Gateway to an HTTPRoute is guarded by a corresponding ReferenceGrant in the target namespace. Guarantees that reconciliation controllers drop unpermitted cross-namespace references with a ReferenceGrantStatus condition of ResolvedRefs=False.

Prohibited Anti-Pattern: Unvalidated Cross-Namespace Backend and Gateway Attachment Permitting Route Hijacking

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Isolation - Enforces strict bidirectional explicit authorization across namespace boundaries within Kubernetes Gateway API v1.2 implementations. Prevents unauthorized multi-tenant route hijacking by ensuring that any cross-namespace reference from an HTTPRoute to a Service or from a Gateway to an HTTPRoute is guarded by a corresponding ReferenceGrant in the target namespace. Guarantees that reconciliation controllers drop unpermitted cross-namespace references with a ReferenceGrantStatus condition of ResolvedRefs=False.
globs: **/k8s/**/*.yaml, **/k8s/**/*.yml, **/helm/**/*.yaml, **/helm/**/*.yml, **/kustomize/**/*.yaml, **/kustomize/**/*.yml, **/manifests/**/*.yaml, **/manifests/**/*.yml, **/*.k8s.ts, **/*.cdk8s.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-referencegrant-isolation-standard"
---

# Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Isolation

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unvalidated Cross-Namespace Backend and Gateway Attachment Permitting Route Hijacking

## System Constraints
You are a Principal Cloud Native Architect enforcing strict Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Isolation. Mandate invariants that prevent unauthorized cross-namespace routing, cross-tenant resource hijacking, and implicit authorization assumptions in all Gateway, HTTPRoute, and ReferenceGrant definitions.

## Strict Guidelines
- Engineers MUST ALWAYS provision a `ReferenceGrant` resource within the target resource's namespace whenever an `HTTPRoute` references a backend `Service` in a distinct namespace.
- Engineers MUST ALWAYS restrict Gateway listener attachments using explicit `AllowedRoutes` namespaces policies (via `namespaces.from: Selector` or `namespaces.from: Same`) rather than unconstrained `namespaces.from: All` without upstream gatekeeping.
- Engineers MUST NOT reference cross-namespace resources without specifying the target resource's exact `group` and `kind` in the `ReferenceGrant`'s `from` and `to` declarations.
- Reconciliation engines and automated validation tooling MUST verify that `HTTPRoute.spec.rules.backendRefs` across namespaces mathematically resolve to an active, valid `ReferenceGrant`; otherwise, routes MUST be marked unresolvable.
- Platform engineers MUST ensure that `ReferenceGrant` resources reside strictly in the namespace owning the backend `Service` or target resource, NEVER in the referencing client route's namespace.
- Services MUST NEVER rely on implicit Gateway API trust domains; cross-namespace Service exposure without a `ReferenceGrant` MUST fail closed with `ResolvedRefs=False` and reason `RefNotPermitted`.

## Bad vs Good Implementations
### Unauthenticated Cross-Namespace Backend Attachment Vulnerability

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The HTTPRoute directly references a Service in the 'core-services' namespace. Without a corresponding ReferenceGrant in 'core-services', this creates a cross-namespace privilege escalation and allows rogue tenants to hijack internal services.
```yaml
# INSECURE: HTTPRoute in tenant-a references Service in core-services without authorization
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: tenant-a-api
  namespace: tenant-a
spec:
  parentRefs:
    - name: shared-gateway
      namespace: ingress-infra
  hostnames:
    - "api.example.com"
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /core
      backendRefs:
        # Security Breach: Hijacks core-services/auth-service without target namespace consent
        - name: auth-service
          namespace: core-services
          port: 8080
```

#### Production Standard (Correct)
> **Standard**: A ReferenceGrant is explicitly deployed in 'core-services', specifically permitting the HTTPRoute from 'tenant-a' to bind strictly to 'auth-service'. The controller verifies this bidirectional contract before establishing the route.
```yaml
# SECURE: Explicit ReferenceGrant in target namespace paired with secure HTTPRoute
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-tenant-a-auth
  namespace: core-services
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: tenant-a
  to:
    - group: ""
      kind: Service
      name: auth-service
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: tenant-a-api
  namespace: tenant-a
spec:
  parentRefs:
    - name: shared-gateway
      namespace: ingress-infra
  hostnames:
    - "api.example.com"
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /core
      backendRefs:
        - name: auth-service
          namespace: core-services
          port: 8080
```

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 Cross-Namespace ReferenceGrant Isolation

Kubernetes Gateway API v1.2 Cross-Namespace ReferenceGrant Isolation defines an industrial-grade behavioral standard for devops engineering. Enforces strict bidirectional explicit authorization across namespace boundaries within Kubernetes Gateway API v1.2 implementations. Prevents unauthorized multi-tenant route hijacking by ensuring that any cross-namespace reference from an HTTPRoute to a Service or from a Gateway to an HTTPRoute is guarded by a corresponding ReferenceGrant in the target namespace. Guarantees that reconciliation controllers drop unpermitted cross-namespace references with a ReferenceGrantStatus condition of ResolvedRefs=False. 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 Backend and Gateway Attachment Permitting Route 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 DriftUnvalidated Cross-Namespace Backend and Gateway Attachment Permitting Route 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 provision a `ReferenceGrant` resource within the target resource's namespace whenever an `HTTPRoute` references a backend `Service` in a distinct namespace.

Engineers MUST ALWAYS provision a `ReferenceGrant` resource within the target resource's namespace whenever an `HTTPRoute` references a backend `Service` in a distinct 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 ALWAYS restrict Gateway listener attachments using explicit `AllowedRoutes` namespaces policies (via `namespaces.from

Engineers MUST ALWAYS restrict Gateway listener attachments using explicit `AllowedRoutes` namespaces policies (via `namespaces.from: Selector` or `namespaces.from: Same`) rather than unconstrained `namespaces.from: All` without upstream gatekeeping.

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 reference cross-namespace resources without specifying the target resource's exact `group` and `kind` in the `ReferenceGrant`'s `from` and `to` declarations.

Engineers MUST NOT reference cross-namespace resources without specifying the target resource's exact `group` and `kind` in the `ReferenceGrant`'s `from` and `to` declarations.

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

Reconciliation engines and automated validation tooling MUST verify that `HTTPRoute.spec.rules.backendRefs` across namespaces mathematically resolve to an active, valid `ReferenceGrant`

Reconciliation engines and automated validation tooling MUST verify that `HTTPRoute.spec.rules.backendRefs` across namespaces mathematically resolve to an active, valid `ReferenceGrant`; otherwise, routes MUST be marked unresolvable.

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

Platform engineers MUST ensure that `ReferenceGrant` resources reside strictly in the namespace owning the backend `Service` or target resource, NEVER in the referencing client route's namespace.

Platform engineers MUST ensure that `ReferenceGrant` resources reside strictly in the namespace owning the backend `Service` or target resource, NEVER in the referencing client route's 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.
6

Services MUST NEVER rely on implicit Gateway API trust domains

Services MUST NEVER rely on implicit Gateway API trust domains; cross-namespace Service exposure without a `ReferenceGrant` MUST fail closed with `ResolvedRefs=False` and reason `RefNotPermitted`.

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 Isolation critical for AI coding assistants?

Enforces strict bidirectional explicit authorization across namespace boundaries within Kubernetes Gateway API v1.2 implementations. Prevents unauthorized multi-tenant route hijacking by ensuring that any cross-namespace reference from an HTTPRoute to a Service or from a Gateway to an HTTPRoute is guarded by a corresponding ReferenceGrant in the target namespace. Guarantees that reconciliation controllers drop unpermitted cross-namespace references with a ReferenceGrantStatus condition of ResolvedRefs=False. 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 Backend and Gateway Attachment Permitting Route 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