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

Ignore invalid three-character country codes for FidesJS geolocation (e.g. "USA") #4877

Merged
merged 10 commits into from
May 10, 2024

Conversation

NevilleS
Copy link
Contributor

@NevilleS NevilleS commented May 9, 2024

Closes PROD-2063

Description Of Changes

This makes FidesJS and the Privacy Center slightly more defensive against invalid ISO 3166-2 codes that must use two-character country codes (e.g. US vs USA). Technically there are ISO 3166-1 alpha-3 codes that are three letter codes, but these are meant for other purposes (like passports) and aren't used along with the ISO 3166-2 subdivisions, etc.

More importantly, the Fides API expects only two-letter country codes compliant with ISO 3166-1 alpha-2, so this ensures that FidesJS & Privacy Center code will match the validation used in the API 👍

However, we do support a special-case EEA location code, so this ensures that is still considered valid!

Code Changes

  • Update Privacy Center and FidesJS validation regexes for geolocations
  • Update tests for 2-letter, 3-letter, and EEA codes
  • Update docs

Steps to Confirm

  • Run all unit tests in fides-js and privacy-center projects
  • Locally, test that the privacy center's /fides.js?geolocation=USA route does not return a geolocation object in the generated bundle

Pre-Merge Checklist

Copy link

vercel bot commented May 9, 2024

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

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
fides-plus-nightly ⬜️ Ignored (Inspect) Visit Preview May 10, 2024 1:58pm

Copy link

cypress bot commented May 9, 2024

Passing run #7675 ↗︎

0 4 0 0 Flakiness 0
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.

Details:

Merge 23b903e into 418a1b8...
Project: fides Commit: 2f78ab04ca ℹ️
Status: Passed Duration: 00:35 💡
Started: May 10, 2024 2:09 PM Ended: May 10, 2024 2:10 PM

Review all test suite changes for PR #4877 ↗︎

Comment on lines -199 to -201
.fides-acknowledge-button-container {
margin-bottom: 0px;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated, but this is invalid CSS and prints a console error in the privacy center unit tests if you look closely at them: https://github.com/ethyca/fides/actions/runs/9008947049/job/24752130402#step:8:18

Copy link
Contributor

Choose a reason for hiding this comment

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

speaking of which, have we considered using sass for fides.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! The postprocessor we use in rollup supports lots of plugins 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

apparently this is supposedly valid now. https://caniuse.com/?search=css%20nesting

* convenience.
*/
export const VALID_ISO_3166_LOCATION_REGEX =
/^(?:([a-z]{2})(-[a-z0-9]{1,3})?|(eea))$/i;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This regex is getting a bit more complicated but really only because of the () characters that add some visual noise. You can try it here to see more of an explanation: https://regex101.com/r/lYuAGj/2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like this:

  1. ^(?:...)$: outer, non-capturing group (that's the ?: to ensure we test the whole string from ^ -> $
  2. ([a-z]{2}): required 2-letter country code
  3. (-[a-z0-9]{1,3})? optional 1-3 alphanumeric region code
  4. |(eea) alternate support for eea as a special-case match

Comment on lines -104 to -105
// DEFER: return geoLocation.country when BE supports filtering by just country
// see https://github.com/ethyca/fides/issues/3300
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been supported for ages, just fixing the comment

@@ -86,7 +111,6 @@ describe("getGeolocation", () => {
const tests = [
{ input: { country: "US", region: undefined }, expected: "US" },
{ input: { country: "us", region: undefined }, expected: "us" },
{ input: { country: "USA", region: "NY" }, expected: "USA-NY" },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer valid 👍

Comment on lines 176 to 180
const req = createRequest({
url: "https://privacy.example.com/fides.js?geolocation=USA",
});
const geolocation = await lookupGeolocation(req);
expect(geolocation).toBeNull();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there not a reason to test USA-NY also, since that was removed above? Or do you think that's a redundant test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, more tests are totally reasonable! If a second engineer thinks a test helps I always prefer to add it - modifying now...

clients/privacy-center/pages/api/fides-js.ts Outdated Show resolved Hide resolved
@NevilleS
Copy link
Contributor Author

Updated!

@NevilleS NevilleS requested a review from gilluminate May 10, 2024 13:45
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.

Great update. Looks good.

@NevilleS NevilleS merged commit 5a377ca into main May 10, 2024
13 checks passed
@NevilleS NevilleS deleted the PROD-2063-ns-prevent-3-char-country-codes branch May 10, 2024 16:21
NevilleS added a commit that referenced this pull request May 10, 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.

2 participants