OWASP · · 5 min read

SSRF Attack GCP Metadata Service Prevention: Securing Your Workloads

Mitigate Server-Side Request Forgery (SSRF) risks targeting the GCP metadata service, preventing credential exfiltration and unauthorized resource access within your Google Cloud environment.

Key Takeaways

  • Restrict outbound network access from application instances using VPC Firewall rules to deny egress to `169.254.169.254`.
  • Implement Workload Identity for GKE pods to ensure service account tokens are scoped precisely, minimizing the blast radius of a compromised instance.
  • Validate all user-supplied URLs and headers, specifically checking for the `X-Google-Metadata-Request` header and denying requests to internal IP ranges.

runred.ai connects application source code with live GCP infrastructure context to discover vulnerabilities with contextual severity, generate exploit-confirming integration tests, and produce immutable audit evidence. Server-Side Request Forgery (SSRF) attacks pose a significant threat to cloud environments by enabling attackers to force a server to make requests to arbitrary internal resources. On Google Cloud Platform (GCP), this often targets the instance metadata service, accessible at the link-local IP address 169.254.169.254. This service provides critical instance information, including project IDs, zone details, and, most critically, short-lived service account credentials. Effective SSRF attack GCP metadata service prevention is paramount for maintaining the security and compliance posture of your cloud infrastructure.

Understanding the GCP Metadata Service as an Attack Vector

The GCP metadata service is an essential component for instances running on Compute Engine, GKE, and other services. It allows applications to query information about the instance itself without requiring explicit authentication. For example, an application can retrieve its associated service account's access token by making an HTTP request to http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token. This token can then be used to authenticate to other GCP services, such as Cloud Storage, Cloud SQL, or even to manage IAM policies, depending on the service account's permissions.

An SSRF vulnerability in a web application allows an attacker to control the URL that the server requests. If an attacker can inject http://169.254.169.254/... into a vulnerable parameter, the server will fetch the metadata endpoint's response and potentially return it to the attacker. This enables the exfiltration of sensitive data, including service account credentials, which can lead to privilege escalation, data theft, and unauthorized resource manipulation. The CVSS score for such an exploit can range from 7.5 (High) for credential disclosure to 9.8 (Critical) if it leads to full system compromise.

Implementing Robust SSRF Attack GCP Metadata Service Prevention

Preventing SSRF attacks targeting the GCP metadata service requires a multi-layered approach, combining network controls, application-level validation, and robust identity management.

Network-Level Controls

The most direct way to prevent unauthorized access to the metadata service is through network segmentation. Your engineering teams should implement VPC Firewall rules or GKE Network Policies to restrict outbound traffic from application instances to the metadata service IP address.

  • VPC Firewall Rules: Create egress deny rules for specific subnets or instances that do not legitimately require metadata service access. For example, to deny egress to 169.254.169.254 from instances tagged no-metadata-access:
    gcloud compute firewall-rules create deny-metadata-egress \
        --network=your-vpc-network \
        --action=DENY \
        --direction=EGRESS \
        --destination-ranges=169.254.169.254 \
        --priority=1000 \
        --target-tags=no-metadata-access
  • GKE Network Policies: For Kubernetes workloads, define Network Policies that restrict egress from pods. A policy can deny all egress to 169.254.169.254 for specific namespaces or labels.

Application-Level Validation

At the application layer, rigorous input validation is critical. Any user-supplied URL or hostname that your application processes must be validated to ensure it does not point to internal resources. This includes:

  • URL Parsing: Parse and validate all components of a URL (scheme, host, port) before making a request. Deny requests to private IP ranges (e.g., 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, and specifically 169.254.169.254).
  • Header Validation: The GCP metadata service requires the X-Google-Metadata-Request: True header for requests made from outside the instance. While this header is not a security control in itself, its absence can sometimes be used to identify legitimate internal requests versus external ones. Your application should never forward this header from user input.

Identity and Access Management (IAM)

Even with network and application controls, a robust IAM strategy is essential. Implement the principle of least privilege for all service accounts. Specifically, for GKE workloads, leverage Workload Identity:

  • Workload Identity: This feature allows Kubernetes service accounts to act as IAM service accounts, providing fine-grained permissions to individual pods. By binding a Kubernetes service account to a specific IAM service account with minimal permissions (e.g., roles/storage.objectViewer instead of roles/editor), the blast radius of a compromised instance is significantly reduced. Even if an attacker obtains a token via SSRF, its limited scope prevents broader unauthorized access.
    gcloud iam service-accounts create my-app-sa --project=your-project-id
    gcloud iam service-accounts add-iam-policy-binding \
        my-app-sa@your-project-id.iam.gserviceaccount.com \
        --role="roles/iam.workloadIdentityUser" \
        --member="serviceAccount:your-project-id.svc.id.goog[your-namespace/kubernetes-sa]"

runred.ai assists engineering teams by automatically identifying potential SSRF vulnerabilities in your application code and correlating them with your GCP infrastructure context. This allows runred.ai to determine if a vulnerable code path could indeed reach the metadata service, assess the contextual severity based on the associated service account's permissions, and then generate integration tests that first confirm the exploit and subsequently verify that your patch effectively closes the vulnerability. All remediation steps and verification results are logged as immutable audit evidence to Cloud Logging, streamlining compliance for frameworks like NIS2 and SOC2 Type II.

Frequently Asked Questions

What specific data can an attacker exfiltrate via SSRF to the GCP metadata service?

An attacker can exfiltrate instance identity tokens, service account access tokens (e.g., from /computeMetadata/v1/instance/service-accounts/default/token), project IDs, zone information, and custom metadata. The most critical exfiltration is the service account token, which can grant access to other GCP resources based on the service account's IAM roles.

How does Workload Identity specifically mitigate SSRF risks compared to traditional service accounts?

Workload Identity mitigates SSRF by ensuring that the service account token available to a compromised pod has only the minimum necessary permissions for that specific application. Unlike traditional Compute Engine default service accounts which often have broad permissions, Workload Identity scopes permissions to a Kubernetes service account, significantly reducing the blast radius if an SSRF vulnerability leads to token exfiltration.

Are there any legitimate reasons for an application to access 169.254.169.254? How do we handle those?

Yes, applications legitimately access the metadata service to retrieve instance details, project IDs, or short-lived credentials for authenticating to other GCP services. For such cases, network policies must be carefully crafted to permit only the necessary egress from specific, trusted applications. Additionally, these applications should implement robust internal validation and ensure that any user-supplied input cannot influence requests to the metadata service.

Prevent SSRF Credential Theft on GCP

runred.ai automates the discovery and remediation of critical infrastructure vulnerabilities like SSRF, ensuring your GCP environment remains secure and compliant.

Apply for Private Enterprise Beta
← Back to all posts