Skip to content

Commit 4e8b2d7

Browse files
committed
Add runtime type check to args passed to setUploadEnabled API
1 parent e6e9272 commit 4e8b2d7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

glean/src/core/glean.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ class Glean {
356356
* @param flag When true, enable metric collection.
357357
*/
358358
static setUploadEnabled(flag: boolean): void {
359+
if (!isBoolean(flag)) {
360+
log(
361+
LOG_TAG,
362+
"Unable to change upload state, new value must be a boolean. Ignoring.",
363+
LoggingLevel.Error
364+
);
365+
return;
366+
}
367+
359368
Context.dispatcher.launch(async () => {
360369
if (!Context.initialized) {
361370
log(

glean/tests/unit/core/glean.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,18 @@ describe("Glean", function() {
605605
await Glean.testResetGlean(testAppId, true);
606606
assert.strictEqual(Context.initialized, true);
607607
});
608+
609+
it("setUploadEnabled does nothing in case a non-boolean value is passed to it", async function() {
610+
// Set the current upload value to false,
611+
// strings are "truthy", this way we can be sure calling with the wrong type dod not work.
612+
Glean.setUploadEnabled(false);
613+
await Context.dispatcher.testBlockOnQueue();
614+
assert.strictEqual(Context.uploadEnabled, false);
615+
616+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
617+
// @ts-ignore
618+
Glean.setUploadEnabled("not a boolean");
619+
await Context.dispatcher.testBlockOnQueue();
620+
assert.strictEqual(Context.uploadEnabled, false);
621+
});
608622
});

0 commit comments

Comments
 (0)