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-delegation-boundary

Strict Gateway API v1.2 Cross-Namespace HTTPRoute Delegation & ReferenceGrant Boundaries

Enforces strict RBAC and architectural separation between cluster operators managing Gateway resources and tenant teams managing HTTPRoutes. Mandates ReferenceGrant enforcement, explicit allowedNamespaces route selector boundaries on Gateway listeners, and HTTPRoute-to-HTTPRoute delegation invariants to prevent tenant route hijacking and ingress privilege escalation.

Prohibited Anti-Pattern: Permissive Gateway AllowedNamespaces Wildcards and Uncontrolled Cross-Namespace Backend References

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Strict Gateway API v1.2 Cross-Namespace HTTPRoute Delegation & ReferenceGrant Boundaries - Enforces strict RBAC and architectural separation between cluster operators managing Gateway resources and tenant teams managing HTTPRoutes. Mandates ReferenceGrant enforcement, explicit allowedNamespaces route selector boundaries on Gateway listeners, and HTTPRoute-to-HTTPRoute delegation invariants to prevent tenant route hijacking and ingress privilege escalation.
globs: deploy/**/*.yaml, deploy/**/*.yml, k8s/**/*.yaml, k8s/**/*.yml, helm/**/*.yaml, helm/**/*.yml, templates/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-v1-2-cross-namespace-delegation-boundary"
---

# Strict Gateway API v1.2 Cross-Namespace HTTPRoute Delegation & ReferenceGrant Boundaries

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Permissive Gateway AllowedNamespaces Wildcards and Uncontrolled Cross-Namespace Backend References

## System Constraints
You are a Principal Software Architect enforcing zero-trust multi-tenancy with Kubernetes Gateway API v1.2 (gateway.networking.k8s.io). You must mandate strict separation between infrastructure operators and application developers: Gateways MUST NOT bind to HTTPRoutes from arbitrary namespaces without explicit namespace label selectors or selector lists, HTTPRoutes targeting services across namespace boundaries MUST possess corresponding ReferenceGrant manifests in the target namespace, and root-to-child route delegation MUST restrict hostnames and path prefixes to prevent path-traversal route hijacking.

## Strict Guidelines
- Engineers MUST ALWAYS configure `allowedRoutes.namespaces.from: Selector` with strict `matchLabels` or `matchExpressions` on Gateway listeners instead of `from: All`.
- Engineers MUST NOT allow child HTTPRoutes to override hostnames specified by parent HTTPRoutes or Gateway listeners during HTTPRoute delegation.
- Code MUST deploy a `ReferenceGrant` resource in the target namespace whenever an `HTTPRoute` references a `backendRef` located outside its own namespace.
- Services MUST enforce HTTPRoute child delegation using rule-level `backendRefs` targeting `group: gateway.networking.k8s.io` and `kind: HTTPRoute` with explicit path prefixes.
- Components NEVER attach child routes to root routes without validating that the delegated path prefix is bounded and non-overlapping with cluster-level admin endpoints.

## Bad vs Good Implementations
### Insecure Wildcard Gateway Binding vs Production-Grade Cross-Namespace Delegation Boundary

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The Gateway configuration uses `from: All`, enabling any namespace tenant to hijack apex/admin hostnames. Additionally, the HTTPRoute directly references a Service in another namespace (`internal-system`) without an authorizing ReferenceGrant, violating cross-namespace isolation.
```yaml
# INSECURE: Wildcard route attachment allows any namespace to attach routes
# to root hostnames and route across namespaces without authorization grants.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-gateway
  namespace: infra-system
spec:
  gatewayClassName: istio
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      hostname: "*.enterprise.com"
      allowedRoutes:
        namespaces:
          from: All
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: app-route
  namespace: team-alpha
spec:
  parentRefs:
    - name: shared-gateway
      namespace: infra-system
  hostnames:
    - "admin.enterprise.com" # PRIVILEGE ESCALATION: Claiming unauthorized root domain
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: core-db-service
          namespace: internal-system # UNAUTHORIZED CROSS-NAMESPACE ACCESS
          port: 5432
```

