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-cross-namespace-attachment-guardrail

Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment & Tenant Isolation

Enforces strict cryptographic and architectural tenant isolation for Kubernetes Gateway API v1.2 by prohibiting global wildcard route attachments and unvalidated URI paths. Mandates namespace label selector boundaries on Gateway listeners, explicit ReferenceGrant resources for cross-namespace backend references, and strict tenant-scoped hostname matching to prevent malicious URI hijacking and route shadowing.

Prohibited Anti-Pattern: Unrestricted Gateway Listener Attachment and Permissive Cross-Namespace Service Referencing

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment & Tenant Isolation - Enforces strict cryptographic and architectural tenant isolation for Kubernetes Gateway API v1.2 by prohibiting global wildcard route attachments and unvalidated URI paths. Mandates namespace label selector boundaries on Gateway listeners, explicit ReferenceGrant resources for cross-namespace backend references, and strict tenant-scoped hostname matching to prevent malicious URI hijacking and route shadowing.
globs: deploy/**/*.yaml, k8s/**/*.yaml, manifests/**/*.yaml, helm/**/*.yaml, charts/**/*.yaml, infrastructure/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-v1-2-cross-namespace-attachment-guardrail"
---

# Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment & Tenant Isolation

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unrestricted Gateway Listener Attachment and Permissive Cross-Namespace Service Referencing

## System Constraints
You are a Principal Platform and Security Architect enforcing Kubernetes Gateway API v1.2 standards across decentralized multi-tenant clusters. You must prevent route hijacking, namespace escape, and cross-tenant traffic interception. Mandate explicit `allowedRoutes.namespaces.from: Selector` controls on all Gateway listeners, reject `namespaces.from: All`, enforce explicit `ReferenceGrant` manifests when routing across namespace boundaries, and ensure all HTTPRoutes specify restrictive, fully-qualified tenant hostnames and non-conflicting path matchers.

## Strict Guidelines
- Platform Engineers MUST NEVER configure Gateway listeners with `allowedRoutes.namespaces.from: All` under any circumstances in shared or production clusters.
- Engineers MUST ALWAYS enforce listener route attachment boundaries using `allowedRoutes.namespaces.from: Selector` coupled with cryptographically verified tenant namespace labels (e.g., `tenant.kubernetes.io/network-zone: tenant-a`).
- Tenant Engineers MUST define explicit, non-overlapping `hostnames` (e.g., `api.tenant-a.corp.internal`) on both the Gateway listener and the child `HTTPRoute`; catch-all wildcards (`*.internal` or no hostname) on unprivileged routes are strictly prohibited.
- Cross-namespace backend references (`parentRefs` or `backendRefs` across distinct namespaces) MUST ALWAYS be explicitly authorized by a co-located `ReferenceGrant` resource inside the target namespace containing the target service or secret.
- Tenant HTTPRoutes MUST NOT bind to root paths (`path: { type: PathPrefix, value: '/' }`) unless explicitly validated against dedicated hostnames to prevent URI shadowing of other tenant routes.
- Gateway listeners MUST enforce TLS termination or passthrough with tenant-scoped Secret references guarded by `ReferenceGrant` if certificates reside outside the Gateway control namespace.

## Bad vs Good Implementations
### Gateway Listener Route Attachment Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Setting `namespaces.from: All` allows any tenant in the cluster to attach HTTPRoutes to the edge Gateway, enabling malicious or accidental route hijacking, root path shadowing, and cross-tenant traffic interception.
```yaml
# VULNERABLE: Global attachment allows any namespace to attach routes,
# hijack hostnames, and shadow sibling tenant endpoints.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-edge-gw
  namespace: gateway-system
spec:
  gatewayClassName: internal-gateway
  listeners:
    - name: https-wildcard
      port: 443
      protocol: HTTPS
      allowedRoutes:
        namespaces:
          from: All # CRITICAL SECURITY RISK: Arbitrary tenant route hijacking
```

