/writing/your-eu-country-list-is-missing-part-of-the-eu

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:

  • A second header for “we looked”. A geo match statement labels every request it inspects with awswaf:clientip:geo:country:<code>, match or not, and uses XX when it can’t place the IP. A follow-up rule that matches those labels inserts geo-checked: true. Without it, a missing consent header means either “outside the EEA” or “location unknown”, and the app can’t tell which. With it, the app can fail towards showing the banner. One catch: the obvious way to write that rule, a NAMESPACE match on awswaf:clientip:geo:country:, also matches the XX label, so “we looked and found nothing” counts as checked. Exclude XX explicitly with a not_statement.
  • Strip what the client sends. WAF only overwrites a header with the same name when its rule actually inserts it. Anyone can send x-amzn-waf-geo-checked themselves, and AWS suggests a rule that blocks incoming headers with that prefix.
  • Opt-in per environment. The rule group sits behind a module flag that defaults to off. The shared module change deploys nothing; each environment turns it on in its own change, pre-production first. The headers land on every host behind that web ACL, not just the app that reads them, so check what else shares it.

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:

  • Åland (AX) is part of Finland and joined the EU with it, but it has its own alpha-2 code. GeoNames, which MaxMind keys its locations to, lists it as a separate country. An EU-27 list built from member state names misses it.
  • Saint Barthélemy (BL) was an outermost region until 2012 and is now an overseas territory outside the EU. Copy an old list and it’s in; it shouldn’t be.
  • Faroe Islands (FO) and Greenland (GL) belong to Denmark and are outside the EU.
  • Ceuta and Melilla are part of the EU. ISO only reserves EA for them, and GeoNames has no separate entry, so I’d expect them to come back as ES. I haven’t confirmed that against a real lookup.
  • Svalbard (SJ) is Norwegian, but Norway exempted it from the EEA Agreement. Whether that matters for your requirement is a question for whoever owns the requirement.
  • Gibraltar (GI), Jersey (JE), Guernsey (GG) and Isle of Man (IM) are not GB. If the spec says “the UK”, someone has to decide whether it means these too.

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

  • ISO 3166-1 gives separate codes to many places that are legally part of an EU state. Geolocation reports those codes, not the parent country.
  • It’s inconsistent: the French outermost regions have their own codes, while the Canaries, Azores and Madeira come back as ES or PT. Checking one tells you nothing about the others.
  • Write the spec in the law’s terms, then translate it into territory codes. Don’t start from a list of countries.
  • Keep a table of edge territories with an explicit answer for each one, and make the build fail when the list disagrees with it.
  • Emit a “location checked” signal alongside the result, and make sure XX doesn’t count as checked.