Summary
A validation flaw in ingress-nginx allows the nginx.ingress.kubernetes.io/rewrite-target annotation to inject attacker-controlled configuration fragments into the generated nginx config.
Under certain configurations, this can lead to arbitrary code execution within the ingress-nginx controller pod and exposure of Kubernetes Secrets that the controller can access, which may include all cluster-wide Secrets in default setups.
Affected context
This vulnerability affects Kubernetes clusters using the ingress-nginx controller where:
- Ingress objects are created or modified by users or services that may not be fully trusted.
- The
nginx.ingress.kubernetes.io/rewrite-targetannotation is honored and not restricted. - The ingress-nginx controller has broad permissions to read Kubernetes Secrets (commonly all Secrets in the cluster by default).
The issue arises during the translation of Ingress resources into nginx configuration, where annotation values are merged into the config without sufficiently strict validation of allowed content.
Exploit preconditions
- Attack surface: Kubernetes API surface for creating or updating Ingress resources using ingress-nginx.
- Required access: Ability to create, update, or control Ingress objects in a namespace serviced by the ingress-nginx controller (via Kubernetes credentials, compromised CI/CD, or misconfigured multi-tenancy).
- Trigger point: Application of an Ingress manifest that uses a malicious
nginx.ingress.kubernetes.io/rewrite-targetvalue, which the controller then renders into the nginx configuration and reloads. - Why exploitation is possible: Annotation values are treated as configuration snippets without robust syntactic and semantic validation, allowing an attacker to escape the intended value context and inject arbitrary nginx directives that can execute commands or exfiltrate data available to the controller.
Why this happens
The root cause is improper input validation (CWE-20) of user-controlled configuration metadata in Kubernetes Ingress resources.
The ingress-nginx controller takes the nginx.ingress.kubernetes.io/rewrite-target annotation, assumes it is a benign path rewrite value, and interpolates it into nginx configuration templates.
Because the content is not constrained to a strict grammar or escaped safely, attackers can craft values that break out of the expected directive value and introduce new nginx directives or configuration blocks.
This configuration injection can be chained into command execution (for example, via nginx modules or misused directives) and makes it possible to read or forward Kubernetes Secrets that are accessible to the controller.
Abstractly, this is a configuration injection flaw stemming from mixing untrusted metadata with privileged infrastructure configuration without enforcing a minimal, well-specified input format.
Potential impact
- Remote code execution in the security context of the ingress-nginx controller pod.
- Unauthorized reading and exfiltration of Kubernetes Secrets that the controller can access (potentially all cluster-wide Secrets).
- Lateral movement by using exposed credentials (e.g., cloud provider keys, database passwords, service account tokens).
- Compromise of applications behind the ingress via tampered routing, header injection, or malicious response handling.
- Disruption of traffic by forcing misconfiguration, denial-of-service, or redirecting requests to attacker-controlled endpoints.
General mitigation guidance
Mitigation focuses on tightening the trust boundary around Ingress metadata and constraining how annotations are processed into configuration:
- Constrain annotation semantics: Treat
nginx.ingress.kubernetes.io/rewrite-targetas data, not config; accept only safe patterns (e.g., validated URL paths) and reject or normalize anything that does not match a strict regex or schema. - Introduce strict templating boundaries: Ensure that annotation values cannot break out of their intended configuration context by robust quoting, escaping, and avoiding direct concatenation of untrusted strings into configuration directives.
- Harden RBAC and multi-tenancy: Limit who can create or modify Ingress resources, especially in shared clusters; avoid granting cluster-wide write access to Ingress in multi-tenant environments.
- Reduce controller privileges: Apply least-privilege to the ingress-nginx controller's Kubernetes permissions, restricting it to only the Secrets it truly needs rather than all cluster Secrets.
- Use policy enforcement layers: Apply admission controllers or policy engines (e.g., validating webhooks, policy-as-code) to disallow dangerous or overly flexible annotations in Ingress manifests.
- Segregate critical workloads: Run highly sensitive applications behind separate ingress controllers or in isolated clusters where annotation-driven configuration is more tightly controlled.
How teams detect issues like this
Effective detection requires both code-centric and cluster-centric approaches:
- Static analysis and code review: Examine ingress-nginx code and Helm templates for patterns where annotation values are interpolated directly into nginx configuration without strong validation or escaping; flag any string concatenation into config templates.
- Configuration and policy checks: Use admission controllers or policy tools to scan Ingress manifests for suspicious
nginx.ingress.kubernetes.io/rewrite-targetvalues (e.g., containing newline characters, semicolons, or directive-like tokens). - Runtime configuration inspection: Periodically inspect rendered nginx configuration in controller pods to detect unexpected directives or anomalous blocks that can be traced back to specific Ingress resources.
- Log monitoring and anomaly detection: Monitor ingress-nginx logs and Kubernetes audit logs for unusual Ingress creation patterns, frequent config reloads, or anomalous requests that might indicate configuration injection attempts.
- Automated security review: Integrate automated configuration linters and policy-as-code tools into CI/CD for Kubernetes manifests to prevent unsafe annotation usage before deployment.