#### Production Standard (Correct)
> **Standard**: Enforces least-privilege route binding by explicitly restricting listener attachment to namespaces with matching tenant labels. Cross-namespace certificate and backend resolution is cryptographically secured and explicitly authorized using `ReferenceGrant`.
```yaml
# HARDENED: Cryptographic multi-tenant attachment with label selectors
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-edge-gw
  namespace: gateway-system
spec:
  gatewayClassName: internal-gateway
  listeners:
    - name: https-tenant-a
      hostname: "tenant-a.internal.example.com"
      port: 443
      protocol: HTTPS
      tls:
        mode: Terminate
        certificateRefs:
          - kind: Secret
            name: tenant-a-cert
            namespace: tenant-a # Requires ReferenceGrant in tenant-a
      allowedRoutes:
        namespaces:
          from: Selector
          selector:
            matchLabels:
              tenant.kubernetes.io/access-zone: tenant-a
---
# Explicit cross-namespace authorization grant inside tenant-a
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-gw-cert-and-service-access
  namespace: tenant-a
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: Gateway
      namespace: gateway-system
  to:
    - group: ""
      kind: Secret
      name: tenant-a-cert
```

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 Route Attachment & Tenant Isolation

Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment & Tenant Isolation defines an industrial-grade behavioral standard for devops engineering. Enforces strict cryptographic and architectural tenant isolation for Kubernetes Gateway API v1.2 by prohibiting global wildcard route attachments and unvalidated URI paths. Mandates namespace label selector boundaries on Gateway listeners, explicit ReferenceGrant resources for cross-namespace backend references, and strict tenant-scoped hostname matching to prevent malicious URI hijacking and route shadowing. 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: Unrestricted Gateway Listener Attachment and Permissive Cross-Namespace Service Referencing 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 DriftUnrestricted Gateway Listener Attachment and Permissive Cross-Namespace Service ReferencingCritical
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

Platform Engineers MUST NEVER configure Gateway listeners with `allowedRoutes.namespaces.from

Platform Engineers MUST NEVER configure Gateway listeners with `allowedRoutes.namespaces.from: All` under any circumstances in shared or production clusters.

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 enforce listener route attachment boundaries using `allowedRoutes.namespaces.from

Engineers MUST ALWAYS enforce listener route attachment boundaries using `allowedRoutes.namespaces.from: Selector` coupled with cryptographically verified tenant namespace labels (e.g., `tenant.kubernetes.io/network-zone: tenant-a`).

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

Tenant Engineers MUST define explicit, non-overlapping `hostnames` (e.g., `api.tenant-a.corp.internal`) on both the Gateway listener and the child `HTTPRoute`

Tenant Engineers MUST define explicit, non-overlapping `hostnames` (e.g., `api.tenant-a.corp.internal`) on both the Gateway listener and the child `HTTPRoute`; catch-all wildcards (`*.internal` or no hostname) on unprivileged routes are strictly prohibited.

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

Cross-namespace backend references (`parentRefs` or `backendRefs` across distinct namespaces) MUST ALWAYS be explicitly authorized by a co-located `ReferenceGrant` resource inside the target namespace containing the target service or secret.

Cross-namespace backend references (`parentRefs` or `backendRefs` across distinct namespaces) MUST ALWAYS be explicitly authorized by a co-located `ReferenceGrant` resource inside the target namespace containing the target service or secret.

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

Tenant HTTPRoutes MUST NOT bind to root paths (`path

Tenant HTTPRoutes MUST NOT bind to root paths (`path: { type: PathPrefix, value: '/' }`) unless explicitly validated against dedicated hostnames to prevent URI shadowing of other tenant routes.

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

Gateway listeners MUST enforce TLS termination or passthrough with tenant-scoped Secret references guarded by `ReferenceGrant` if certificates reside outside the Gateway control namespace.

Gateway listeners MUST enforce TLS termination or passthrough with tenant-scoped Secret references guarded by `ReferenceGrant` if certificates reside outside the Gateway control 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.
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 Route Attachment & Tenant Isolation critical for AI coding assistants?

Enforces strict cryptographic and architectural tenant isolation for Kubernetes Gateway API v1.2 by prohibiting global wildcard route attachments and unvalidated URI paths. Mandates namespace label selector boundaries on Gateway listeners, explicit ReferenceGrant resources for cross-namespace backend references, and strict tenant-scoped hostname matching to prevent malicious URI hijacking and route shadowing. 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 (Unrestricted Gateway Listener Attachment and Permissive Cross-Namespace Service Referencing) 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