Skip to content

feat: geolocation controller fix#8137

Merged
MarioAslau merged 3 commits intomainfrom
feat/geolocation-controller-correct-countrycode
Mar 6, 2026
Merged

feat: geolocation controller fix#8137
MarioAslau merged 3 commits intomainfrom
feat/geolocation-controller-correct-countrycode

Conversation

@MarioAslau
Copy link
Contributor

@MarioAslau MarioAslau commented Mar 6, 2026

Explanation

The /geolocation API endpoint returns ISO 3166-2 subdivision codes (e.g. US-NY, CA-ON, GB), not ISO 3166-1 alpha-2 country-only codes (e.g. US, CA, GB).

The GeolocationApiService.#performFetch() validation regex was /^[A-Z]{2}$/u, which only accepts exactly 2 uppercase letters. Since the API returns values like US-NY, the regex rejects every valid response and the controller always stores UNKNOWN.

This causes all downstream consumers to receive UNKNOWN as the geolocation:

  • Perps: Geo-blocking is ineffective — users in restricted regions (e.g. US) can access geo-blocked features because EligibilityService defaults to eligible for UNKNOWN.
  • Ramps: Region detection always fails, breaking region-specific flows.
  • Card: Geolocation is unavailable.

Fix: Updated the validation regex to /^[A-Z]{2}(-[A-Z0-9]{1,3})?$/u, which accepts both 2-letter country codes (US, GB) and country+subdivision codes (US-NY, CA-ON). This matches the actual API response format and is consistent with the previous mobile implementation (useDetectGeolocation), which stored the raw API response with no validation.

Updated all JSDoc references from "ISO 3166-1 alpha-2" to "ISO 3166-2" to accurately reflect the API contract.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Changes geolocation response validation to allow ISO 3166-2 subdivision codes, which can affect region-based eligibility/geo-blocking decisions across consumers. Logic change is small (regex + tests/docs) but touches compliance-sensitive behavior.

Overview
Fixes geolocation parsing to match the API contract. GeolocationApiService.#performFetch() now accepts ISO 3166-2 codes (country plus optional subdivision like US-NY, CA-ON) instead of rejecting everything except 2-letter country codes.

Updates JSDoc/auto-generated method action docs and the package changelog to describe the new location-code format, and expands the service test suite to cover valid/invalid subdivision variants.

Written by Cursor Bugbot for commit 2a922f4. This will update automatically on new commits. Configure here.

@MarioAslau MarioAslau self-assigned this Mar 6, 2026
@MarioAslau MarioAslau marked this pull request as ready for review March 6, 2026 17:53
@MarioAslau MarioAslau requested a review from a team as a code owner March 6, 2026 17:53
@cryptodev-2s
Copy link
Contributor

cryptodev-2s commented Mar 6, 2026

@MarioAslau Could you also update the changelog file ?

@mcmire
Copy link
Contributor

mcmire commented Mar 6, 2026

@MarioAslau FYI, this repo has its own pull request template, it doesn't use the same template as extension/mobile. Updating changelogs also follows a different process.

@MarioAslau
Copy link
Contributor Author

@MarioAslau FYI, this repo has its own pull request template, it doesn't use the same template as extension/mobile. Updating changelogs also follows a different process.

Found it! Thanks for pointing that out! I configured a mono repo folder with cursor rules at the root, to allow the agent to learn from both codebases for more complex cross repo implementations. I'm modifying it !

@MarioAslau MarioAslau requested a review from mcmire March 6, 2026 18:43
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests could be more comprehensive, but I guess they can be adjusted as the need arises.

@MarioAslau MarioAslau added this pull request to the merge queue Mar 6, 2026
Merged via the queue into main with commit 91729bd Mar 6, 2026
322 checks passed
@MarioAslau MarioAslau deleted the feat/geolocation-controller-correct-countrycode branch March 6, 2026 19:38
@MarioAslau MarioAslau mentioned this pull request Mar 6, 2026
4 tasks
github-merge-queue bot pushed a commit that referenced this pull request Mar 6, 2026
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

Includes Geolocation Controller Patch:
#8137

## References

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Low risk release bookkeeping only: version and changelog updates with
no functional code changes in this diff.
> 
> **Overview**
> **Release bump:** Updates the root monorepo version to `854.0.0`.
> 
> **Package release:** Bumps `@metamask/geolocation-controller` to
`0.1.1` and updates its `CHANGELOG.md` to document the fix for accepting
ISO 3166-2 subdivision codes from the geolocation API.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
91ab404. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants