Anthropic/OpenAI: Add country codes for territories (#17089)

- Cloudflare provides ISO-3166-1 country code for protectorates. Expand our allowlist to include the territories of countries on the allowlist (US, UK, France, Australia, New Zealand). 
- Also include the country_code in the error message when we block. 

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
Peter Tripp 2024-08-29 15:32:29 +00:00 committed by GitHub
parent 7db8d80c30
commit 4d6bb52d1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 2 deletions

View File

@ -15,8 +15,10 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
vec![ vec![
"AL", // Albania "AL", // Albania
"DZ", // Algeria "DZ", // Algeria
"AS", // American Samoa (US)
"AD", // Andorra "AD", // Andorra
"AO", // Angola "AO", // Angola
"AI", // Anguilla (UK)
"AG", // Antigua and Barbuda "AG", // Antigua and Barbuda
"AR", // Argentina "AR", // Argentina
"AM", // Armenia "AM", // Armenia
@ -30,11 +32,13 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"BE", // Belgium "BE", // Belgium
"BZ", // Belize "BZ", // Belize
"BJ", // Benin "BJ", // Benin
"BM", // Bermuda (UK)
"BT", // Bhutan "BT", // Bhutan
"BO", // Bolivia "BO", // Bolivia
"BA", // Bosnia and Herzegovina "BA", // Bosnia and Herzegovina
"BW", // Botswana "BW", // Botswana
"BR", // Brazil "BR", // Brazil
"IO", // British Indian Ocean Territory (UK)
"BN", // Brunei "BN", // Brunei
"BG", // Bulgaria "BG", // Bulgaria
"BF", // Burkina Faso "BF", // Burkina Faso
@ -43,11 +47,15 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"KH", // Cambodia "KH", // Cambodia
"CM", // Cameroon "CM", // Cameroon
"CA", // Canada "CA", // Canada
"KY", // Cayman Islands (UK)
"TD", // Chad "TD", // Chad
"CL", // Chile "CL", // Chile
"CX", // Christmas Island (AU)
"CC", // Cocos (Keeling) Islands (AU)
"CO", // Colombia "CO", // Colombia
"KM", // Comoros "KM", // Comoros
"CG", // Congo (Brazzaville) "CG", // Congo (Brazzaville)
"CK", // Cook Islands (NZ)
"CR", // Costa Rica "CR", // Costa Rica
"CI", // Côte d'Ivoire "CI", // Côte d'Ivoire
"HR", // Croatia "HR", // Croatia
@ -63,21 +71,28 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"GQ", // Equatorial Guinea "GQ", // Equatorial Guinea
"EE", // Estonia "EE", // Estonia
"SZ", // Eswatini "SZ", // Eswatini
"FK", // Falkland Islands (UK)
"FJ", // Fiji "FJ", // Fiji
"FI", // Finland "FI", // Finland
"FR", // France "FR", // France
"GF", // French Guiana (FR)
"PF", // French Polynesia (FR)
"TF", // French Southern Territories
"GA", // Gabon "GA", // Gabon
"GM", // Gambia "GM", // Gambia
"GE", // Georgia "GE", // Georgia
"DE", // Germany "DE", // Germany
"GH", // Ghana "GH", // Ghana
"GI", // Gibraltar (UK)
"GR", // Greece "GR", // Greece
"GD", // Grenada "GD", // Grenada
"GT", // Guatemala "GT", // Guatemala
"GU", // Guam (US)
"GN", // Guinea "GN", // Guinea
"GW", // Guinea-Bissau "GW", // Guinea-Bissau
"GY", // Guyana "GY", // Guyana
"HT", // Haiti "HT", // Haiti
"HM", // Heard Island and McDonald Islands (AU)
"HN", // Honduras "HN", // Honduras
"HU", // Hungary "HU", // Hungary
"IS", // Iceland "IS", // Iceland
@ -116,6 +131,7 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"MD", // Moldova "MD", // Moldova
"MC", // Monaco "MC", // Monaco
"MN", // Mongolia "MN", // Mongolia
"MS", // Montserrat (UK)
"ME", // Montenegro "ME", // Montenegro
"MA", // Morocco "MA", // Morocco
"MZ", // Mozambique "MZ", // Mozambique
@ -126,8 +142,11 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"NZ", // New Zealand "NZ", // New Zealand
"NE", // Niger "NE", // Niger
"NG", // Nigeria "NG", // Nigeria
"NF", // Norfolk Island (AU)
"MK", // North Macedonia "MK", // North Macedonia
"MI", // Northern Mariana Islands (UK)
"NO", // Norway "NO", // Norway
"NU", // Niue (NZ)
"OM", // Oman "OM", // Oman
"PK", // Pakistan "PK", // Pakistan
"PW", // Palau "PW", // Palau
@ -137,13 +156,18 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"PY", // Paraguay "PY", // Paraguay
"PE", // Peru "PE", // Peru
"PH", // Philippines "PH", // Philippines
"PN", // Pitcairn (UK)
"PL", // Poland "PL", // Poland
"PT", // Portugal "PT", // Portugal
"PR", // Puerto Rico (US)
"QA", // Qatar "QA", // Qatar
"RO", // Romania "RO", // Romania
"RW", // Rwanda "RW", // Rwanda
"BL", // Saint Barthélemy (FR)
"KN", // Saint Kitts and Nevis "KN", // Saint Kitts and Nevis
"LC", // Saint Lucia "LC", // Saint Lucia
"MF", // Saint Martin (FR)
"PM", // Saint Pierre and Miquelon (FR)
"VC", // Saint Vincent and the Grenadines "VC", // Saint Vincent and the Grenadines
"WS", // Samoa "WS", // Samoa
"SM", // San Marino "SM", // San Marino
@ -152,6 +176,7 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"SN", // Senegal "SN", // Senegal
"RS", // Serbia "RS", // Serbia
"SC", // Seychelles "SC", // Seychelles
"SH", // Saint Helena, Ascension and Tristan da Cunha (UK)
"SL", // Sierra Leone "SL", // Sierra Leone
"SG", // Singapore "SG", // Singapore
"SK", // Slovakia "SK", // Slovakia
@ -170,22 +195,28 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"TH", // Thailand "TH", // Thailand
"TL", // Timor-Leste "TL", // Timor-Leste
"TG", // Togo "TG", // Togo
"TK", // Tokelau (NZ)
"TO", // Tonga "TO", // Tonga
"TT", // Trinidad and Tobago "TT", // Trinidad and Tobago
"TN", // Tunisia "TN", // Tunisia
"TR", // Türkiye (Turkey) "TR", // Türkiye (Turkey)
"TM", // Turkmenistan "TM", // Turkmenistan
"TC", // Turks and Caicos Islands (UK)
"TV", // Tuvalu "TV", // Tuvalu
"UG", // Uganda "UG", // Uganda
"UA", // Ukraine (except Crimea, Donetsk, and Luhansk regions) "UA", // Ukraine (except Crimea, Donetsk, and Luhansk regions)
"AE", // United Arab Emirates "AE", // United Arab Emirates
"GB", // United Kingdom "GB", // United Kingdom
"UM", // United States Minor Outlying Islands (US)
"US", // United States of America "US", // United States of America
"UY", // Uruguay "UY", // Uruguay
"UZ", // Uzbekistan "UZ", // Uzbekistan
"VU", // Vanuatu "VU", // Vanuatu
"VA", // Vatican City "VA", // Vatican City
"VN", // Vietnam "VN", // Vietnam
"VI", // Virgin Islands (US)
"VG", // Virgin Islands (UK)
"WF", // Wallis and Futuna (FR)
"ZM", // Zambia "ZM", // Zambia
"ZW", // Zimbabwe "ZW", // Zimbabwe
] ]

View File

@ -85,7 +85,9 @@ fn authorize_access_for_country(
if !is_country_supported_by_provider { if !is_country_supported_by_provider {
Err(Error::http( Err(Error::http(
StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS, StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS,
format!("access to {provider:?} models is not available in your region"), format!(
"access to {provider:?} models is not available in your region ({country_code})"
),
))? ))?
} }
@ -195,7 +197,7 @@ mod tests {
.to_vec(); .to_vec();
assert_eq!( assert_eq!(
String::from_utf8(response_body).unwrap(), String::from_utf8(response_body).unwrap(),
format!("access to {provider:?} models is not available in your region") format!("access to {provider:?} models is not available in your region ({country_code})")
); );
} }
} }

View File

@ -15,6 +15,8 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
vec![ vec![
"AL", // Albania "AL", // Albania
"DZ", // Algeria "DZ", // Algeria
"AS", // American Samoa (US)
"AI", // Anguilla (UK)
"AF", // Afghanistan "AF", // Afghanistan
"AD", // Andorra "AD", // Andorra
"AO", // Angola "AO", // Angola
@ -31,11 +33,13 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"BE", // Belgium "BE", // Belgium
"BZ", // Belize "BZ", // Belize
"BJ", // Benin "BJ", // Benin
"BM", // Bermuda (UK)
"BT", // Bhutan "BT", // Bhutan
"BO", // Bolivia "BO", // Bolivia
"BA", // Bosnia and Herzegovina "BA", // Bosnia and Herzegovina
"BW", // Botswana "BW", // Botswana
"BR", // Brazil "BR", // Brazil
"IO", // British Indian Ocean Territory (UK)
"BN", // Brunei "BN", // Brunei
"BG", // Bulgaria "BG", // Bulgaria
"BF", // Burkina Faso "BF", // Burkina Faso
@ -44,13 +48,17 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"KH", // Cambodia "KH", // Cambodia
"CM", // Cameroon "CM", // Cameroon
"CA", // Canada "CA", // Canada
"KY", // Cayman Islands (UK)
"CF", // Central African Republic "CF", // Central African Republic
"TD", // Chad "TD", // Chad
"CL", // Chile "CL", // Chile
"CX", // Christmas Island (AU)
"CC", // Cocos (Keeling) Islands (AU)
"CO", // Colombia "CO", // Colombia
"KM", // Comoros "KM", // Comoros
"CG", // Congo (Brazzaville) "CG", // Congo (Brazzaville)
"CD", // Congo (DRC) "CD", // Congo (DRC)
"CK", // Cook Islands (NZ)
"CR", // Costa Rica "CR", // Costa Rica
"CI", // Côte d'Ivoire "CI", // Côte d'Ivoire
"HR", // Croatia "HR", // Croatia
@ -68,21 +76,28 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"EE", // Estonia "EE", // Estonia
"SZ", // Eswatini (Swaziland) "SZ", // Eswatini (Swaziland)
"ET", // Ethiopia "ET", // Ethiopia
"FK", // Falkland Islands (UK)
"FJ", // Fiji "FJ", // Fiji
"FI", // Finland "FI", // Finland
"FR", // France "FR", // France
"GF", // French Guiana (FR)
"PF", // French Polynesia (FR)
"TF", // French Southern Territories
"GA", // Gabon "GA", // Gabon
"GM", // Gambia "GM", // Gambia
"GE", // Georgia "GE", // Georgia
"DE", // Germany "DE", // Germany
"GH", // Ghana "GH", // Ghana
"GI", // Gibraltar (UK)
"GR", // Greece "GR", // Greece
"GD", // Grenada "GD", // Grenada
"GT", // Guatemala "GT", // Guatemala
"GU", // Guam (US)
"GN", // Guinea "GN", // Guinea
"GW", // Guinea-Bissau "GW", // Guinea-Bissau
"GY", // Guyana "GY", // Guyana
"HT", // Haiti "HT", // Haiti
"HM", // Heard Island and McDonald Islands (AU)
"VA", // Holy See (Vatican City) "VA", // Holy See (Vatican City)
"HN", // Honduras "HN", // Honduras
"HU", // Hungary "HU", // Hungary
@ -124,6 +139,7 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"MD", // Moldova "MD", // Moldova
"MC", // Monaco "MC", // Monaco
"MN", // Mongolia "MN", // Mongolia
"MS", // Montserrat (UK)
"ME", // Montenegro "ME", // Montenegro
"MA", // Morocco "MA", // Morocco
"MZ", // Mozambique "MZ", // Mozambique
@ -136,8 +152,11 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"NI", // Nicaragua "NI", // Nicaragua
"NE", // Niger "NE", // Niger
"NG", // Nigeria "NG", // Nigeria
"NF", // Norfolk Island (AU)
"MK", // North Macedonia "MK", // North Macedonia
"MI", // Northern Mariana Islands (UK)
"NO", // Norway "NO", // Norway
"NU", // Niue (NZ)
"OM", // Oman "OM", // Oman
"PK", // Pakistan "PK", // Pakistan
"PW", // Palau "PW", // Palau
@ -147,13 +166,18 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"PY", // Paraguay "PY", // Paraguay
"PE", // Peru "PE", // Peru
"PH", // Philippines "PH", // Philippines
"PN", // Pitcairn (UK)
"PL", // Poland "PL", // Poland
"PT", // Portugal "PT", // Portugal
"PR", // Puerto Rico (US)
"QA", // Qatar "QA", // Qatar
"RO", // Romania "RO", // Romania
"RW", // Rwanda "RW", // Rwanda
"BL", // Saint Barthélemy (FR)
"KN", // Saint Kitts and Nevis "KN", // Saint Kitts and Nevis
"LC", // Saint Lucia "LC", // Saint Lucia
"MF", // Saint Martin (FR)
"PM", // Saint Pierre and Miquelon (FR)
"VC", // Saint Vincent and the Grenadines "VC", // Saint Vincent and the Grenadines
"WS", // Samoa "WS", // Samoa
"SM", // San Marino "SM", // San Marino
@ -162,6 +186,7 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"SN", // Senegal "SN", // Senegal
"RS", // Serbia "RS", // Serbia
"SC", // Seychelles "SC", // Seychelles
"SH", // Saint Helena, Ascension and Tristan da Cunha (UK)
"SL", // Sierra Leone "SL", // Sierra Leone
"SG", // Singapore "SG", // Singapore
"SK", // Slovakia "SK", // Slovakia
@ -183,21 +208,27 @@ static SUPPORTED_COUNTRIES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
"TH", // Thailand "TH", // Thailand
"TL", // Timor-Leste (East Timor) "TL", // Timor-Leste (East Timor)
"TG", // Togo "TG", // Togo
"TK", // Tokelau (NZ)
"TO", // Tonga "TO", // Tonga
"TT", // Trinidad and Tobago "TT", // Trinidad and Tobago
"TN", // Tunisia "TN", // Tunisia
"TR", // Turkey "TR", // Turkey
"TM", // Turkmenistan "TM", // Turkmenistan
"TC", // Turks and Caicos Islands (UK)
"TV", // Tuvalu "TV", // Tuvalu
"UG", // Uganda "UG", // Uganda
"UA", // Ukraine (with certain exceptions) "UA", // Ukraine (with certain exceptions)
"AE", // United Arab Emirates "AE", // United Arab Emirates
"GB", // United Kingdom "GB", // United Kingdom
"UM", // United States Minor Outlying Islands (US)
"US", // United States of America "US", // United States of America
"UY", // Uruguay "UY", // Uruguay
"UZ", // Uzbekistan "UZ", // Uzbekistan
"VU", // Vanuatu "VU", // Vanuatu
"VN", // Vietnam "VN", // Vietnam
"VI", // Virgin Islands (US)
"VG", // Virgin Islands (UK)
"WF", // Wallis and Futuna (FR)
"YE", // Yemen "YE", // Yemen
"ZM", // Zambia "ZM", // Zambia
"ZW", // Zimbabwe "ZW", // Zimbabwe