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(telemetry): handle malformed telemetry tokens #3014

Merged
merged 3 commits into from
Aug 24, 2021

Conversation

rwaskiewicz
Copy link
Contributor

@rwaskiewicz rwaskiewicz commented Aug 23, 2021

Fixes: #3013

Summary:

this fix handles the event that a user has an Ionic configuration file,
but does not have a 'tokens.telemetry' field in said file.

Replication:
With master@HEAD, modify your local ~/.ionic/config.json in one of two ways

  1. remove 'tokens.telemetry' completely
  2. set 'tokens.telemetry' to undefined (or any other valid JSON)

Then, spin up a small Stencil project, and attempt to turn telemetry off/on:

$ cd <your_sandbox_dir>
$ npm init stencil
<create an ionic pwa app>
$ cd <your_app_name>
$ npx stencil telemetry on
[25:32.7]  @stencil/core
[25:32.8]  v2.7.0 🌟

[ ERROR ]  uncaught cli error: TypeError: Cannot read property 'match' of undefined

Testing:
With the HEAD of this branch, build it and install it in the PWA app you build

$ cd stencil
$ npm run build && npm pack
$ cd <your_sandbox_dir>/<your_app_name>
$ npm i <location_of_stencil_tarball>
$  ionic-pwa npx stencil telemetry on
[28:20.0]  @stencil/core
[28:20.1]  [LOCAL DEV] 🏆

Telemetry is now Enabled. Thank you for helping to make Stencil better! 💖

@@ -25,7 +30,10 @@ export async function readConfig(): Promise<TelemetryConfig> {
};

await writeConfig(config);
} else if (!!config && !config['tokens.telemetry'].match(UUID_REGEX)) {
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 is the line that was failing. If the config file existed, but 'tokens.telemetry' either:

  • existed and wasn't a string
  • did not exist at all

we would call match() on "something" that didn't have it in it's prototype chain

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be safe to do the inverse and call RegExp.test() with "something" as an argument, i.e. !UUID_REGEX.test(config['tokens.telemetry']).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhh good point - done in fc90181

@rwaskiewicz rwaskiewicz changed the title @rwaskiewicz fix(telemetry): handle malformed telemetry tokens fix(telemetry): handle malformed telemetry tokens Aug 23, 2021
@rwaskiewicz rwaskiewicz marked this pull request as ready for review August 23, 2021 13:31
@rwaskiewicz rwaskiewicz requested a review from a team August 23, 2021 13:31
@@ -25,7 +30,10 @@ export async function readConfig(): Promise<TelemetryConfig> {
};

await writeConfig(config);
} else if (!!config && !config['tokens.telemetry'].match(UUID_REGEX)) {
} else if (
!!config &&
Copy link
Contributor Author

@rwaskiewicz rwaskiewicz Aug 23, 2021

Choose a reason for hiding this comment

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

@splitinfinities is there a reason I can't remove this check? (Just the !!config &&). Per this condition, this should always return true

Copy link
Contributor

Choose a reason for hiding this comment

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

I intended this to be a guard clause to protect against accessing config if it ended up not being defined. I think what you changed it to is sensible as well.

Copy link
Contributor

@splitinfinities splitinfinities left a comment

Choose a reason for hiding this comment

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

LGTM!


expect(result.isFile).toBe(true);

const config = await readConfig();

expect(Object.keys(config).join()).toBe('telemetry.stencil,tokens.telemetry');
expect(config['telemetry.stencil']).toBe(true);
expect(!!config['tokens.telemetry'].match(UUID_REGEX)).toBe(true);
expect(config['tokens.telemetry']).toMatch(UUID_REGEX);
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL!

this fix handles the event that a user has an Ionic configuration file,
but does not have a 'tokens.telemetry' field in said file.

STENCIL-85: ionic-pwa fails at startup
add a `beforeEach` test setup block to delete the configuration file
before each test is run, in order to ensure better separation of tests &
attempt to mitigate side effects between tests for this function. this
led to a small amount of clean up in pre-existing tests

update usage of let->const where appropriate

use jest's toMatch() where appropriate

update the test for an invalid, but string value for tokens.telemetry to
better differentiate it from the test added in the parent commit
@rwaskiewicz rwaskiewicz force-pushed the rwaskiewicz/match_fail branch from fc90181 to 7445e0e Compare August 24, 2021 16:19
@rwaskiewicz rwaskiewicz merged commit ff75a47 into master Aug 24, 2021
@rwaskiewicz rwaskiewicz deleted the rwaskiewicz/match_fail branch August 24, 2021 16:30
rwaskiewicz added a commit that referenced this pull request Aug 27, 2021
handle the scenario that a user has an Ionic configuration file,
but does not have a 'tokens.telemetry' field in said file.

STENCIL-85: ionic-pwa fails at startup
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.

ionic-pwa failing to start
3 participants