Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of some ISO-3166 geolocation edge cases in Privacy Center /fides.js endpoint #4858

Merged
merged 9 commits into from
May 3, 2024

Conversation

NevilleS
Copy link
Contributor

@NevilleS NevilleS commented May 2, 2024

Closes PROD-2015

Description Of Changes

This fixes a subtle error where the Privacy Center would consider some valid ISO-3166 codes as invalid if they had single-character regions; for example, SE-O would be rejected. The fix here is trivial (accept 1-3 characters!) but this PR also adds additional test coverage for confidence, and also adds an extra defensive check to fallback to just the country header if an invalid CloudFront region header is detected.

Code Changes

  • Update geolocation unit tests
  • Fix ISO-3166 regex in geolocation.ts
  • Discard invalid CloudFront region headers and fallback to country-only

Steps to Confirm

  • Run test suite 👍

Pre-Merge Checklist

Copy link

vercel bot commented May 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
fides-plus-nightly ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 2, 2024 10:57pm

@NevilleS NevilleS changed the title Fix handling of ISO-3166-2 subdivision codes in Privacy Center /fides.js endpoint Fix handling of some ISO-3166 edge cases in Privacy Center /fides.js endpoint May 2, 2024
@NevilleS NevilleS changed the title Fix handling of some ISO-3166 edge cases in Privacy Center /fides.js endpoint Fix handling of some ISO-3166 geolocation edge cases in Privacy Center /fides.js endpoint May 2, 2024
Copy link

cypress bot commented May 2, 2024

Passing run #7594 ↗︎

0 4 0 0 Flakiness 0

Details:

Merge eb7c620 into 8751e3e...
Project: fides Commit: 541868a4b4 ℹ️
Status: Passed Duration: 00:33 💡
Started: May 2, 2024 11:06 PM Ended: May 2, 2024 11:07 PM

Review all test suite changes for PR #4858 ↗︎

@gilluminate
Copy link
Contributor

@NevilleS this all looks good, but we also do a validation on the fides-js-demo.html and fides-js-components-demo.html pages. Is that still necessary? If so, do those need to be updated to reflect these same changes?

// 3) Separated by a dash (e.g. "US-CA")
const VALID_ISO_3166_LOCATION_REGEX = /^\w{2,3}(-\w{2,3})?$/;
const VALID_ISO_3166_LOCATION_REGEX = /^\w{2,3}(-\w{1,3})?$/;
Copy link
Contributor

@RobertKeyser RobertKeyser May 2, 2024

Choose a reason for hiding this comment

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

nit: technically the \w metacharacter in regex would match some values that are invalid, e.g. _ and also country codes that starts with a digit. For the country part only (the region part is good, of course)

This is how the regex was already built, so it doesn't make anything worse as written. It's also of minimal risk IMO, but I just wanted to call it out.

The fix would be to replace your \w metacharacters with a set: [A-Za-z]

Copy link
Contributor

Choose a reason for hiding this comment

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

Might also worth adding it to the unit tests 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair- however, the true spec is "alphanumeric" characters (at least for the region codes, aka subdivisions), where numeric codes are definitely allowed. However, not just all "non-whitespace" characters!

There's some wiggle room in this regex to be more permissive, but tightening it up here might make sense...

I'll definitely add some test cases and then decide

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Er my last comment is a bit backwards, low reading comprehension there - but yes, I do think I should reject numeric country codes, that's just another subtle footgun!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, updated! I think it's preferable to be defensive here and not overly permissive - the change to discard invalid region headers is there as a safety hatch, but I think it's good to be strict about the ?geolocation= param since that's a user-facing API

@NevilleS
Copy link
Contributor Author

NevilleS commented May 2, 2024

@NevilleS this all looks good, but we also do a validation on the fides-js-demo.html and fides-js-components-demo.html pages. Is that still necessary? If so, do those need to be updated to reflect these same changes?

I don't think it was ever really needed on those pages... removing for consistency, so that our tests don't silently discard that and hide potential issues...!

Pushed up and re-running.

Copy link
Contributor

@gilluminate gilluminate left a comment

Choose a reason for hiding this comment

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

Approved without looking too closely at the validation specifics since @RobertKeyser is the expert there.

@NevilleS NevilleS merged commit a302531 into main May 3, 2024
13 checks passed
@NevilleS NevilleS deleted the PROD-2015-ns-fix-pc-geolocation branch May 3, 2024 12:23
Kelsey-Ethyca pushed a commit that referenced this pull request May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants