diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index 1644c07f36..c10149fb38 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -51,6 +51,7 @@ export * from "./tar"; export * from "./toggle-set"; export * from "./toJS"; export * from "./type-narrowing"; +export * from "./isValidSentryDsn"; import * as iter from "./iter"; diff --git a/src/common/utils/isValidSentryDsn.ts b/src/common/utils/isValidSentryDsn.ts new file mode 100644 index 0000000000..10033e5013 --- /dev/null +++ b/src/common/utils/isValidSentryDsn.ts @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +// Regular expression used to parse a Sentry Dsn. +// from https://github.com/getsentry/sentry-javascript/blob/f624f442fd76bf655de6def9fa4d7631735ebba0/packages/utils/src/dsn.ts#L6 +const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/; + +/** + * check if string is a valid Sentry dsn + * + * @param {(string | undefined | null)} string + * @return {boolean} + */ +export const isValidSentryDsn = (string: string | undefined | null) => { + + const valid = DSN_REGEX.exec(string); + + return valid; +}; diff --git a/src/common/vars.ts b/src/common/vars.ts index 74dcb0abf3..dd9a89ca79 100644 --- a/src/common/vars.ts +++ b/src/common/vars.ts @@ -24,6 +24,7 @@ import path from "path"; import { SemVer } from "semver"; import packageInfo from "../../package.json"; import { defineGlobal } from "./utils/defineGlobal"; +import { isValidSentryDsn } from "./utils/isValidSentryDsn"; export const isMac = process.platform === "darwin"; export const isWindows = process.platform === "win32"; @@ -71,4 +72,5 @@ export const supportUrl = "https://docs.k8slens.dev/latest/support/" as string; export const appSemVer = new SemVer(packageInfo.version); export const docsUrl = "https://docs.k8slens.dev/main/" as string; -export const sentryDsn = packageInfo.config?.sentryDsn; +export const sentryDsn = packageInfo.config?.sentryDsn ?? ""; +export const sentryDsnIsValid = isValidSentryDsn(sentryDsn); diff --git a/src/renderer/components/+preferences/preferences.tsx b/src/renderer/components/+preferences/preferences.tsx index 285c9b2c75..3ec4693fe8 100644 --- a/src/renderer/components/+preferences/preferences.tsx +++ b/src/renderer/components/+preferences/preferences.tsx @@ -41,7 +41,7 @@ import { FormSwitch, Switcher } from "../switch"; import { KubeconfigSyncs } from "./kubeconfig-syncs"; import { SettingLayout } from "../layout/setting-layout"; import { Checkbox } from "../checkbox"; -import { sentryIsInitialized } from "../../../common/sentry"; +import { sentryDsnIsValid } from "../../../common/vars"; enum Pages { Application = "application", @@ -259,7 +259,7 @@ export class Preferences extends React.Component {

Telemetry

{telemetryExtensions.map(this.renderExtension)} - {sentryIsInitialized ? ( + {sentryDsnIsValid ? (