riot-web/src/sentry.ts

233 lines
8.1 KiB
TypeScript
Raw Normal View History

2021-08-12 18:46:28 +02:00
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-08-11 17:11:10 +02:00
import * as Sentry from "@sentry/browser";
import { MatrixClient } from "matrix-js-sdk/src/client";
2021-08-11 17:11:10 +02:00
import SdkConfig from "./SdkConfig";
2021-08-11 17:50:33 +02:00
import { MatrixClientPeg } from "./MatrixClientPeg";
2021-08-11 17:49:28 +02:00
import SettingsStore from "./settings/SettingsStore";
import { IConfigOptions } from "./IConfigOptions";
2021-08-11 17:11:10 +02:00
2021-08-18 10:21:57 +02:00
/* eslint-disable camelcase */
type StorageContext = {
storageManager_persisted?: string;
storageManager_quota?: string;
storageManager_usage?: string;
storageManager_usageDetails?: string;
};
type UserContext = {
username: string;
enabled_labs: string;
low_bandwidth: string;
};
type CryptoContext = {
device_keys?: string;
cross_signing_ready?: string;
cross_signing_supported_by_hs?: string;
cross_signing_key?: string;
cross_signing_privkey_in_secret_storage?: string;
cross_signing_master_privkey_cached?: string;
cross_signing_user_signing_privkey_cached?: string;
secret_storage_ready?: string;
secret_storage_key_in_account?: string;
session_backup_key_in_secret_storage?: string;
session_backup_key_cached?: string;
session_backup_key_well_formed?: string;
};
type DeviceContext = {
device_id: string;
mx_local_settings: string;
modernizr_missing_features?: string;
};
type Contexts = {
user: UserContext;
crypto: CryptoContext;
device: DeviceContext;
storage: StorageContext;
};
/* eslint-enable camelcase */
async function getStorageContext(): Promise<StorageContext> {
2021-08-11 17:50:33 +02:00
const result = {};
2021-08-11 17:49:28 +02:00
// add storage persistence/quota information
if (navigator.storage && navigator.storage.persisted) {
try {
result["storageManager_persisted"] = String(await navigator.storage.persisted());
} catch (e) {}
2022-12-12 12:24:14 +01:00
} else if (document.hasStorageAccess) {
// Safari
2021-08-11 17:49:28 +02:00
try {
result["storageManager_persisted"] = String(await document.hasStorageAccess());
} catch (e) {}
}
if (navigator.storage && navigator.storage.estimate) {
try {
const estimate = await navigator.storage.estimate();
result["storageManager_quota"] = String(estimate.quota);
result["storageManager_usage"] = String(estimate.usage);
if (estimate.usageDetails) {
2021-08-18 10:21:57 +02:00
const usageDetails = [];
2022-12-12 12:24:14 +01:00
Object.keys(estimate.usageDetails).forEach((k) => {
2021-08-18 10:21:57 +02:00
usageDetails.push(`${k}: ${String(estimate.usageDetails[k])}`);
2021-08-11 17:49:28 +02:00
});
2021-08-18 10:21:57 +02:00
result[`storageManager_usage`] = usageDetails.join(", ");
2021-08-11 17:49:28 +02:00
}
} catch (e) {}
}
return result;
}
2021-08-18 10:21:57 +02:00
function getUserContext(client: MatrixClient): UserContext {
2021-08-11 17:49:28 +02:00
return {
2022-12-12 12:24:14 +01:00
username: client.credentials.userId,
enabled_labs: getEnabledLabs(),
low_bandwidth: SettingsStore.getValue("lowBandwidth") ? "enabled" : "disabled",
2021-08-11 17:49:28 +02:00
};
}
function getEnabledLabs(): string {
2022-12-12 12:24:14 +01:00
const enabledLabs = SettingsStore.getFeatureSettingNames().filter((f) => SettingsStore.getValue(f));
2021-08-11 17:49:28 +02:00
if (enabledLabs.length) {
return enabledLabs.join(", ");
}
2021-08-18 10:21:57 +02:00
return "";
2021-08-11 17:49:28 +02:00
}
2021-08-18 10:21:57 +02:00
async function getCryptoContext(client: MatrixClient): Promise<CryptoContext> {
2021-08-11 17:49:28 +02:00
if (!client.isCryptoEnabled()) {
return {};
}
const keys = [`ed25519:${client.getDeviceEd25519Key()}`];
if (client.getDeviceCurve25519Key) {
keys.push(`curve25519:${client.getDeviceCurve25519Key()}`);
}
const crossSigning = client.crypto.crossSigningInfo;
const secretStorage = client.crypto.secretStorage;
const pkCache = client.getCrossSigningCacheCallbacks();
const sessionBackupKeyFromCache = await client.crypto.getSessionBackupPrivateKey();
return {
2022-12-12 12:24:14 +01:00
device_keys: keys.join(", "),
cross_signing_ready: String(await client.isCrossSigningReady()),
cross_signing_supported_by_hs: String(
await client.doesServerSupportUnstableFeature("org.matrix.e2e_cross_signing"),
),
cross_signing_key: crossSigning.getId(),
cross_signing_privkey_in_secret_storage: String(!!(await crossSigning.isStoredInSecretStorage(secretStorage))),
cross_signing_master_privkey_cached: String(!!(pkCache && (await pkCache.getCrossSigningKeyCache("master")))),
cross_signing_user_signing_privkey_cached: String(
!!(pkCache && (await pkCache.getCrossSigningKeyCache("user_signing"))),
),
secret_storage_ready: String(await client.isSecretStorageReady()),
secret_storage_key_in_account: String(!!(await secretStorage.hasKey())),
session_backup_key_in_secret_storage: String(!!(await client.isKeyBackupKeyStored())),
session_backup_key_cached: String(!!sessionBackupKeyFromCache),
session_backup_key_well_formed: String(sessionBackupKeyFromCache instanceof Uint8Array),
2021-08-11 17:49:28 +02:00
};
}
2021-08-18 10:21:57 +02:00
function getDeviceContext(client: MatrixClient): DeviceContext {
2021-08-11 17:49:28 +02:00
const result = {
2022-12-12 12:24:14 +01:00
device_id: client?.deviceId,
mx_local_settings: localStorage.getItem("mx_local_settings"),
2021-08-11 17:49:28 +02:00
};
if (window.Modernizr) {
2022-12-12 12:24:14 +01:00
const missingFeatures = Object.keys(window.Modernizr).filter((key) => window.Modernizr[key] === false);
2021-08-11 17:49:28 +02:00
if (missingFeatures.length > 0) {
result["modernizr_missing_features"] = missingFeatures.join(", ");
}
}
return result;
}
2021-08-18 10:21:57 +02:00
async function getContexts(): Promise<Contexts> {
2021-08-11 17:49:28 +02:00
const client = MatrixClientPeg.get();
return {
2022-12-12 12:24:14 +01:00
user: getUserContext(client),
crypto: await getCryptoContext(client),
device: getDeviceContext(client),
storage: await getStorageContext(),
2021-08-11 17:49:28 +02:00
};
}
2021-08-16 14:06:19 +02:00
export async function sendSentryReport(userText: string, issueUrl: string, error: Error): Promise<void> {
const sentryConfig = SdkConfig.getObject("sentry");
2021-08-11 18:19:15 +02:00
if (!sentryConfig) return;
const captureContext = {
2022-12-12 12:24:14 +01:00
contexts: await getContexts(),
extra: {
user_text: userText,
issue_url: issueUrl,
2021-08-11 18:19:15 +02:00
},
};
2021-08-11 17:11:10 +02:00
2021-08-11 18:19:15 +02:00
// If there's no error and no issueUrl, the report will just produce non-grouped noise in Sentry, so don't
// upload it
2021-08-11 17:11:10 +02:00
if (error) {
2021-08-11 18:19:15 +02:00
Sentry.captureException(error, captureContext);
} else if (issueUrl) {
Sentry.captureMessage(`Issue: ${issueUrl}`, captureContext);
2021-08-11 17:11:10 +02:00
}
}
export function setSentryUser(mxid: string): void {
if (!SdkConfig.get().sentry || !SettingsStore.getValue("automaticErrorReporting")) return;
Sentry.setUser({ username: mxid });
}
export async function initSentry(sentryConfig: IConfigOptions["sentry"]): Promise<void> {
2021-08-11 17:11:10 +02:00
if (!sentryConfig) return;
// Only enable Integrations.GlobalHandlers, which hooks uncaught exceptions, if automaticErrorReporting is true
const integrations = [
new Sentry.Integrations.InboundFilters(),
new Sentry.Integrations.FunctionToString(),
new Sentry.Integrations.Breadcrumbs(),
new Sentry.Integrations.HttpContext(),
new Sentry.Integrations.Dedupe(),
];
if (SettingsStore.getValue("automaticErrorReporting")) {
2022-12-12 12:24:14 +01:00
integrations.push(new Sentry.Integrations.GlobalHandlers({ onerror: false, onunhandledrejection: true }));
integrations.push(new Sentry.Integrations.TryCatch());
}
2021-08-11 17:11:10 +02:00
Sentry.init({
dsn: sentryConfig.dsn,
release: process.env.VERSION,
2021-08-11 17:11:10 +02:00
environment: sentryConfig.environment,
defaultIntegrations: false,
autoSessionTracking: false,
integrations,
2021-08-11 17:11:10 +02:00
// Set to 1.0 which is reasonable if we're only submitting Rageshakes; will need to be set < 1.0
// if we collect more frequently.
tracesSampleRate: 1.0,
});
}
window.mxSendSentryReport = sendSentryReport;