Agent Wranglers

INF-34Required

Require tokens for the metadata service

Make every cloud server's metadata service refuse requests that lack the provider's session token or required header. Where the provider lets you limit how far the answer travels, allow one network hop, or two for containers on the host.

How to do it

The metadata address hands out the machine's credentials. A server-side request forgery hole can read it with a plain GET. On AWS, require IMDSv2 with http_tokens = "required" and a hop limit of 1. Use 2 when containers on the host need it. Google Cloud and Azure already require a special header, Metadata-Flavor: Google or Metadata: true. Set the account default in every region too, so new servers start safe.

Examples

Terraform example

resource "aws_instance" "web" {
  ami           = data.aws_ami.web.id
  instance_type = "t4g.small"
  metadata_options {
    http_tokens                 = "required"
    http_put_response_hop_limit = 1
  }
}

Terraform example 2

# The default for every new server in this region
resource "aws_ec2_instance_metadata_defaults" "baseline" {
  http_tokens                 = "required"
  http_put_response_hop_limit = 1
}

Shell example

aws ec2 describe-instances --query 'Reservations[].Instances[?MetadataOptions.HttpTokens==`optional`].InstanceId' --output text   # empty
curl -s -o /dev/null -w '%{http_code}\n' http://169.254.169.254/latest/meta-data/   # 401

How to check

From every server, a plain GET to the metadata address, such as 169.254.169.254, without the token or header is refused. On AWS, the query below lists no instance with tokens optional, and no instance has a hop limit above 2.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/networking-and-dns/metadata-service-tokens">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.