Your EU Country List Is Missing Part of the EU

I shipped a WAF rule that tagged every request from the EEA and the UK so the app could decide whether to show a cookie consent banner. The country list was 31 codes long and every one of them was right. It still missed part of the EU, because some of the EU doesn’t geolocate as the country it belongs to.

This is an engineering post, not legal advice. Where the law matters below, I’m describing how I’d turn a requirement into a list, not what the requirement should be.

The setup

The requirement: a site with EU visitors has to show a consent banner to visitors from the EEA and the UK. The app could do its own IP lookup, but there’s already a WAF in front of it that geolocates every request, so the cheaper design is to let the edge decide and pass the answer along as a header.

AWS WAF can do that without blocking anything. A rule with a Count action doesn’t end evaluation, and it can carry custom_request_handling that inserts a header into the request before it reaches the origin:

rule {
  name     = "consent-eea-gb"
  priority = 0

  action {
    count {
      custom_request_handling {
        insert_header {
          name  = "consent-required"
          value = "true"
        }
      }
    }
  }

  statement {
    geo_match_statement {
      country_codes = local.consent_required_country_codes
    }
  }

  visibility_config {
    cloudwatch_metrics_enabled = true
    metric_name                = "consent-eea-gb"
    sampled_requests_enabled   = true
  }
}

WAF prefixes every header it inserts with x-amzn-waf-, so the app sees x-amzn-waf-consent-required: true.

Three more details made it usable:

The list itself was what you’d write from memory: the 27 member states, plus Iceland, Liechtenstein and Norway for the EEA, plus GB.

The dig

Writing the PR description, I went to note which ISO codes the list used, and that’s where it fell apart. AWS WAF geolocates with MaxMind and reports ISO 3166-1 alpha-2 codes. ISO assigns separate alpha-2 codes to a lot of places that are legally part of an EU member state.

The EU has nine outermost regions. They’re fully part of the EU, and EU law applies there with some adaptations. Six of them are French, and every one of those has its own ISO code:

Territory ISO code Geolocates as FR?
Réunion RE No
Guadeloupe GP No
Martinique MQ No
French Guiana GF No
Mayotte YT No
Saint-Martin (French part) MF No
Canary Islands none assigned Yes, as ES
Azores none (PT-20 subdivision) Yes, as PT
Madeira none (PT-30 subdivision) Yes, as PT

So a visitor in Réunion is in the EU, and the rule said they weren’t. A visitor in the Canary Islands is also in the EU, and the rule got them right, by accident, because the Canaries don’t have an assigned alpha-2 code of their own and MaxMind reports them as Spain.

That inconsistency is the trap. If none of the outermost regions had its own code, the country list would just work. Because it’s split, you can check the Canaries, see ES, decide the outlying regions are handled, and be wrong about six others.

The follow-up added RE, GP, MQ, GF, YT and MF once the person who owned the requirement confirmed they were in scope. It was a one-line change. Finding it was the hard part.

It’s not only the outermost regions

Once you start looking, there are more codes where “country” and “EU” disagree:

There’s one more trap in the data itself. MaxMind’s continent code EU means Europe, the continent, not the European Union. GeoNames puts Guernsey, Gibraltar and the Faroes on the EU continent, and Réunion in Africa. If you filter on continent, you get both errors at once. MaxMind also has an is_in_european_union flag, defined in terms of member states. I haven’t checked what it returns for RE, and I wouldn’t rely on it without checking.

The insight

The bug wasn’t a missing code. It was where the list came from. I built it from my mental picture of “the countries in the EU”, and that picture is a list of nation states. The requirement is really about where EU law applies, and that’s a different set. The database answering the question uses a third vocabulary, ISO territory codes, which matches neither.

So the list has to be derived from a spec written in the law’s terms (“EU law applies here”), then translated into the vocabulary the geolocation database actually returns, one territory at a time. And because the translation is where it goes wrong, it should be tested against the territories most likely to be translated wrong.

In Terraform, a precondition next to the rule is enough:

locals {
  consent_edge_cases = {
    RE = true, GP = true, MQ = true, GF = true, YT = true, MF = true,
    AX = true,
    BL = false, FO = false, GL = false,
  }
}

resource "aws_wafv2_rule_group" "consent_headers" {
  # ...

  lifecycle {
    precondition {
      condition = alltrue([
        for code, required in local.consent_edge_cases :
        contains(local.consent_required_country_codes, code) == required
      ])
      error_message = "Consent country list disagrees with consent_edge_cases."
    }
  }
}

The value isn’t the assertion, it’s the table. Each edge territory gets an explicit yes or no that a reviewer can argue with, instead of being silently absent. When someone later rebuilds the list from a fresh copy of “EU countries”, the plan fails. Put the codes you couldn’t decide on (Gibraltar, the Crown dependencies, Svalbard) in front of the person who owns the requirement, and add them to the table once they’ve answered.

Takeaways


← all writing