Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions care.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ const careConfig = {

// Plugins related configs...
sentry: {
dsn:
env.REACT_SENTRY_DSN ||
"https://8801155bd0b848a09de9ebf6f387ebc8@sentry.io/5183632",
dsn: env.REACT_SENTRY_DSN,
environment: env.REACT_SENTRY_ENVIRONMENT || "staging",
},

Expand Down
29 changes: 29 additions & 0 deletions src/Integrations/Sentry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it } from "vitest";

import { scrubUrl } from "@/Integrations/Sentry";

describe("scrubUrl", () => {
it("strips query strings", () => {
expect(scrubUrl("/patients?phone_number=999")).toBe("/patients");
});

it("replaces UUID path segments", () => {
expect(
scrubUrl(
"/facility/0198aaaa-1111-7bbb-8ccc-2ddddddddddd/patients/0198bbbb-2222-7ccc-8ddd-3eeeeeeeeeee/encounter",
),
).toBe("/facility/<id>/patients/<id>/encounter");
});

it("strips query strings and replaces UUID path segments together", () => {
expect(
scrubUrl(
"/facility/0198aaaa-1111-7bbb-8ccc-2ddddddddddd/patients?phone_number=999",
),
).toBe("/facility/<id>/patients");
});

it("leaves non-sensitive urls intact", () => {
expect(scrubUrl("/login")).toBe("/login");
});
});
Comment thread
greptile-apps[bot] marked this conversation as resolved.
43 changes: 35 additions & 8 deletions src/Integrations/Sentry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,45 @@ interface Props {
disabled?: boolean;
}

const UUID_PATTERN =
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;

export function scrubUrl(url: string): string {
return url.split("?")[0].replace(UUID_PATTERN, "<id>");
}

export default function Sentry({ disabled }: Props) {
useEffect(() => {
if (disabled) return;
if (!careConfig.sentry.dsn || !careConfig.sentry.environment) {
console.error(
"Sentry is not configured correctly. Please check your environment variables.",
);
return;
}
if (disabled || !careConfig.sentry.dsn) return;

import("@sentry/browser").then((Sentry) => {
Sentry.init(careConfig.sentry);
Sentry.init({
Comment thread
rithviknishad marked this conversation as resolved.
dsn: careConfig.sentry.dsn,
environment: careConfig.sentry.environment,
sendDefaultPii: false,
beforeSend(event) {
if (event.request?.url) {
event.request.url = scrubUrl(event.request.url);
}
if (event.request) {
delete event.request.headers;
delete event.request.cookies;
}
event.breadcrumbs = event.breadcrumbs?.map((crumb) => {
if (typeof crumb.data?.url === "string") {
crumb.data.url = scrubUrl(crumb.data.url);
}
if (typeof crumb.data?.to === "string") {
crumb.data.to = scrubUrl(crumb.data.to);
}
if (typeof crumb.data?.from === "string") {
crumb.data.from = scrubUrl(crumb.data.from);
}
return crumb;
});
return event;
},
});
});
}, [disabled]);
Comment thread
rithviknishad marked this conversation as resolved.

Expand Down
9 changes: 0 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import "@/style/index.css";
import "reactflow/dist/style.css";

import * as Sentry from "@sentry/browser";

import App from "@/App";
import { AuthContextType, AuthUserContext } from "@/hooks/useAuthUser";
import { initI18n } from "@/i18n";
Expand Down Expand Up @@ -42,13 +40,6 @@ window.addEventListener("vite:preloadError", (event) => {
}
});

if (import.meta.env.PROD) {
Sentry.init({
environment: import.meta.env.MODE,
dsn: "https://8801155bd0b848a09de9ebf6f387ebc8@sentry.io/5183632",
});
}

// Initialize i18n with namespaces from API before rendering the app
initI18n()
.then(() => {
Expand Down
Loading