#### Production Standard (Correct)
> **Standard**: Enforces least-privilege tenant boundary: the Gateway restricts listeners via namespace label selectors, uses root-to-child route delegation restricting the tenant to `/api/v1`, and explicitly authorizes cross-namespace backend routing via a `ReferenceGrant` deployed in the destination namespace.
```yaml
# SECURE: Gateway binds only to verified namespaces, uses Root HTTPRoute delegation,
# and mandates ReferenceGrant for verified cross-namespace service references.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-gateway
  namespace: infra-system
spec:
  gatewayClassName: istio
  listeners:
    - name: https-team-alpha
      protocol: HTTPS
      port: 443
      hostname: "alpha.enterprise.com"
      allowedRoutes:
        namespaces:
          from: Selector
          selector:
            matchLabels:
              gateway.enterprise.com/tenant: team-alpha
---
# Root Route (Operator Controlled)
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: alpha-root-route
  namespace: infra-system
spec:
  parentRefs:
    - name: shared-gateway
  hostnames:
    - "alpha.enterprise.com"
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/v1
      backendRefs:
        - group: gateway.networking.k8s.io
          kind: HTTPRoute
          name: alpha-child-route
          namespace: team-alpha
---
# Child Route (Tenant Controlled - Delegated Scope)
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: alpha-child-route
  namespace: team-alpha
spec:
  parentRefs:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: alpha-root-route
      namespace: infra-system
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/v1/orders
      backendRefs:
        - name: orders-service
          namespace: shared-backends
          port: 8080
---
# ReferenceGrant authorizing cross-namespace reference
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-alpha-to-shared
  namespace: shared-backends
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: team-alpha
  to:
    - group: ""
      kind: Service
      name: orders-service
```

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: Strict Gateway API v1.2 Cross-Namespace HTTPRoute Delegation & ReferenceGrant Boundaries

Strict Gateway API v1.2 Cross-Namespace HTTPRoute Delegation & ReferenceGrant Boundaries defines an industrial-grade behavioral standard for devops engineering. Enforces strict RBAC and architectural separation between cluster operators managing Gateway resources and tenant teams managing HTTPRoutes. Mandates ReferenceGrant enforcement, explicit allowedNamespaces route selector boundaries on Gateway listeners, and HTTPRoute-to-HTTPRoute delegation invariants to prevent tenant route hijacking and ingress privilege escalation. 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: Permissive Gateway AllowedNamespaces Wildcards and Uncontrolled Cross-Namespace Backend References 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 DriftPermissive Gateway AllowedNamespaces Wildcards and Uncontrolled Cross-Namespace Backend ReferencesCritical
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 configure `allowedRoutes.namespaces.from

Engineers MUST ALWAYS configure `allowedRoutes.namespaces.from: Selector` with strict `matchLabels` or `matchExpressions` on Gateway listeners instead of `from: All`.

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 allow child HTTPRoutes to override hostnames specified by parent HTTPRoutes or Gateway listeners during HTTPRoute delegation.

Engineers MUST NOT allow child HTTPRoutes to override hostnames specified by parent HTTPRoutes or Gateway listeners during HTTPRoute delegation.

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 deploy a `ReferenceGrant` resource in the target namespace whenever an `HTTPRoute` references a `backendRef` located outside its own namespace.

Code MUST deploy a `ReferenceGrant` resource in the target namespace whenever an `HTTPRoute` references a `backendRef` located outside its own 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 HTTPRoute child delegation using rule-level `backendRefs` targeting `group

Services MUST enforce HTTPRoute child delegation using rule-level `backendRefs` targeting `group: gateway.networking.k8s.io` and `kind: HTTPRoute` with explicit path prefixes.

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 attach child routes to root routes without validating that the delegated path prefix is bounded and non-overlapping with cluster-level admin endpoints.

Components NEVER attach child routes to root routes without validating that the delegated path prefix is bounded and non-overlapping with cluster-level admin endpoints.

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 Strict Gateway API v1.2 Cross-Namespace HTTPRoute Delegation & ReferenceGrant Boundaries critical for AI coding assistants?

Enforces strict RBAC and architectural separation between cluster operators managing Gateway resources and tenant teams managing HTTPRoutes. Mandates ReferenceGrant enforcement, explicit allowedNamespaces route selector boundaries on Gateway listeners, and HTTPRoute-to-HTTPRoute delegation invariants to prevent tenant route hijacking and ingress privilege escalation. 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 (Permissive Gateway AllowedNamespaces Wildcards and Uncontrolled Cross-Namespace Backend References) 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

Strict