From 05c17e34390217cac9678ce5e1d733454c2dc511 Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Mon, 3 Feb 2025 09:53:45 +0700 Subject: [PATCH 1/2] feat(developer): include command line in kmc sentry reports In order to reproduce kmc errors, it's very helpful to know how it was instantiated. This change includes the command line call for kmc. This includes things such as a keyboard filename and may include file paths, but does not include private personal information or secrets. --- .../src/common/web/utils/src/utils/KeymanSentry.ts | 10 ++++++++-- developer/src/kmc/src/util/TestKeymanSentry.ts | 2 +- developer/src/kmc/src/util/kmcSentryOptions.ts | 6 ++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/developer/src/common/web/utils/src/utils/KeymanSentry.ts b/developer/src/common/web/utils/src/utils/KeymanSentry.ts index 94409eafc8..dfe62bfa3d 100644 --- a/developer/src/common/web/utils/src/utils/KeymanSentry.ts +++ b/developer/src/common/web/utils/src/utils/KeymanSentry.ts @@ -64,11 +64,17 @@ export class KeymanSentry { } } - static async captureException(e: any): Promise { + /** + * capture an exception for Sentry; note that in local environments, this will normally + * throw the exception rather than sending to Sentry + * @param e + * @param force if true, reports to Sentry even in local environments + */ + static async captureException(e: any, force?: boolean): Promise { if(isInit) { // For local development, we don't want to bury the trace; we need the cast to avoid // TS2367 (comparison appears to be unintentional) - if((KEYMAN_VERSION.VERSION_ENVIRONMENT as string) == 'local') { + if(!force && (KEYMAN_VERSION.VERSION_ENVIRONMENT as string) == 'local') { throw e; } diff --git a/developer/src/kmc/src/util/TestKeymanSentry.ts b/developer/src/kmc/src/util/TestKeymanSentry.ts index 430ab73d68..b8572516bd 100644 --- a/developer/src/kmc/src/util/TestKeymanSentry.ts +++ b/developer/src/kmc/src/util/TestKeymanSentry.ts @@ -43,7 +43,7 @@ export class TestKeymanSentry { try { throw new Error('Test error from -sentry-client-test-exception event'); } catch(e: any) { - await KeymanSentry.captureException(e); + await KeymanSentry.captureException(e, true); } } } diff --git a/developer/src/kmc/src/util/kmcSentryOptions.ts b/developer/src/kmc/src/util/kmcSentryOptions.ts index bbb5c09a87..4b87990777 100644 --- a/developer/src/kmc/src/util/kmcSentryOptions.ts +++ b/developer/src/kmc/src/util/kmcSentryOptions.ts @@ -1,5 +1,11 @@ +import Sentry from "@sentry/node"; +import * as process from "node:process"; import { SentryNodeOptions } from "@keymanapp/developer-utils"; +Sentry.setContext('Command Line', { + argv: process.argv.join(' ') +}); + /** * Rewrites sourcemap paths for the esbuild distribution of kmc (as used in * Keyman Developer itself) //kmc.mjs to /dist/kmc.mjs, so that From 4a879acd9d510c72d0ee3ce5d297c35c0915c4d1 Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Tue, 4 Feb 2025 10:12:23 +0700 Subject: [PATCH 2/2] chore(developer): minimize PII in sentry cmdline report --- developer/src/kmc/src/util/kmcSentryOptions.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/developer/src/kmc/src/util/kmcSentryOptions.ts b/developer/src/kmc/src/util/kmcSentryOptions.ts index 4b87990777..c6c47f732f 100644 --- a/developer/src/kmc/src/util/kmcSentryOptions.ts +++ b/developer/src/kmc/src/util/kmcSentryOptions.ts @@ -3,7 +3,15 @@ import * as process from "node:process"; import { SentryNodeOptions } from "@keymanapp/developer-utils"; Sentry.setContext('Command Line', { - argv: process.argv.join(' ') + // obfusate parameters with longer paths e.g. from 'c:/users/name/a/b' to + // '…/a/b' to minimize PII risk without losing all command line data. + // Also normalizes backslashes to slashes for simplicity. + argv: process.argv.map(param => { + const p = param.replaceAll('\\', '/').split('/'); + return (p.length < 3) + ? param + : ("…/" + p.slice(-2).join('/')); + }).join(' ') }); /**