Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

fix: critical and high-severity bugs from the October bug sweep (#1328)

Goldflagfe676c6727f3
diff --git a/client/src/api/admin/hooks/useImport.ts b/client/src/api/admin/hooks/useImport.ts
index 84f5dfcec..de573df0e 100644
--- a/client/src/api/admin/hooks/useImport.ts
+++ b/client/src/api/admin/hooks/useImport.ts
@@ -1,10 +1,23 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+import { DateTime } from "luxon";
import { ImportPlatform } from "@/types/import";
import { DEFAULT_EVENT_LIMIT } from "@/lib/subscription/constants";
import { IS_CLOUD } from "@/lib/const";
-import { getSiteImports, createSiteImport, deleteSiteImport } from "../endpoints";
+import { getSiteImports, createSiteImport, deleteSiteImport, GetSiteImportsResponse } from "../endpoints";
import { useCurrentSite } from "./useSites";
+// Mirrors the server's IMPORT_TIMEOUT_MS. Only the parser's final batch completes an import, so one
+// whose upload stopped early (tab closed, network lost) is abandoned after this: it stops blocking
+// new imports and polling, and the server lets it be deleted.
+const IMPORT_TIMEOUT_MS = 2 * 60 * 60 * 1000;
+
+export function isImportRunning(imp: GetSiteImportsResponse): boolean {
+ if (imp.completedAt !== null) return false;
+ const sql = DateTime.fromSQL(imp.startedAt, { zone: "utc" });
+ const startedAt = sql.isValid ? sql : DateTime.fromISO(imp.startedAt, { zone: "utc" });
+ return !startedAt.isValid || DateTime.utc().diff(startedAt).toMillis() <= IMPORT_TIMEOUT_MS;
+}
+
export function useGetSiteImports(site: number) {
const { subscription } = useCurrentSite();
@@ -14,7 +27,7 @@ export function useGetSiteImports(site: number) {
queryKey: ["get-site-imports", site],
queryFn: async () => await getSiteImports(site),
refetchInterval: data => {
- const hasActiveImports = data.state.data?.data.some(imp => imp.completedAt === null);
+ const hasActiveImports = data.state.data?.data.some(isImportRunning);
return hasActiveImports ? 5000 : false;
},
placeholderData: { data: [] },
diff --git a/client/src/api/analytics/hooks/featureFlags/useFeatureFlags.ts b/client/src/api/analytics/hooks/featureFlags/useFeatureFlags.ts
index ed5ac1481..72592fc6e 100644
--- a/client/src/api/analytics/hooks/featureFlags/useFeatureFlags.ts
+++ b/client/src/api/analytics/hooks/featureFlags/useFeatureFlags.ts
@@ -52,6 +52,7 @@ export function useDeleteFeatureFlag() {
mutationFn: (flagId: number) => deleteFeatureFlag(site, flagId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["feature-flags", site] });
+ queryClient.invalidateQueries({ queryKey: ["experiments", site] });
},
});
}
diff --git a/client/src/app/[site]/replay/components/ReplayCard.tsx b/client/src/app/[site]/replay/components/ReplayCard.tsx
index a8a62ec85..33a708ef0 100644
--- a/client/src/app/[site]/replay/components/ReplayCard.tsx
+++ b/client/src/app/[site]/replay/components/ReplayCard.tsx
@@ -66,11 +66,10 @@ function cleanUrl(url: string) {
export function ReplayCard({ replay, onSelect }: { replay: SessionReplayListItem; onSelect?: () => void }) {
const t = useExtracted();
const { formatRelative } = useDateTimeFormat();
- const { sessionId, setSessionId, resetPlayerState } = useReplayStore(
+ const { sessionId, setSessionId } = useReplayStore(
useShallow(s => ({
sessionId: s.sessionId,
setSessionId: s.setSessionId,
- resetPlayerState: s.resetPlayerState,
}))
);
const [isDialogOpen, setIsDialogOpen] = useState(false);
@@ -82,10 +81,9 @@ export function ReplayCard({ replay, onSelect }: { replay: SessionReplayListItem
const handleDelete = async () => {
try {
+ // If this was the selected replay, ReplayList moves the selection once the
+ // refetched list no longer contains it, and the player re-initialises then.
await deleteSessionReplay.mutateAsync({ sessionId: replay.session_id });
- if (sessionId === replay.session_id) {
- resetPlayerState();
- }
setIsDialogOpen(false);
} catch (error) {
console.error("Failed to delete session replay:", error);
@@ -194,7 +192,10 @@ export function ReplayCard({ replay, onSelect }: { replay: SessionReplayListItem
screen_height={replay.screen_height}
/>
<div className="ml-auto flex items-center gap-2 text-[11px] tabular-nums">
- <span className="flex items-center gap-1" title={t("{count} events", { count: formatter(replay.event_count) })}>
+ <span
+ className="flex items-center gap-1"
+ title={t("{count} events", { count: formatter(replay.event_count) })}
+ >
<MousePointerClick className="w-3 h-3" />
{formatter(replay.event_count)}
</span>
diff --git a/client/src/app/settings/account/components/DeleteAccount.tsx b/client/src/app/settings/account/components/DeleteAccount.tsx
index 317517ad5..90fa48be9 100644
--- a/client/src/app/settings/account/components/DeleteAccount.tsx
+++ b/client/src/app/settings/account/components/DeleteAccount.tsx
@@ -19,18 +19,24 @@ import {
import { Button } from "../../../../components/ui/button";
import { authClient } from "../../../../lib/auth";
import { useStripeSubscription } from "../../../../lib/subscription/useStripeSubscription";
+import { useUserOrganizations } from "../../../../api/admin/hooks/useOrganizations";
/**
- * Paid plans must be cancelled before the account can be deleted. Deliberately independent of a
- * running deletion, which used to flip the dialog to "Cannot delete account" mid-delete.
+ * Whether to remind the user to cancel the active organization's plan before deleting their
+ * account: they own it and a Stripe plan will renew. Only a hint; the server decides, refusing
+ * only when the user is the last owner of an organization whose subscription will still charge
+ * (it also knows about cancellations scheduled for a date, which this data doesn't carry).
*/
export function useHasActiveSubscription() {
const { data: subscription } = useStripeSubscription();
- return !!(subscription?.planName.startsWith("standard") || subscription?.planName.startsWith("pro"));
+ const { data: session } = authClient.useSession();
+ const { data: organizations } = useUserOrganizations({ enabled: !!session?.user });
+ const activeRole = organizations?.find(org => org.id === session?.session.activeOrganizationId)?.role;
+ // Any Stripe plan (Basic and legacy prices included) that will renew.
+ return activeRole === "owner" && !!subscription?.id && !subscription.cancelAtPeriodEnd;
}
export function DeleteAccount() {
- const hasActiveSubscription = useHasActiveSubscription();
const [isDeleting, setIsDeleting] = useState(false);
const [isOpen, setIsOpen] = useState(false);
const queryClient = useQueryClient();
@@ -83,14 +89,12 @@ export function DeleteAccount() {
<AlertDialogHeader>
<AlertDialogTitle className="flex items-center gap-2">
<AlertTriangle className="h-5 w-5" color="hsl(var(--red-500))" />
- {hasActiveSubscription ? t("Cannot delete account") : t("Delete your account?")}
+ {t("Delete your account?")}
</AlertDialogTitle>
<AlertDialogDescription>
- {hasActiveSubscription
- ? t("You have an active subscription. Please cancel your subscription before deleting your account.")
- : t(
- "This action cannot be undone. This will permanently delete your account and remove all your data from our servers."
- )}
+ {t(
+ "This action cannot be undone. This will permanently delete your account and remove all your data from our servers."
+ )}
</AlertDialogDescription>
</AlertDialogHeader>
@@ -98,11 +102,9 @@ export function DeleteAccount() {
<AlertDialogCancel onClick={handleClose} disabled={isDeleting}>
{t("Cancel")}
</AlertDialogCancel>
- {!hasActiveSubscription && (
- <HoldToConfirm onConfirm={handleAccountDeletion} pending={isDeleting} pendingLabel={t("Deleting...")}>
- {t("Hold to delete account")}
- </HoldToConfirm>
- )}
+ <HoldToConfirm onConfirm={handleAccountDeletion} pending={isDeleting} pendingLabel={t("Deleting...")}>
+ {t("Hold to delete account")}
+ </HoldToConfirm>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
diff --git a/client/src/app/settings/organization/components/DeleteOrganizationDialog.tsx b/client/src/app/settings/organization/components/DeleteOrganizationDialog.tsx
index 3ae5d139c..db85f5a83 100644
--- a/client/src/app/settings/organization/components/DeleteOrganizationDialog.tsx
+++ b/client/src/app/settings/organization/components/DeleteOrganizationDialog.tsx
@@ -26,7 +26,8 @@ import type { Organization } from "../page";
/** Whether the organization is on a paid plan, which has to be cancelled before it can be deleted. */
export function useHasActiveSubscription() {
const { data: subscription } = useStripeSubscription();
- return !!(subscription?.planName.startsWith("standard") || subscription?.planName.startsWith("pro"));
+ // Any Stripe plan (Basic and legacy prices included) that will renew. The server enforces the same rule.
+ return !!subscription?.id && !subscription.cancelAtPeriodEnd;
}
interface DeleteOrganizationDialogProps {
diff --git a/client/src/components/OrganizationInitializer.tsx b/client/src/components/OrganizationInitializer.tsx
index eeb36be66..f94cc7413 100644
--- a/client/src/components/OrganizationInitializer.tsx
+++ b/client/src/components/OrganizationInitializer.tsx
@@ -5,17 +5,19 @@ import { authClient } from "../lib/auth";
import { useUserOrganizations } from "../api/admin/hooks/useOrganizations";
import { useTrack } from "../hooks/useTrack";
-function OrganizationInitializerInner() {
+function OrganizationInitializerInner({ activeOrganizationId }: { activeOrganizationId?: string | null }) {
const { data: organizations } = useUserOrganizations();
- const { data: activeOrganization, isPending: isPendingActiveOrganization } = authClient.useActiveOrganization();
+ // Read the active organization from the session, not useActiveOrganization:
+ // a failed full-organization fetch also reports no active organization, which
+ // switched multi-org users to an arbitrary one and made single-org users loop.
useEffect(() => {
- if (!isPendingActiveOrganization && !activeOrganization && organizations?.length) {
+ if (!activeOrganizationId && organizations?.length) {
authClient.organization.setActive({
- organizationId: organizations?.[0]?.id,
+ organizationId: organizations[0].id,
});
}
- }, [isPendingActiveOrganization, activeOrganization, organizations]);
+ }, [activeOrganizationId, organizations]);
return null; // This component doesn't render anything
}
@@ -24,7 +26,7 @@ export function OrganizationInitializer() {
const session = authClient.useSession();
useTrack();
if (session.data?.user) {
- return <OrganizationInitializerInner />;
+ return <OrganizationInitializerInner activeOrganizationId={session.data.session.activeOrganizationId} />;
}
return null;
}
diff --git a/client/src/components/Sessions/ReplayDrawer.tsx b/client/src/components/Sessions/ReplayDrawer.tsx
index e77aa9046..297b0ff65 100644
--- a/client/src/components/Sessions/ReplayDrawer.tsx
+++ b/client/src/components/Sessions/ReplayDrawer.tsx
@@ -14,7 +14,7 @@ interface ReplayDrawerProps {
}
export function ReplayDrawer({ sessionId, open, onOpenChange }: ReplayDrawerProps) {
- const { setSessionId, resetPlayerState } = useReplayStore();
+ const setSessionId = useReplayStore(s => s.setSessionId);
const containerRef = useRef<HTMLDivElement>(null);
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
@@ -25,12 +25,8 @@ export function ReplayDrawer({ sessionId, open, onOpenChange }: ReplayDrawerProp
}
}, [open, sessionId, setSessionId]);
- // Reset player state when drawer closes
- useEffect(() => {
- if (!open) {
- resetPlayerState();
- }
- }, [open, resetPlayerState]);
+ // No reset on close: the drawer's player unregisters itself when it unmounts,
+ // handing the controls back to any player still on the page.
// Measure container dimensions using getBoundingClientRect for more reliable sizing
useEffect(() => {
diff --git a/client/src/components/SiteSettings/ImportManager.tsx b/client/src/components/SiteSettings/ImportManager.tsx
index b7149577b..3cf004e47 100644
--- a/client/src/components/SiteSettings/ImportManager.tsx
+++ b/client/src/components/SiteSettings/ImportManager.tsx
@@ -21,7 +21,13 @@ import { Alert, AlertDescription } from "@/components/ui/alert";
import { Badge } from "@/components/ui/badge";
import { Upload, FileText, AlertCircle, CheckCircle2, Loader2, Trash2 } from "lucide-react";
import { useExtracted } from "next-intl";
-import { useGetSiteImports, useCreateSiteImport, useDeleteSiteImport } from "@/api/admin/hooks/useImport";
+import {
+ isImportRunning,
+ useGetSiteImports,
+ useCreateSiteImport,
+ useDeleteSiteImport,
+} from "@/api/admin/hooks/useImport";
+import { GetSiteImportsResponse } from "@/api/admin/endpoints";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { IS_CLOUD } from "@/lib/const";
import { CsvParser } from "@/lib/import/csvParser";
@@ -174,8 +180,14 @@ export function ImportManager({ siteId, disabled }: ImportManagerProps) {
}
};
- const getStatusInfo = (completedAt: string | null) => {
- if (completedAt === null) {
+ const getStatusInfo = (imp: GetSiteImportsResponse) => {
+ if (imp.completedAt !== null) {
+ return {
+ variant: "success" as const,
+ icon: CheckCircle2,
+ label: t("Completed"),
+ };
+ } else if (isImportRunning(imp)) {
return {
variant: "info" as const,
icon: Loader2,
@@ -183,9 +195,9 @@ export function ImportManager({ siteId, disabled }: ImportManagerProps) {
};
} else {
return {
- variant: "success" as const,
- icon: CheckCircle2,
- label: t("Completed"),
+ variant: "destructive" as const,
+ icon: AlertCircle,
+ label: t("Failed"),
};
}
};
@@ -202,7 +214,7 @@ export function ImportManager({ siteId, disabled }: ImportManagerProps) {
});
}, [data?.data]);
- const hasActiveImport = IS_CLOUD && sortedImports.some(imp => imp.completedAt === null);
+ const hasActiveImport = IS_CLOUD && sortedImports.some(isImportRunning);
const isImportDisabled = !selectedFile || !selectedPlatform || !!fileError || disabled || hasActiveImport;
@@ -334,7 +346,8 @@ export function ImportManager({ siteId, disabled }: ImportManagerProps) {
</TableHeader>
<TableBody>
{sortedImports.map(imp => {
- const statusInfo = getStatusInfo(imp.completedAt);
+ const isRunning = isImportRunning(imp);
+ const statusInfo = getStatusInfo(imp);
const StatusIcon = statusInfo.icon;
const startedAt = DateTime.fromSQL(imp.startedAt).toFormat("MMM dd, yyyy HH:mm");
@@ -346,7 +359,7 @@ export function ImportManager({ siteId, disabled }: ImportManagerProps) {
</TableCell>
<TableCell>
<Badge variant={statusInfo.variant} className="gap-1">
- <StatusIcon className={`h-3 w-3 ${imp.completedAt === null ? "animate-spin" : ""}`} />
+ <StatusIcon className={`h-3 w-3 ${isRunning ? "animate-spin" : ""}`} />
{statusInfo.label}
</Badge>
</TableCell>
@@ -388,7 +401,7 @@ export function ImportManager({ siteId, disabled }: ImportManagerProps) {
)}
</TableCell>
<TableCell className="text-center">
- {imp.completedAt !== null && (
+ {!isRunning && (
<Button
variant="outline"
size="smIcon"
diff --git a/client/src/components/replay/player/ReplayPlayer.tsx b/client/src/components/replay/player/ReplayPlayer.tsx
index 09c860bfc..810842f77 100644
--- a/client/src/components/replay/player/ReplayPlayer.tsx
+++ b/client/src/components/replay/player/ReplayPlayer.tsx
@@ -1,5 +1,5 @@
import { useParams } from "next/navigation";
-import { useCallback, useEffect } from "react";
+import { useCallback } from "react";
import "rrweb-player/dist/style.css";
import { useShallow } from "zustand/react/shallow";
import { useGetSessionReplayEvents } from "@/api/analytics/hooks/sessionReplay/useGetSessionReplayEvents";
@@ -15,37 +15,25 @@ import { ReplayPlayerTopbar } from "./ReplayPlayerTopbar";
export function ReplayPlayer({ width, height, isDrawer }: { width: number; height: number; isDrawer?: boolean }) {
const params = useParams();
const siteId = Number(params.site);
- const {
- sessionId,
- player,
- isPlaying,
- setIsPlaying,
- currentTime,
- setCurrentTime,
- duration,
- setPlaybackSpeed,
- resetPlayerState,
- } = useReplayStore(
- useShallow(s => ({
- sessionId: s.sessionId,
- player: s.player,
- isPlaying: s.isPlaying,
- setIsPlaying: s.setIsPlaying,
- currentTime: s.currentTime,
- setCurrentTime: s.setCurrentTime,
- duration: s.duration,
- setPlaybackSpeed: s.setPlaybackSpeed,
- resetPlayerState: s.resetPlayerState,
- }))
- );
+ const { sessionId, player, isPlaying, setIsPlaying, currentTime, setCurrentTime, duration, setPlaybackSpeed } =
+ useReplayStore(
+ useShallow(s => ({
+ sessionId: s.sessionId,
+ player: s.player,
+ isPlaying: s.isPlaying,
+ setIsPlaying: s.setIsPlaying,
+ currentTime: s.currentTime,
+ setCurrentTime: s.setCurrentTime,
+ duration: s.duration,
+ setPlaybackSpeed: s.setPlaybackSpeed,
+ }))
+ );
+ // Playback state is reset by the player itself when it is created for a new
+ // session (see registerPlayer). A reset here would run after the child's
+ // effect and null out a player built from cached events.
const { data, isLoading, error } = useGetSessionReplayEvents(siteId, sessionId);
- // Reset player state when session changes
- useEffect(() => {
- resetPlayerState();
- }, [sessionId, resetPlayerState]);
-
// Calculate activity periods when player and data are ready
useActivityPeriods({ data, player });
diff --git a/client/src/components/replay/player/hooks/useReplayPlayer.ts b/client/src/components/replay/player/hooks/useReplayPlayer.ts
index a9c77c5e2..26aafd45d 100644
--- a/client/src/components/replay/player/hooks/useReplayPlayer.ts
+++ b/client/src/components/replay/player/hooks/useReplayPlayer.ts
@@ -13,9 +13,10 @@ interface UseReplayPlayerProps {
export const useReplayPlayer = ({ data, width, height }: UseReplayPlayerProps) => {
const playerContainerRef = useRef<HTMLDivElement>(null);
const playerRef = useRef<any>(null);
- const { setPlayer, setCurrentTime, setIsPlaying, setDuration } = useReplayStore(
+ const { registerPlayer, unregisterPlayer, setCurrentTime, setIsPlaying, setDuration } = useReplayStore(
useShallow(s => ({
- setPlayer: s.setPlayer,
+ registerPlayer: s.registerPlayer,
+ unregisterPlayer: s.unregisterPlayer,
setCurrentTime: s.setCurrentTime,
setIsPlaying: s.setIsPlaying,
setDuration: s.setDuration,
@@ -52,10 +53,15 @@ export const useReplayPlayer = ({ data, width, height }: UseReplayPlayerProps) =
});
playerRef.current = newPlayer;
- setPlayer(newPlayer);
+ registerPlayer(newPlayer);
+
+ // Another mounted player (e.g. the maximize drawer's) may own the
+ // shared controls; only the active one writes playback state.
+ const isActive = () => useReplayStore.getState().player === newPlayer;
// Set up event listeners
newPlayer.addEventListener("ui-update-current-time", (event: any) => {
+ if (!isActive()) return;
// Validate that current time doesn't exceed duration
const currentTime = event.payload;
const playerDuration = newPlayer.getMetaData().totalTime;
@@ -71,17 +77,19 @@ export const useReplayPlayer = ({ data, width, height }: UseReplayPlayerProps) =
});
newPlayer.addEventListener("ui-update-player-state", (event: any) => {
+ if (!isActive()) return;
setIsPlaying(event.payload === "playing");
});
newPlayer.addEventListener("ui-update-duration", (event: any) => {
+ if (!isActive()) return;
setDuration(event.payload);
});
// Get the initial duration from the player
setTimeout(() => {
const playerDuration = newPlayer.getMetaData().totalTime;
- if (playerDuration) {
+ if (playerDuration && isActive()) {
setDuration(playerDuration);
}
}, 100);
@@ -135,10 +143,10 @@ export const useReplayPlayer = ({ data, width, height }: UseReplayPlayerProps) =
document.removeEventListener("visibilitychange", handleVisibilityChange);
}
playerRef.current = null;
- setPlayer(null);
+ unregisterPlayer(newPlayer);
};
}
- }, [data, setPlayer, setCurrentTime, setIsPlaying, setDuration]);
+ }, [data, registerPlayer, unregisterPlayer, setCurrentTime, setIsPlaying, setDuration]);
// Update dimensions without recreating the player
useEffect(() => {
diff --git a/client/src/components/replay/replayStore.test.ts b/client/src/components/replay/replayStore.test.ts
new file mode 100644
index 000000000..3a23b4f5c
--- /dev/null
+++ b/client/src/components/replay/replayStore.test.ts
@@ -0,0 +1,102 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { useReplayStore } from "./replayStore";
+
+function fakePlayer({ currentTime = 0, totalTime = 0, speed = 1 } = {}) {
+ return {
+ pause: vi.fn(),
+ getMetaData: () => ({ totalTime }),
+ getReplayer: () => ({ getCurrentTime: () => currentTime, config: { speed } }),
+ };
+}
+
+const initialState = useReplayStore.getState();
+
+beforeEach(() => {
+ useReplayStore.setState(initialState, true);
+});
+
+describe("replay player registration", () => {
+ it("installs a new player with fresh playback state", () => {
+ const store = useReplayStore.getState();
+ store.setIsPlaying(true);
+ store.setCurrentTime(30);
+ store.setDuration(60);
+ store.setPlaybackSpeed("2");
+ store.setActivityPeriods([{ start: 0, end: 10 }]);
+
+ const player = fakePlayer();
+ store.registerPlayer(player);
+
+ expect(useReplayStore.getState()).toMatchObject({
+ player,
+ isPlaying: false,
+ currentTime: 0,
+ duration: 0,
+ playbackSpeed: "1",
+ activityPeriods: [],
+ });
+ });
+
+ it("swaps players when the same view re-initialises for another session", () => {
+ const store = useReplayStore.getState();
+ const previous = fakePlayer();
+ const next = fakePlayer();
+
+ store.registerPlayer(previous);
+ store.unregisterPlayer(previous);
+ store.registerPlayer(next);
+
+ expect(useReplayStore.getState().player).toBe(next);
+ expect(useReplayStore.getState().players).toEqual([next]);
+ });
+
+ it("hands control to a maximize drawer and back to the page player when it closes", () => {
+ const store = useReplayStore.getState();
+ const page = fakePlayer({ currentTime: 42, totalTime: 120, speed: 2 });
+ const drawer = fakePlayer({ totalTime: 120 });
+
+ store.registerPlayer(page);
+ store.registerPlayer(drawer);
+
+ expect(page.pause).toHaveBeenCalled();
+ expect(useReplayStore.getState().player).toBe(drawer);
+
+ useReplayStore.getState().setIsPlaying(true);
+ useReplayStore.getState().setCurrentTime(10);
+ useReplayStore.getState().unregisterPlayer(drawer);
+
+ expect(useReplayStore.getState()).toMatchObject({
+ player: page,
+ players: [page],
+ isPlaying: false,
+ currentTime: 42,
+ duration: 120,
+ playbackSpeed: "2",
+ });
+ });
+
+ it("ignores teardown of a player that is not driving the controls", () => {
+ const store = useReplayStore.getState();
+ const page = fakePlayer();
+ const drawer = fakePlayer();
+
+ store.registerPlayer(page);
+ store.registerPlayer(drawer);
+ useReplayStore.getState().setCurrentTime(5);
+ useReplayStore.getState().unregisterPlayer(page);
+
+ expect(useReplayStore.getState()).toMatchObject({ player: drawer, players: [drawer], currentTime: 5 });
+ });
+
+ it("clears playback state when the last player unmounts", () => {
+ const store = useReplayStore.getState();
+ const player = fakePlayer();
+
+ store.registerPlayer(player);
+ useReplayStore.getState().setCurrentTime(5);
+ useReplayStore.getState().setDuration(10);
+ useReplayStore.getState().unregisterPlayer(player);
+
+ expect(useReplayStore.getState()).toMatchObject({ player: null, players: [], currentTime: 0, duration: 0 });
+ });
+});
diff --git a/client/src/components/replay/replayStore.ts b/client/src/components/replay/replayStore.ts
index cbfdca349..8478d48d8 100644
--- a/client/src/components/replay/replayStore.ts
+++ b/client/src/components/replay/replayStore.ts
@@ -5,6 +5,14 @@ interface ActivityPeriod {
end: number;
}
+const initialPlaybackState = {
+ isPlaying: false,
+ currentTime: 0,
+ duration: 0,
+ playbackSpeed: "1",
+ activityPeriods: [] as ActivityPeriod[],
+};
+
export const useReplayStore = create<{
minDuration: number;
setMinDuration: (minDuration: number) => void;
@@ -13,9 +21,12 @@ export const useReplayStore = create<{
sessionId: string;
setSessionId: (sessionId: string) => void;
- // Player state
+ // Player state. More than one player can be mounted at once (the page player
+ // and the maximize drawer's), so they stack and the newest drives the controls.
player: any;
- setPlayer: (player: any) => void;
+ players: any[];
+ registerPlayer: (player: any) => void;
+ unregisterPlayer: (player: any) => void;
// Playback state
isPlaying: boolean;
@@ -32,10 +43,7 @@ export const useReplayStore = create<{
activityPeriods: ActivityPeriod[];
setActivityPeriods: (periods: ActivityPeriod[]) => void;
-
- // Reset all player state when session changes
- resetPlayerState: () => void;
-}>(set => ({
+}>((set, get) => ({
minDuration: 30,
setMinDuration: minDuration => set({ minDuration }),
@@ -45,7 +53,33 @@ export const useReplayStore = create<{
// Player state
player: null,
- setPlayer: player => set({ player }),
+ players: [],
+ // A new player resets playback state in the same step that installs it, so
+ // nothing that runs after the player is created can null it out again.
+ registerPlayer: player => {
+ get().player?.pause();
+ set(state => ({ ...initialPlaybackState, players: [...state.players, player], player }));
+ },
+ // Only the player being torn down is removed. If it was driving the controls
+ // (e.g. the maximize drawer closing), the one beneath takes over where it is.
+ unregisterPlayer: player =>
+ set(state => {
+ const players = state.players.filter(p => p !== player);
+ if (state.player !== player) return { players };
+
+ const previous = players[players.length - 1];
+ if (!previous) return { ...initialPlaybackState, players, player: null };
+
+ const replayer = previous.getReplayer();
+ return {
+ players,
+ player: previous,
+ isPlaying: false,
+ currentTime: replayer.getCurrentTime(),
+ duration: previous.getMetaData().totalTime,
+ playbackSpeed: String(replayer.config.speed),
+ };
+ }),
// Playback state
isPlaying: false,
@@ -62,15 +96,4 @@ export const useReplayStore = create<{
activityPeriods: [],
setActivityPeriods: activityPeriods => set({ activityPeriods }),
-
- // Reset all player state when session changes
- resetPlayerState: () =>
- set({
- player: null,
- isPlaying: false,
- currentTime: 0,
- duration: 0,
- playbackSpeed: "1",
- activityPeriods: [],
- }),
}));
diff --git a/client/src/lib/import/csvParser.test.ts b/client/src/lib/import/csvParser.test.ts
index 756f56ca0..ada742ac6 100644
--- a/client/src/lib/import/csvParser.test.ts
+++ b/client/src/lib/import/csvParser.test.ts
@@ -1,4 +1,5 @@
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import Papa from "papaparse";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ImportPlatform } from "@/types/import";
const authedFetch = vi.hoisted(() => vi.fn());
@@ -9,9 +10,9 @@ import { CsvParser } from "./csvParser";
// ---------------------------------------------------------------------------
// Harness
//
-// startImport hands the input straight to Papa. Papa's worker path is inert
-// outside a browser, and it streams a string through the same chunk/complete
-// callbacks a File would take, so a plain string stands in for the upload.
+// startImport hands the input straight to Papa, which streams a string through
+// the same chunk/complete callbacks (and pause/resume) a File would take, so a
+// plain string stands in for the upload.
// ---------------------------------------------------------------------------
const SITE_ID = 42;
@@ -28,6 +29,17 @@ function uploads(): Upload[] {
const events = () => uploads().flatMap(u => u.events);
+const shape = () => uploads().map(u => ({ n: u.events.length, last: u.isLastBatch }));
+
+// Uploads settle on microtasks, so one macrotask lets every chunk and the final batch go out.
+const flush = () => new Promise(resolve => setTimeout(resolve, 0));
+
+function startImport(csv: string, platform: ImportPlatform = "umami"): CsvParser {
+ const parser = new CsvParser(SITE_ID, IMPORT_ID, platform, "2024-01-01", "2024-12-31");
+ parser.startImport(csv as unknown as File);
+ return parser;
+}
+
async function runImport(
csv: string,
opts: { platform?: ImportPlatform; earliest?: string; latest?: string } = {}
@@ -41,11 +53,36 @@ async function runImport(
opts.latest ?? "2024-12-31"
);
parser.startImport(csv as unknown as File);
- // The chunk/complete callbacks are async and Papa does not await them.
- await new Promise(resolve => setTimeout(resolve, 0));
+ await flush();
return uploads();
}
+// The parser reads 5MB at a time; shrink that so a few rows span several chunks.
+function useChunkSize(bytes: number) {
+ const parse = Papa.parse;
+ vi.spyOn(Papa, "parse").mockImplementation(((input: unknown, config: object) =>
+ (parse as (input: unknown, config: object) => unknown)(input, {
+ ...config,
+ chunkSize: bytes,
+ })) as typeof Papa.parse);
+}
+
+function deferred() {
+ let resolve!: () => void;
+ let reject!: (error: Error) => void;
+ const promise = new Promise<void>((res, rej) => {
+ resolve = res;
+ reject = rej;
+ });
+ return { promise, resolve, reject };
+}
+
+const MANY_ROWS = "session_id,created_at\n" + [1, 2, 3, 4, 5].map(i => `sess-${i},2024-03-1${i} 10:00:00\n`).join("");
+
+afterEach(() => {
+ vi.restoreAllMocks();
+});
+
const UMAMI_HEADER =
"session_id,hostname,browser,os,device,screen,language,country,region,city," +
"url_path,url_query,referrer_path,referrer_domain,page_title,event_type,event_name,distinct_id,created_at\n";
@@ -208,21 +245,21 @@ describe("date range", () => {
expect(sent[0].events.map(e => e.session_id)).toEqual(["ok"]);
});
- it("uploads nothing at all when the constructor gets an unparseable range", async () => {
+ it("uploads no rows, only closes the import, when the constructor gets an unparseable range", async () => {
const parser = new CsvParser(SITE_ID, IMPORT_ID, "umami", "01-01-2024", "2024-12-31");
parser.startImport((UMAMI_HEADER + UMAMI_ROW) as unknown as File);
- await new Promise(resolve => setTimeout(resolve, 0));
+ await flush();
- expect(authedFetch).not.toHaveBeenCalled();
+ expect(uploads()).toEqual([{ events: [], isLastBatch: true }]);
});
- it("uploads nothing after cancel()", async () => {
+ it("uploads no rows, only closes the import, after cancel()", async () => {
const parser = new CsvParser(SITE_ID, IMPORT_ID, "umami", "2024-01-01", "2024-12-31");
parser.cancel();
parser.startImport((UMAMI_HEADER + UMAMI_ROW) as unknown as File);
- await new Promise(resolve => setTimeout(resolve, 0));
+ await flush();
- expect(authedFetch).not.toHaveBeenCalled();
+ expect(uploads()).toEqual([{ events: [], isLastBatch: true }]);
});
});
@@ -280,21 +317,105 @@ describe("malformed and awkward csv", () => {
});
});
+describe("upload ordering", () => {
+ it("holds the final batch until the last chunk's upload has landed", async () => {
+ const upload = deferred();
+ authedFetch.mockReturnValueOnce(upload.promise);
+
+ startImport(UMAMI_HEADER + UMAMI_ROW);
+ await flush();
+
+ // The server marks the import complete on the final batch, so it must not overtake the data.
+ expect(shape()).toEqual([{ n: 1, last: false }]);
+
+ upload.resolve();
+ await flush();
+
+ expect(shape()).toEqual([
+ { n: 1, last: false },
+ { n: 0, last: true },
+ ]);
+ });
+
+ it("uploads chunks one at a time, in file order", async () => {
+ useChunkSize(30);
+ let inFlight = 0;
+ let maxInFlight = 0;
+ authedFetch.mockImplementation(async () => {
+ inFlight++;
+ maxInFlight = Math.max(maxInFlight, inFlight);
+ await flush();
+ inFlight--;
+ });
+
+ startImport(MANY_ROWS);
+ await vi.waitFor(() => expect(uploads().at(-1)?.isLastBatch).toBe(true));
+
+ expect(maxInFlight).toBe(1);
+ expect(uploads().filter(u => !u.isLastBatch).length).toBeGreaterThan(1);
+ expect(events().map(e => e.session_id)).toEqual(["sess-1", "sess-2", "sess-3", "sess-4", "sess-5"]);
+ expect(uploads().filter(u => u.isLastBatch)).toHaveLength(1);
+ });
+});
+
describe("upload failures", () => {
- it("finalises the import even though the data chunk failed to upload", async () => {
+ beforeEach(() => {
vi.spyOn(console, "error").mockImplementation(() => {});
- authedFetch.mockRejectedValue(new Error("network down"));
+ });
+
+ it("closes the import only after the failed chunk has settled", async () => {
+ const upload = deferred();
+ authedFetch.mockReturnValueOnce(upload.promise);
+
+ startImport(UMAMI_HEADER + UMAMI_ROW);
+ await flush();
+ expect(shape()).toEqual([{ n: 1, last: false }]);
- const sent = await runImport(UMAMI_HEADER + UMAMI_ROW);
+ upload.reject(new Error("network down"));
+ await flush();
- // Current behaviour, and a bug: Papa does not await the async chunk
- // callback, so `complete` runs β€” and marks the import finished β€” before the
- // rejected upload has had a chance to set `cancelled`.
- expect(sent.map(u => ({ n: u.events.length, last: u.isLastBatch }))).toEqual([
+ // The final batch is what lets the user delete the partial import, so a failure still sends it.
+ expect(shape()).toEqual([
{ n: 1, last: false },
{ n: 0, last: true },
]);
+ });
+
+ it("stops reading the file at the first failed chunk", async () => {
+ useChunkSize(30);
+ authedFetch.mockRejectedValueOnce(new Error("network down"));
+
+ startImport(MANY_ROWS);
+ await flush();
+
+ expect(events().map(e => e.session_id)).toEqual(["sess-1"]);
+ expect(uploads().at(-1)).toEqual({ events: [], isLastBatch: true });
+ expect(uploads()).toHaveLength(2);
+ });
+
+ it("stops reading the file when cancelled mid-upload, then closes the import", async () => {
+ useChunkSize(30);
+ const upload = deferred();
+ authedFetch.mockReturnValueOnce(upload.promise);
+
+ const parser = startImport(MANY_ROWS);
+ await flush();
+ parser.cancel();
+ upload.resolve();
+ await flush();
+
+ expect(events().map(e => e.session_id)).toEqual(["sess-1"]);
+ expect(uploads().at(-1)).toEqual({ events: [], isLastBatch: true });
+ expect(uploads()).toHaveLength(2);
+ });
+
+ it("closes the import when the file can't be read", async () => {
+ vi.spyOn(Papa, "parse").mockImplementation(((_input: unknown, config: { error: (error: Error) => void }) =>
+ config.error(new Error("NotReadableError"))) as unknown as typeof Papa.parse);
+
+ startImport(UMAMI_HEADER + UMAMI_ROW);
+ await flush();
- vi.restoreAllMocks();
+ expect(uploads()).toEqual([{ events: [], isLastBatch: true }]);
});
});
diff --git a/client/src/lib/import/csvParser.ts b/client/src/lib/import/csvParser.ts
index b8570121f..62d0c9e04 100644
--- a/client/src/lib/import/csvParser.ts
+++ b/client/src/lib/import/csvParser.ts
@@ -44,6 +44,7 @@ interface SimpleAnalyticsEvent {
export class CsvParser {
private cancelled: boolean = false;
+ private finished: boolean = false;
private readonly siteId: number;
private readonly importId: string;
private readonly platform: ImportPlatform;
@@ -71,67 +72,86 @@ export class CsvParser {
startImport(file: File): void {
if (this.cancelled) {
+ // The import already exists on the server; close it so it can be deleted.
+ void this.finish();
return;
}
+ // Parsed on the main thread because Papa's worker mode can't pause. Papa doesn't await the
+ // chunk callback, so each chunk pauses the parser until its upload lands: uploads run one at
+ // a time, only one chunk is held in memory, and `complete` (the final batch) can't overtake them.
Papa.parse(file, {
header: true,
skipEmptyLines: "greedy",
- worker: true,
chunkSize: 5 * 1024 * 1024, // 5MB CSV chunks to stay under 10MB JSON limit
- chunk: async (results, parser) => {
- if (this.cancelled) {
- parser.abort();
- return;
- }
-
- try {
- if (this.platform === "umami") {
- const validEvents: UmamiEvent[] = [];
- for (const row of results.data) {
- const event = this.transformRow(row);
- if (event && this.isDateInRange((event as UmamiEvent).created_at)) {
- validEvents.push(event as UmamiEvent);
- }
- }
- if (validEvents.length > 0) {
- await this.uploadChunk(validEvents, false);
- }
- } else {
- const validEvents: SimpleAnalyticsEvent[] = [];
- for (const row of results.data) {
- const event = this.transformRow(row);
- if (event && this.isDateInRange((event as SimpleAnalyticsEvent).added_iso)) {
- validEvents.push(event as SimpleAnalyticsEvent);
- }
- }
- if (validEvents.length > 0) {
- await this.uploadChunk(validEvents, false);
- }
+ chunk: (results, parser) => {
+ parser.pause();
+ this.uploadRows(results.data).then(
+ () => (this.cancelled ? parser.abort() : parser.resume()),
+ (error: unknown) => {
+ console.error("Error uploading chunk:", error);
+ this.cancel();
+ parser.abort();
}
- } catch (error) {
- console.error("Error uploading chunk:", error);
- this.cancel();
- parser.abort();
- }
+ );
},
- complete: async () => {
- if (this.cancelled) return;
-
- try {
- // Send final batch to mark import as complete
- await this.uploadChunk([], true);
- } catch (error) {
- console.error("Error completing import:", error);
- }
+ // Also reached through parser.abort(), so a failed or cancelled import still gets its final
+ // batch: the server only marks an import complete (and deletable) when that batch arrives.
+ complete: () => {
+ void this.finish();
},
- error: () => {
- if (this.cancelled) return;
- this.cancelled = true;
+ error: (error: Error) => {
+ console.error("Error reading import file:", error);
+ this.cancel();
+ void this.finish();
},
});
}
+ private async uploadRows(rows: unknown[]): Promise<void> {
+ if (this.cancelled) {
+ return;
+ }
+
+ if (this.platform === "umami") {
+ const validEvents: UmamiEvent[] = [];
+ for (const row of rows) {
+ const event = this.transformRow(row);
+ if (event && this.isDateInRange((event as UmamiEvent).created_at)) {
+ validEvents.push(event as UmamiEvent);
+ }
+ }
+ if (validEvents.length > 0) {
+ await this.uploadChunk(validEvents, false);
+ }
+ } else {
+ const validEvents: SimpleAnalyticsEvent[] = [];
+ for (const row of rows) {
+ const event = this.transformRow(row);
+ if (event && this.isDateInRange((event as SimpleAnalyticsEvent).added_iso)) {
+ validEvents.push(event as SimpleAnalyticsEvent);
+ }
+ }
+ if (validEvents.length > 0) {
+ await this.uploadChunk(validEvents, false);
+ }
+ }
+ }
+
+ private async finish(): Promise<void> {
+ if (this.finished) {
+ return;
+ }
+ this.finished = true;
+
+ try {
+ // Send final batch to mark import as complete
+ await this.uploadChunk([], true);
+ } catch (error) {
+ console.error("Error completing import:", error);
+ }
+ }
+
private isDateInRange(dateStr: string): boolean {
// Handle both formats: "yyyy-MM-dd HH:mm:ss" (Umami) and ISO (Simple Analytics)
let createdAt = DateTime.fromFormat(dateStr, "yyyy-MM-dd HH:mm:ss", { zone: "utc" });
diff --git a/docker-compose.cloud.yml b/docker-compose.cloud.yml
index ce55fdba4..5c1d19716 100644
--- a/docker-compose.cloud.yml
+++ b/docker-compose.cloud.yml
@@ -129,6 +129,7 @@ services:
# AppSumo configuration
- APPSUMO_CLIENT_ID=${APPSUMO_CLIENT_ID}
- APPSUMO_CLIENT_SECRET=${APPSUMO_CLIENT_SECRET}
+ - APPSUMO_API_KEY=${APPSUMO_API_KEY}
- GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID}
- GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET}
- GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID}
diff --git a/server/src/analytics-script/index.test.ts b/server/src/analytics-script/index.test.ts
index 3ca7728af..8f19b6ce1 100644
--- a/server/src/analytics-script/index.test.ts
+++ b/server/src/analytics-script/index.test.ts
@@ -153,6 +153,23 @@ describe("automatic navigation tracking", () => {
expect(pageviews()).toHaveLength(3);
});
+ it("still tracks when the browser blocks storage", async () => {
+ // Blocked storage (cookies disabled, sandboxed iframes) throws on access.
+ vi.spyOn(window, "localStorage", "get").mockImplementation(() => {
+ throw new DOMException("The operation is insecure.", "SecurityError");
+ });
+ await start();
+ window.rybbit.event("signup");
+ expect(pageviews().map(event => event.type)).toEqual(["pageview", "custom_event"]);
+ });
+
+ it("installs a no-op API and sends nothing when the visitor opted out", async () => {
+ localStorage.setItem("disable-rybbit", "true");
+ await start();
+ window.rybbit.pageview();
+ expect(pageviews()).toHaveLength(0);
+ });
+
it("ignores anchors with initial tracking disabled and still tracks subsequent routes", async () => {
config.autoTrackPageview = false;
await start();
diff --git a/server/src/analytics-script/index.ts b/server/src/analytics-script/index.ts
index 12e5f8909..a5ac68f60 100644
--- a/server/src/analytics-script/index.ts
+++ b/server/src/analytics-script/index.ts
@@ -15,6 +15,16 @@ declare global {
}
}
+function hasOptedOut(optOutKey: string): boolean {
+ if (window.__RYBBIT_OPTOUT__) return true;
+ try {
+ return localStorage.getItem(optOutKey) !== null;
+ } catch {
+ // Storage is blocked (cookies disabled, sandboxed iframe): not opted out.
+ return false;
+ }
+}
+
(async function () {
// Script optimizers that re-inject the tag can leave `document.currentScript`
// null; fall back to locating our own tag by its URL.
@@ -31,7 +41,7 @@ declare global {
const optOutKey = `disable-${namespace}`;
// Check if user has opted out
- if (window.__RYBBIT_OPTOUT__ || localStorage.getItem(optOutKey) !== null) {
+ if (hasOptedOut(optOutKey)) {
// Create no-op implementation
window[namespace] = {
pageview: () => {},
diff --git a/server/src/api/__snapshots__/routes.test.ts.snap b/server/src/api/__snapshots__/routes.test.ts.snap
index b1eda930e..fc4df9be3 100644
--- a/server/src/api/__snapshots__/routes.test.ts.snap
+++ b/server/src/api/__snapshots__/routes.test.ts.snap
@@ -153,7 +153,7 @@ exports[`API route table > pins the access each route requires 1`] = `
"POST /api/sites/:siteId/annotations β†’ site annotations:write [member+]",
"POST /api/sites/:siteId/claim β†’ authenticated",
"POST /api/sites/:siteId/dashboards β†’ site dashboards:write [member+]",
- "POST /api/sites/:siteId/dashboards/run-card β†’ site dashboards:read [viewer+]",
+ "POST /api/sites/:siteId/dashboards/run-card β†’ site sql:read [viewer+]",
"POST /api/sites/:siteId/experiments β†’ site experiments:write [editor+]",
"POST /api/sites/:siteId/feature-flags β†’ site flags:write [editor+]",
"POST /api/sites/:siteId/feature-flags/evaluate β†’ site flags:read [viewer+]",
diff --git a/server/src/api/as/webhook.test.ts b/server/src/api/as/webhook.test.ts
index fbae02c5e..ed16bde12 100644
--- a/server/src/api/as/webhook.test.ts
+++ b/server/src/api/as/webhook.test.ts
@@ -1,3 +1,4 @@
+import { createHmac } from "node:crypto";
import { PgDialect } from "drizzle-orm/pg-core";
import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -34,8 +35,25 @@ function executedQuery(n: number) {
return dialect.sqlToQuery(mocks.execute.mock.calls[n][0]);
}
-function createRequest(body: Record<string, unknown>) {
- return { body, log: requestLogger } as any;
+const API_KEY = "as_api_key";
+const TIMESTAMP = "1761264000000";
+
+function sign(rawBody: Buffer, timestamp = TIMESTAMP, key = API_KEY) {
+ return createHmac("sha256", key)
+ .update(`${timestamp}${rawBody.toString("utf8")}`)
+ .digest("hex");
+}
+
+// Signed the way AppSumo does: HMAC-SHA256 over the timestamp header followed
+// by the raw body, keyed with the partner API key.
+function createRequest(body: Record<string, unknown>, headers?: Record<string, string>) {
+ const rawBody = Buffer.from(JSON.stringify(body));
+ return {
+ body,
+ rawBody,
+ headers: headers ?? { "x-appsumo-timestamp": TIMESTAMP, "x-appsumo-signature": sign(rawBody) },
+ log: requestLogger,
+ } as any;
}
function createReply() {
@@ -51,9 +69,9 @@ function createReply() {
return reply;
}
-async function invoke(body: Record<string, unknown>) {
+async function invoke(body: Record<string, unknown>, headers?: Record<string, string>) {
const reply = createReply();
- await handleAppSumoWebhook(createRequest(body), reply);
+ await handleAppSumoWebhook(createRequest(body, headers), reply);
return reply;
}
@@ -61,6 +79,7 @@ beforeEach(() => {
vi.clearAllMocks();
process.env.APPSUMO_CLIENT_ID = "as_client_id";
process.env.APPSUMO_CLIENT_SECRET = "as_client_secret";
+ process.env.APPSUMO_API_KEY = API_KEY;
mocks.execute.mockResolvedValue([]);
});
@@ -76,6 +95,52 @@ describe("handleAppSumoWebhook β€” availability gate", () => {
});
});
+describe("handleAppSumoWebhook β€” signature", () => {
+ const body = { event: "activate", license_key: "lic_1", tier: 7 };
+
+ it("rejects an unsigned request before any db access", async () => {
+ const reply = await invoke(body, {});
+
+ expect(reply.statusCode).toBe(401);
+ expect(mocks.execute).not.toHaveBeenCalled();
+ });
+
+ it("rejects a signature made with a different key", async () => {
+ const raw = Buffer.from(JSON.stringify(body));
+ const reply = await invoke(body, {
+ "x-appsumo-timestamp": TIMESTAMP,
+ "x-appsumo-signature": sign(raw, TIMESTAMP, "wrong_key"),
+ });
+
+ expect(reply.statusCode).toBe(401);
+ expect(mocks.execute).not.toHaveBeenCalled();
+ });
+
+ it("rejects a valid signature replayed with a different timestamp", async () => {
+ const raw = Buffer.from(JSON.stringify(body));
+ const reply = await invoke(body, { "x-appsumo-timestamp": "1761264000001", "x-appsumo-signature": sign(raw) });
+
+ expect(reply.statusCode).toBe(401);
+ expect(mocks.execute).not.toHaveBeenCalled();
+ });
+
+ it("returns 503 when the API key is not configured", async () => {
+ delete process.env.APPSUMO_API_KEY;
+
+ const reply = await invoke(body);
+
+ expect(reply.statusCode).toBe(503);
+ expect(mocks.execute).not.toHaveBeenCalled();
+ });
+
+ it("accepts a correctly signed request", async () => {
+ const reply = await invoke(body);
+
+ expect(reply.statusCode).toBe(200);
+ expect(reply.payload).toEqual({ event: "activate", success: true });
+ });
+});
+
describe("handleAppSumoWebhook β€” test webhooks", () => {
it.each([
["test flag", { test: true, event: "purchase", license_key: "lic_1" }],
@@ -308,47 +373,31 @@ describe("handleAppSumoWebhook β€” upgrade", () => {
expect(reply.payload).toEqual({ event: "upgrade", success: true });
});
- it("PINNED (suspected bug): unknown prev_license_key falls back to ANY license with an org and transfers that org", async () => {
- // If the previous license is not found, the handler grabs the most
- // recently updated license belonging to ANY organization and attaches the
- // new license to it. On this public, unauthenticated endpoint that means a
- // fabricated upgrade payload with an unknown prev_license_key can mint an
- // active license attached to another customer's organization.
+ it("keeps the new license pending when prev_license_key is unknown instead of guessing an org", async () => {
mocks.execute
.mockResolvedValueOnce([]) // audit insert
.mockResolvedValueOnce([]) // old license lookup: not found
- .mockResolvedValueOnce([{ organization_id: "org_someone_else" }]) // fallback lookup
- .mockResolvedValueOnce([]) // insert new active license
+ .mockResolvedValueOnce([]) // insert pending license
.mockResolvedValueOnce([]); // deactivate (nonexistent) old license
const reply = await invoke({
event: "upgrade",
- license_key: "lic_forged",
+ license_key: "lic_new",
prev_license_key: "lic_unknown",
tier: 5,
});
- const fallback = executedQuery(2);
- expect(fallback.sql).toContain("WHERE organization_id IS NOT NULL");
- expect(fallback.sql).toContain("ORDER BY updated_at DESC");
-
- const insert = executedQuery(3);
- expect(insert.sql).toContain("'active'");
- expect(insert.params).toEqual(["org_someone_else", "lic_forged", "5", "org_someone_else", "5"]);
-
- expect(reply.payload).toEqual({ event: "upgrade", success: true });
- });
-
- it("gives up when neither the previous license nor any fallback license exists", async () => {
- mocks.execute
- .mockResolvedValueOnce([]) // audit insert
- .mockResolvedValueOnce([]) // old license lookup: not found
- .mockResolvedValueOnce([]); // fallback lookup: nothing
-
- const reply = await invoke({ event: "upgrade", license_key: "lic_new", prev_license_key: "lic_gone", tier: 2 });
-
- expect(mocks.execute).toHaveBeenCalledTimes(3);
- // Still acked as success despite doing nothing.
+ expect(mocks.execute).toHaveBeenCalledTimes(4);
+ for (let i = 0; i < 4; i++) {
+ expect(executedQuery(i).sql).not.toContain("WHERE organization_id IS NOT NULL");
+ }
+ const insert = executedQuery(2);
+ expect(insert.sql).toContain("'pending'");
+ // An already activated (org-linked) row keeps its status.
+ expect(insert.sql).toContain(
+ "WHEN appsumo.licenses.organization_id IS NULL THEN 'pending' ELSE appsumo.licenses.status END"
+ );
+ expect(insert.params).toEqual(["lic_new", "5", "5"]);
expect(reply.payload).toEqual({ event: "upgrade", success: true });
});
});
diff --git a/server/src/api/as/webhook.ts b/server/src/api/as/webhook.ts
index 9391ec48d..75bf8ed62 100644
--- a/server/src/api/as/webhook.ts
+++ b/server/src/api/as/webhook.ts
@@ -1,3 +1,4 @@
+import { createHmac, timingSafeEqual } from "node:crypto";
import { sql } from "drizzle-orm";
import { FastifyReply, FastifyRequest } from "fastify";
import type { FastifyBaseLogger } from "fastify";
@@ -41,6 +42,25 @@ function isAppSumoEnabled(): boolean {
return IS_CLOUD && !!process.env.APPSUMO_CLIENT_ID && !!process.env.APPSUMO_CLIENT_SECRET;
}
+/**
+ * AppSumo signs each webhook with HMAC-SHA256 over `${X-Appsumo-Timestamp}${raw body}`,
+ * keyed with the partner API key and hex-encoded in X-Appsumo-Signature.
+ * https://docs.licensing.appsumo.com/webhook/webhook__security.html
+ */
+export function isValidAppSumoSignature(
+ rawBody: Buffer | undefined,
+ timestamp: unknown,
+ signature: unknown,
+ apiKey: string
+): boolean {
+ if (!rawBody || typeof timestamp !== "string" || typeof signature !== "string") {
+ return false;
+ }
+ const expected = createHmac("sha256", apiKey).update(timestamp).update(rawBody).digest();
+ const received = Buffer.from(signature, "hex");
+ return received.length === expected.length && timingSafeEqual(received, expected);
+}
+
/**
* Validate webhook payload
*/
@@ -96,6 +116,27 @@ export async function handleAppSumoWebhook(
});
}
+ // Every other event changes license state, so it must carry AppSumo's signature.
+ const apiKey = process.env.APPSUMO_API_KEY;
+ if (!apiKey) {
+ requestLogger.error("APPSUMO_API_KEY is not set; rejecting webhook");
+ return reply.status(503).send({ error: "AppSumo webhook verification is not configured" });
+ }
+ if (
+ !isValidAppSumoSignature(
+ request.rawBody,
+ request.headers["x-appsumo-timestamp"],
+ request.headers["x-appsumo-signature"],
+ apiKey
+ )
+ ) {
+ requestLogger.warn(
+ { eventType: eventTypeForLog(payload.event) },
+ "Rejected AppSumo webhook with invalid signature"
+ );
+ return reply.status(401).send({ error: "Invalid signature" });
+ }
+
try {
// Validate webhook payload
validateWebhookPayload(payload);
@@ -293,7 +334,7 @@ async function handleUpgradeEvent(
// Get the old license to find the organization
requestLogger.debug("Looking up previous license for upgrade");
- let oldLicenseResult = await db.execute(
+ const oldLicenseResult = await db.execute(
sql`SELECT organization_id FROM appsumo.licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`
);
@@ -302,22 +343,15 @@ async function handleUpgradeEvent(
"Previous license lookup completed"
);
- // If previous license not found, try to find ANY license with an organization (fallback for missed webhooks)
+ // An unknown previous license can't be tied to an organization. The new
+ // license stays pending until the customer activates it; guessing an org
+ // here would hand the plan to someone else.
if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.warn("Previous license not found; searching for an organization fallback");
- oldLicenseResult = await db.execute(
- sql`SELECT organization_id FROM appsumo.licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`
- );
-
- if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.error("No licenses with an organization found; cannot process upgrade");
- return;
- }
- requestLogger.info("Found fallback license for upgrade");
+ requestLogger.warn("Previous license not found; creating the new license as pending");
}
- const oldLicense = oldLicenseResult[0] as any;
- const organizationId = oldLicense.organization_id;
+ const oldLicense = (Array.isArray(oldLicenseResult) ? oldLicenseResult[0] : undefined) as any;
+ const organizationId = oldLicense?.organization_id ?? null;
requestLogger.info({ organizationId }, "Found organization for previous license");
// Create new license with the organization transferred
@@ -377,7 +411,9 @@ async function handleUpgradeEvent(
)
ON CONFLICT (license_key) DO UPDATE SET
tier = ${tierValue},
- status = 'pending',
+ -- /as/activate may have linked this key before the webhook arrived;
+ -- don't drop an activated license back to pending.
+ status = CASE WHEN appsumo.licenses.organization_id IS NULL THEN 'pending' ELSE appsumo.licenses.status END,
updated_at = NOW()
`);
requestLogger.info({ tier: tierValue }, "Successfully created or updated pending license");
@@ -424,7 +460,7 @@ async function handleDowngradeEvent(
// Get the old license to find the organization
requestLogger.debug("Looking up previous license for downgrade");
- let oldLicenseResult = await db.execute(
+ const oldLicenseResult = await db.execute(
sql`SELECT organization_id FROM appsumo.licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`
);
@@ -433,22 +469,15 @@ async function handleDowngradeEvent(
"Previous license lookup completed"
);
- // If previous license not found, try to find ANY license with an organization (fallback for missed webhooks)
+ // An unknown previous license can't be tied to an organization. The new
+ // license stays pending until the customer activates it; guessing an org
+ // here would hand the plan to someone else.
if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.warn("Previous license not found; searching for an organization fallback");
- oldLicenseResult = await db.execute(
- sql`SELECT organization_id FROM appsumo.licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`
- );
-
- if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {
- requestLogger.error("No licenses with an organization found; cannot process downgrade");
- return;
- }
- requestLogger.info("Found fallback license for downgrade");
+ requestLogger.warn("Previous license not found; creating the new license as pending");
}
- const oldLicense = oldLicenseResult[0] as any;
- const organizationId = oldLicense.organization_id;
+ const oldLicense = (Array.isArray(oldLicenseResult) ? oldLicenseResult[0] : undefined) as any;
+ const organizationId = oldLicense?.organization_id ?? null;
requestLogger.info({ organizationId }, "Found organization for previous license");
// Create new license with the organization transferred
@@ -508,7 +537,9 @@ async function handleDowngradeEvent(
)
ON CONFLICT (license_key) DO UPDATE SET
tier = ${tierValue},
- status = 'pending',
+ -- /as/activate may have linked this key before the webhook arrived;
+ -- don't drop an activated license back to pending.
+ status = CASE WHEN appsumo.licenses.organization_id IS NULL THEN 'pending' ELSE appsumo.licenses.status END,
updated_at = NOW()
`);
requestLogger.info({ tier: tierValue }, "Successfully created or updated pending license");
diff --git a/server/src/api/featureFlags/deleteFeatureFlag.test.ts b/server/src/api/featureFlags/deleteFeatureFlag.test.ts
new file mode 100644
index 000000000..9b4bc0a02
--- /dev/null
+++ b/server/src/api/featureFlags/deleteFeatureFlag.test.ts
@@ -0,0 +1,79 @@
+import type { FastifyReply, FastifyRequest } from "fastify";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ findExperiment: vi.fn(),
+ deleteReturning: vi.fn(),
+ invalidateFeatureFlagDefinitions: vi.fn(),
+}));
+
+vi.mock("../../db/postgres/postgres.js", () => ({
+ db: {
+ query: { experiments: { findFirst: mocks.findExperiment } },
+ delete: () => ({ where: () => ({ returning: mocks.deleteReturning }) }),
+ },
+}));
+vi.mock("../../db/clickhouse/clickhouse.js", () => ({ clickhouse: { query: vi.fn() } }));
+vi.mock("../../db/geolocation/geolocation.js", () => ({ getLocation: vi.fn() }));
+vi.mock("../../lib/siteConfig.js", () => ({ siteConfig: { getConfig: vi.fn() } }));
+vi.mock("../../services/featureFlags/definitions.js", () => ({
+ getFeatureFlagDefinitionsForRuntime: vi.fn(),
+ invalidateFeatureFlagDefinitions: mocks.invalidateFeatureFlagDefinitions,
+}));
+
+import { deleteFeatureFlag } from "./index.js";
+
+type DeleteRequest = FastifyRequest<{ Params: { siteId: string; flagId: string } }>;
+
+function requestStub(siteId = "1", flagId = "7") {
+ return { params: { siteId, flagId } } as unknown as DeleteRequest;
+}
+
+function replyStub() {
+ return {
+ status: vi.fn().mockReturnThis(),
+ send: vi.fn().mockReturnThis(),
+ } as unknown as FastifyReply;
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.findExperiment.mockResolvedValue(undefined);
+ mocks.deleteReturning.mockResolvedValue([{ flagId: 7 }]);
+});
+
+describe("deleteFeatureFlag", () => {
+ it("refuses to delete a flag an experiment is built on", async () => {
+ mocks.findExperiment.mockResolvedValue({ name: "Checkout copy" });
+ const reply = replyStub();
+
+ await deleteFeatureFlag(requestStub(), reply);
+
+ expect(reply.status).toHaveBeenCalledWith(409);
+ expect(reply.send).toHaveBeenCalledWith({
+ error: 'This flag is used by experiment "Checkout copy". Delete the experiment first.',
+ });
+ expect(mocks.deleteReturning).not.toHaveBeenCalled();
+ expect(mocks.invalidateFeatureFlagDefinitions).not.toHaveBeenCalled();
+ });
+
+ it("deletes a flag no experiment uses", async () => {
+ const reply = replyStub();
+
+ await deleteFeatureFlag(requestStub(), reply);
+
+ expect(mocks.deleteReturning).toHaveBeenCalled();
+ expect(mocks.invalidateFeatureFlagDefinitions).toHaveBeenCalledWith(1);
+ expect(reply.send).toHaveBeenCalledWith({ success: true });
+ });
+
+ it("returns 404 when the flag does not exist on the site", async () => {
+ mocks.deleteReturning.mockResolvedValue([]);
+ const reply = replyStub();
+
+ await deleteFeatureFlag(requestStub(), reply);
+
+ expect(reply.status).toHaveBeenCalledWith(404);
+ expect(mocks.invalidateFeatureFlagDefinitions).not.toHaveBeenCalled();
+ });
+});
diff --git a/server/src/api/featureFlags/index.ts b/server/src/api/featureFlags/index.ts
index c86db73ff..efc48a43a 100644
--- a/server/src/api/featureFlags/index.ts
+++ b/server/src/api/featureFlags/index.ts
@@ -6,7 +6,7 @@ import { z } from "zod";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { getLocation } from "../../db/geolocation/geolocation.js";
import { db } from "../../db/postgres/postgres.js";
-import { featureFlags, userProfiles } from "../../db/postgres/schema.js";
+import { experiments, featureFlags, userProfiles } from "../../db/postgres/schema.js";
import { siteConfig } from "../../lib/siteConfig.js";
import { processResults } from "../analytics/utils/utils.js";
import { getDeviceType, getRequestUserAgent } from "../../utils.js";
@@ -251,6 +251,19 @@ export async function deleteFeatureFlag(
return reply.status(400).send({ error: "Invalid feature flag ID" });
}
+ // An experiment is cascade-deleted with its flag, so refuse rather than
+ // silently take the experiment down with it.
+ const experiment = await db.query.experiments.findFirst({
+ columns: { name: true },
+ where: and(eq(experiments.siteId, siteId), eq(experiments.featureFlagId, flagId)),
+ });
+
+ if (experiment) {
+ return reply.status(409).send({
+ error: `This flag is used by experiment "${experiment.name}". Delete the experiment first.`,
+ });
+ }
+
const [deleted] = await db
.delete(featureFlags)
.where(and(eq(featureFlags.siteId, siteId), eq(featureFlags.flagId, flagId)))
diff --git a/server/src/api/gsc/callback.ts b/server/src/api/gsc/callback.ts
index e473719b3..1c8e979e3 100644
--- a/server/src/api/gsc/callback.ts
+++ b/server/src/api/gsc/callback.ts
@@ -1,10 +1,11 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { GSCCallbackRequest } from "./types.js";
-import { gscConnections } from "../../db/postgres/schema.js";
+import { gscConnections, sites } from "../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
import { getGSCProperties, verifyGSCState } from "./utils.js";
import { getSessionFromReq, getUserHasSitePermission } from "../../lib/auth-utils.js";
import { db } from "../../db/postgres/postgres.js";
+import { lockSiteOwnership } from "../sites/applySiteMove.js";
interface TokenResponse {
access_token: string;
@@ -51,6 +52,14 @@ export async function gscCallback(req: FastifyRequest<GSCCallbackRequest>, res:
return res.status(403).send({ error: "State does not match session" });
}
+ // Read the owner before authorizing: the tokens are only written if the
+ // site is still in this organization afterwards (see below).
+ const [authorizedSite] = await db
+ .select({ organizationId: sites.organizationId })
+ .from(sites)
+ .where(eq(sites.siteId, siteId))
+ .limit(1);
+
// Verify the caller actually has admin access to the target site before
// writing OAuth tokens against it (prevents IDOR / connection hijack).
const hasAccess = await getUserHasSitePermission(req, siteId, "gsc:write");
@@ -101,31 +110,48 @@ export async function gscCallback(req: FastifyRequest<GSCCallbackRequest>, res:
const expiresAt = new Date(Date.now() + tokens.expires_in * 1000);
// Store tokens temporarily WITHOUT a property URL
- // We'll update with the selected property after user chooses
- const [existingConnection] = await db.select().from(gscConnections).where(eq(gscConnections.siteId, siteId));
-
- if (existingConnection) {
- // Update existing connection with new tokens (but keep old property if it exists)
- await db
- .update(gscConnections)
- .set({
+ // We'll update with the selected property after user chooses.
+ // The Google round trip above can overlap a site move or transfer, which
+ // deletes the connection; writing afterwards would hand this account's
+ // properties to the new organization. Write only while the site, locked,
+ // is still in the organization the caller was authorized against.
+ const stored = await db.transaction(async tx => {
+ const site = await lockSiteOwnership(tx, siteId);
+ if (!site || !authorizedSite || site.organizationId !== authorizedSite.organizationId) {
+ return false;
+ }
+
+ const [existingConnection] = await tx.select().from(gscConnections).where(eq(gscConnections.siteId, siteId));
+
+ if (existingConnection) {
+ // Update existing connection with new tokens (but keep old property if it exists)
+ await tx
+ .update(gscConnections)
+ .set({
+ accessToken: tokens.access_token,
+ refreshToken: tokens.refresh_token,
+ expiresAt: expiresAt.toISOString(),
+ updatedAt: new Date().toISOString(),
+ })
+ .where(eq(gscConnections.siteId, siteId));
+ } else {
+ // Create new connection with temporary placeholder
+ await tx.insert(gscConnections).values({
+ siteId,
accessToken: tokens.access_token,
refreshToken: tokens.refresh_token,
expiresAt: expiresAt.toISOString(),
+ gscPropertyUrl: "PENDING_SELECTION", // Placeholder until user selects
+ createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
- })
- .where(eq(gscConnections.siteId, siteId));
- } else {
- // Create new connection with temporary placeholder
- await db.insert(gscConnections).values({
- siteId,
- accessToken: tokens.access_token,
- refreshToken: tokens.refresh_token,
- expiresAt: expiresAt.toISOString(),
- gscPropertyUrl: "PENDING_SELECTION", // Placeholder until user selects
- createdAt: new Date().toISOString(),
- updatedAt: new Date().toISOString(),
- });
+ });
+ }
+ return true;
+ });
+
+ if (!stored) {
+ req.log.warn({ siteId }, "Site changed organization during the GSC callback; tokens not stored");
+ return res.redirect(`${process.env.BASE_URL}/error?message=The site moved while connecting Search Console`);
}
// Redirect to property selection page with properties as query params
diff --git a/server/src/api/routes.ts b/server/src/api/routes.ts
index a37834de9..a05edea8e 100644
--- a/server/src/api/routes.ts
+++ b/server/src/api/routes.ts
@@ -212,6 +212,7 @@ import {
type RouteScope,
} from "../lib/auth-middleware.js";
import { IS_CLOUD } from "../lib/const.js";
+import { keepRawJsonBody } from "../lib/rawBody.js";
import { mcpRoutes } from "../mcp/index.js";
// Reject requests whose shared time query params are present but invalid.
@@ -409,9 +410,11 @@ async function analyticsRoutes(fastify: FastifyInstance) {
fastify.post("/sites/:siteId/segments", site("segments:write"), createSegment);
fastify.put("/sites/:siteId/segments/:segmentId", site("segments:write"), updateSegment);
fastify.delete("/sites/:siteId/segments/:segmentId", site("segments:write"), deleteSegment);
+ // The card body carries caller-written SQL, so this is a custom query: a
+ // dashboards:read credential alone must not be able to read raw events.
fastify.post(
"/sites/:siteId/dashboards/run-card",
- withRateLimit(site("dashboards:read"), customQueryRateLimit),
+ withRateLimit(site("sql:read"), customQueryRateLimit),
runDashboardCardQuery
);
fastify.get("/sites/:siteId/feature-flags", site("flags:read"), getFeatureFlags);
@@ -604,14 +607,22 @@ async function stripeAdminRoutes(fastify: FastifyInstance) {
fastify.get("/stripe/subscription", authenticated("deny-scoped"), getSubscription);
fastify.get("/stripe/invoices", authenticated("deny-scoped"), getInvoices);
fastify.post("/stripe/cancellation-feedback", authenticated("deny-scoped"), submitCancellationFeedback);
- fastify.post("/stripe/webhook", { config: { rawBody: true, access: "public" as RouteAccess } }, handleWebhook); // Public - Stripe webhook
// AppSumo Routes
fastify.post("/as/activate", authenticated("deny-scoped"), activateAppSumoLicense);
- fastify.post("/as/webhook", publicRoute, handleAppSumoWebhook); // Public - AppSumo webhook
+
+ await fastify.register(webhookRoutes);
}
}
+// Public webhooks. Both senders sign the raw request bytes, so this scope keeps
+// them on request.rawBody for the handlers' signature checks.
+async function webhookRoutes(fastify: FastifyInstance) {
+ keepRawJsonBody(fastify);
+ fastify.post("/stripe/webhook", publicRoute, handleWebhook);
+ fastify.post("/as/webhook", publicRoute, handleAppSumoWebhook);
+}
+
// Main API routes plugin - registers all domain plugins
export async function apiRoutes(fastify: FastifyInstance) {
fastify.addHook("onRoute", assertRouteAccessDeclared);
diff --git a/server/src/api/sites/applySiteMove.ts b/server/src/api/sites/applySiteMove.ts
index 714c4522e..1a3ec6ab6 100644
--- a/server/src/api/sites/applySiteMove.ts
+++ b/server/src/api/sites/applySiteMove.ts
@@ -2,6 +2,7 @@ import { and, eq, isNull } from "drizzle-orm";
import { db } from "../../db/postgres/postgres.js";
import {
annotations,
+ gscConnections,
importStatus,
memberSiteAccess,
segments,
@@ -81,6 +82,10 @@ export async function applySiteMove(
await tx.update(importStatus).set({ organizationId: targetOrganizationId }).where(eq(importStatus.siteId, siteId));
// A pending hand-over was authorized by the old organization.
await tx.delete(siteTransfers).where(eq(siteTransfers.siteId, siteId));
+ // The Search Console connection holds the Google credentials of whoever
+ // connected it in the old organization; editors in the new one could
+ // otherwise browse and select any property that account can see.
+ await tx.delete(gscConnections).where(eq(gscConnections.siteId, siteId));
return true;
};
if (transaction) return move(transaction);
diff --git a/server/src/api/sites/batchImportEvents.ts b/server/src/api/sites/batchImportEvents.ts
index 31f7bee75..e6d1b14e8 100644
--- a/server/src/api/sites/batchImportEvents.ts
+++ b/server/src/api/sites/batchImportEvents.ts
@@ -11,6 +11,7 @@ import { organization, sites } from "../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
import { getBestSubscription } from "../../lib/subscriptionUtils.js";
import { IS_CLOUD } from "../../lib/const.js";
+import { EVENTS_INSERT_SETTINGS } from "../../services/tracker/utils.js";
const batchImportRequestSchema = z
.object({
@@ -107,6 +108,11 @@ export async function batchImportEvents(request: FastifyRequest<BatchImportReque
table: "events",
values: eventsWithinQuota,
format: "JSONEachRow",
+ // Merge conflicting JSON paths, but don't let ClickHouse skip rows here:
+ // the counts below assume every submitted row was stored.
+ clickhouse_settings: {
+ type_json_skip_duplicated_paths: EVENTS_INSERT_SETTINGS.type_json_skip_duplicated_paths,
+ },
});
}
diff --git a/server/src/api/sites/deleteSiteImport.test.ts b/server/src/api/sites/deleteSiteImport.test.ts
new file mode 100644
index 000000000..03364801d
--- /dev/null
+++ b/server/src/api/sites/deleteSiteImport.test.ts
@@ -0,0 +1,93 @@
+import { DateTime } from "luxon";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ getImportById: vi.fn(),
+ deleteImport: vi.fn(async () => {}),
+ chCommand: vi.fn(async () => {}),
+}));
+
+vi.mock("../../services/import/importStatusManager.js", () => ({
+ getImportById: mocks.getImportById,
+ deleteImport: mocks.deleteImport,
+}));
+
+vi.mock("../../db/clickhouse/clickhouse.js", () => ({
+ clickhouse: { command: mocks.chCommand },
+}));
+
+vi.mock("../../db/postgres/postgres.js", () => ({ db: {} }));
+vi.mock("../../lib/subscriptionUtils.js", () => ({ getBestSubscription: vi.fn() }));
+vi.mock("../../services/import/importQuotaTracker.js", () => ({ ImportQuotaTracker: {} }));
+vi.mock("../../lib/const.js", () => ({ IS_CLOUD: false }));
+
+import { deleteSiteImport } from "./deleteSiteImport.js";
+
+const SITE_ID = 7;
+const IMPORT_ID = "6c1f3a52-0d7e-4f4b-9a8e-2b6f8f0c1d23";
+
+function createReply() {
+ const reply = {
+ statusCode: 200,
+ body: undefined as unknown,
+ status(code: number) {
+ reply.statusCode = code;
+ return reply;
+ },
+ send(body?: unknown) {
+ reply.body = body;
+ return reply;
+ },
+ };
+ return reply;
+}
+
+// startedAt as Postgres returns a `timestamp` column in string mode.
+const startedHoursAgo = (hours: number) => DateTime.utc().minus({ hours }).toFormat("yyyy-MM-dd HH:mm:ss.SSS");
+
+async function deleteWith(record: { startedAt: string; completedAt: string | null }) {
+ mocks.getImportById.mockResolvedValue({
+ importId: IMPORT_ID,
+ siteId: SITE_ID,
+ organizationId: "org_1",
+ ...record,
+ });
+ const reply = createReply();
+ await deleteSiteImport(
+ { params: { siteId: String(SITE_ID), importId: IMPORT_ID }, log: { error: vi.fn() } } as any,
+ reply as any
+ );
+ return reply;
+}
+
+describe("deleteSiteImport", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("deletes a completed import", async () => {
+ const reply = await deleteWith({ startedAt: startedHoursAgo(0.5), completedAt: startedHoursAgo(0.1) });
+
+ expect(reply.statusCode).toBe(200);
+ expect(mocks.chCommand).toHaveBeenCalledTimes(1);
+ expect(mocks.deleteImport).toHaveBeenCalledWith(IMPORT_ID);
+ });
+
+ it("refuses to delete an incomplete import that may still be uploading", async () => {
+ const reply = await deleteWith({ startedAt: startedHoursAgo(1), completedAt: null });
+
+ expect(reply.statusCode).toBe(400);
+ expect(reply.body).toEqual({ error: "Cannot delete active import" });
+ expect(mocks.chCommand).not.toHaveBeenCalled();
+ expect(mocks.deleteImport).not.toHaveBeenCalled();
+ });
+
+ it("deletes an incomplete import once it is past the import timeout", async () => {
+ // A client that never sent its final batch (tab closed, network lost) leaves completedAt null.
+ const reply = await deleteWith({ startedAt: startedHoursAgo(3), completedAt: null });
+
+ expect(reply.statusCode).toBe(200);
+ expect(mocks.chCommand).toHaveBeenCalledTimes(1);
+ expect(mocks.deleteImport).toHaveBeenCalledWith(IMPORT_ID);
+ });
+});
diff --git a/server/src/api/sites/deleteSiteImport.ts b/server/src/api/sites/deleteSiteImport.ts
index 6118509a0..0897c8195 100644
--- a/server/src/api/sites/deleteSiteImport.ts
+++ b/server/src/api/sites/deleteSiteImport.ts
@@ -1,8 +1,9 @@
import { FastifyReply, FastifyRequest } from "fastify";
+import { DateTime } from "luxon";
import { z } from "zod";
import { getImportById, deleteImport } from "../../services/import/importStatusManager.js";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { importQuotaManager } from "../../services/import/importQuotaManager.js";
+import { IMPORT_TIMEOUT_MS, importQuotaManager } from "../../services/import/importQuotaManager.js";
import { db } from "../../db/postgres/postgres.js";
import { organization, sites } from "../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
@@ -22,6 +23,13 @@ type DeleteImportRequest = {
Params: z.infer<typeof deleteImportRequestSchema.shape.params>;
};
+// startedAt is Postgres text ("2026-10-24 09:15:00.123") written in UTC.
+function isAbandoned(startedAt: string): boolean {
+ const sql = DateTime.fromSQL(startedAt, { zone: "utc" });
+ const started = sql.isValid ? sql : DateTime.fromISO(startedAt, { zone: "utc" });
+ return started.isValid && DateTime.utc().diff(started).toMillis() > IMPORT_TIMEOUT_MS;
+}
+
export async function deleteSiteImport(request: FastifyRequest<DeleteImportRequest>, reply: FastifyReply) {
try {
const parsed = deleteImportRequestSchema.safeParse({
@@ -43,7 +51,10 @@ export async function deleteSiteImport(request: FastifyRequest<DeleteImportReque
return reply.status(403).send({ error: "Import does not belong to this site" });
}
- if (importRecord.completedAt === null) {
+ // Only the client's final batch completes an import, so one whose client stopped early would
+ // otherwise be undeletable. Judged from startedAt in Postgres rather than the in-memory import
+ // lock, which only the worker that created the import holds (CLUSTER_WORKERS) and a restart drops.
+ if (importRecord.completedAt === null && !isAbandoned(importRecord.startedAt)) {
return reply.status(400).send({ error: "Cannot delete active import" });
}
diff --git a/server/src/api/sites/siteTransfers.test.ts b/server/src/api/sites/siteTransfers.test.ts
index b449d2899..4bf26fd3e 100644
--- a/server/src/api/sites/siteTransfers.test.ts
+++ b/server/src/api/sites/siteTransfers.test.ts
@@ -43,13 +43,14 @@ import {
// Only the columns these handlers and applySiteMove touch.
const DDL = `
CREATE TABLE "organization" ("id" text PRIMARY KEY, "name" text NOT NULL);
-CREATE TABLE "user" ("id" text PRIMARY KEY, "email" text NOT NULL, "emailVerified" boolean NOT NULL DEFAULT false);
+CREATE TABLE "user" ("id" text PRIMARY KEY, "email" text NOT NULL, "emailVerified" boolean NOT NULL DEFAULT false, "role" text);
CREATE TABLE "sites" (
"site_id" serial PRIMARY KEY,
"name" text NOT NULL,
"domain" text NOT NULL,
"organization_id" text REFERENCES "organization"("id"),
- "updated_at" timestamp
+ "updated_at" timestamp,
+ "private_link_key" text
);
CREATE TABLE "member" (
"id" text PRIMARY KEY,
@@ -118,9 +119,9 @@ beforeEach(async () => {
TRUNCATE "site_transfers", "gsc_connections", "import_status", "annotations", "segments",
"team_site_access", "member_site_access", "member", "sites", "user", "organization" RESTART IDENTITY;
INSERT INTO "organization" VALUES ('org_agency', 'Parcero'), ('org_client', 'TAY'), ('org_other', 'Other');
- INSERT INTO "user" VALUES
+ INSERT INTO "user" ("id", "email", "emailVerified") VALUES
('barnaby', '[email protected]', true), ('tay', '[email protected]', true), ('mallory', '[email protected]', true);
- INSERT INTO "sites" ("name", "domain", "organization_id") VALUES ('TAY', 'tay.example.org', 'org_agency');
+ INSERT INTO "sites" ("name", "domain", "organization_id", "private_link_key") VALUES ('TAY', 'tay.example.org', 'org_agency', 'agency-shared-key');
INSERT INTO "member" ("id", "organizationId", "userId", "role") VALUES
('m_barnaby', 'org_agency', 'barnaby', 'owner'),
('m_tay_client', 'org_client', 'tay', 'owner'),
@@ -251,6 +252,7 @@ describe("the recipient", () => {
expect(await rows(`SELECT * FROM member_site_access`)).toEqual([]);
expect(await rows(`SELECT * FROM site_transfers`)).toEqual([]);
expect(await rows(`SELECT * FROM gsc_connections`)).toEqual([]);
+ expect(await rows(`SELECT private_link_key FROM sites`)).toEqual([{ private_link_key: null }]);
// Organization keys of both sides stop seeing the old site list.
expect(mocks.invalidateOrganizationSitesCache).toHaveBeenCalledWith("org_agency");
expect(mocks.invalidateOrganizationSitesCache).toHaveBeenCalledWith("org_client");
@@ -349,6 +351,37 @@ describe("the recipient", () => {
expect(reply.statusCode).toBe(404);
});
+ it("is refused once the sender has lost the right to transfer the site", async () => {
+ // An admin about to be removed sends the site to their own address, then
+ // tries to accept after the removal.
+ const { body } = await startTransfer();
+ await (pgClient as any).exec(`DELETE FROM member WHERE id = 'm_barnaby'`);
+ const reply = replyStub();
+
+ await acceptSiteTransfer(
+ request({ params: { transferId: body.id }, body: { organizationId: "org_client" }, user: { id: "tay" } }),
+ reply
+ );
+
+ expect(reply.statusCode).toBe(403);
+ expect(await rows(`SELECT organization_id FROM sites`)).toEqual([{ organization_id: "org_agency" }]);
+ expect(await rows(`SELECT * FROM site_transfers`)).toEqual([]);
+ });
+
+ it("is refused once the sender has been demoted below admin", async () => {
+ const { body } = await startTransfer();
+ await (pgClient as any).exec(`UPDATE member SET role = 'member' WHERE id = 'm_barnaby'`);
+ const reply = replyStub();
+
+ await acceptSiteTransfer(
+ request({ params: { transferId: body.id }, body: { organizationId: "org_client" }, user: { id: "tay" } }),
+ reply
+ );
+
+ expect(reply.statusCode).toBe(403);
+ expect(await rows(`SELECT organization_id FROM sites`)).toEqual([{ organization_id: "org_agency" }]);
+ });
+
it("respects the receiving organization's site limit on cloud", async () => {
state.isCloud = true;
state.siteLimit = 0;
diff --git a/server/src/api/sites/siteTransfers.ts b/server/src/api/sites/siteTransfers.ts
index 4d9beb2fe..fe6f41026 100644
--- a/server/src/api/sites/siteTransfers.ts
+++ b/server/src/api/sites/siteTransfers.ts
@@ -4,13 +4,13 @@ import { eq } from "drizzle-orm";
import { FastifyReply, FastifyRequest } from "fastify";
import { z } from "zod";
import { db } from "../../db/postgres/postgres.js";
-import { gscConnections, organization, sites, siteTransfers, user } from "../../db/postgres/schema.js";
+import { organization, sites, siteTransfers, user } from "../../db/postgres/schema.js";
import { effectiveOrgRole, getOrgMembership } from "../../lib/access.js";
import { getIsUserAdmin } from "../../lib/auth-utils.js";
import { IS_CLOUD } from "../../lib/const.js";
import { sendSiteTransferEmail } from "../../lib/email/email.js";
import { claimExpiryIso as utcIso } from "../../services/sites/claimExpiry.js";
-import { withOrganizationSiteLock } from "../../services/sites/withOrganizationSiteLock.js";
+import { type SiteTransaction, withOrganizationSiteLock } from "../../services/sites/withOrganizationSiteLock.js";
import { applySiteMove, invalidateSiteMoveAccess, lockSiteOwnership } from "./applySiteMove.js";
import { getPlanSiteLimit, targetSiteLimitError } from "./siteLimit.js";
@@ -46,6 +46,19 @@ const transferUrl = (transferId: string) => `${process.env.BASE_URL ?? ""}/trans
// Postgres hands timestamps back without a zone; they are UTC.
const isExpired = (transfer: Pick<TransferRow, "expiresAt">) => Date.parse(utcIso(transfer.expiresAt)!) <= Date.now();
+/** Whether the transfer's sender still holds sites:transfer on the source organization. */
+async function senderCanStillTransfer(tx: SiteTransaction, senderId: string | null, sourceOrganizationId: string) {
+ if (!senderId) {
+ return false;
+ }
+ const [sender] = await tx.select({ role: user.role }).from(user).where(eq(user.id, senderId)).limit(1);
+ const role = higherRole(
+ effectiveOrgRole(await getOrgMembership(senderId, sourceOrganizationId, tx)),
+ sender?.role === "admin" ? "admin" : null
+ );
+ return roleHasPermission(role, "sites:transfer");
+}
+
function serializeForSender(transfer: TransferRow) {
return {
id: transfer.id,
@@ -327,6 +340,14 @@ export async function acceptSiteTransfer(
return { status: 404, error: "This transfer link is no longer valid" };
}
+ // The sender's authority was checked when the link was created; it must
+ // still hold now. Otherwise an admin who was demoted or removed could
+ // send a link to themselves and take the site with them days later.
+ if (!(await senderCanStillTransfer(tx, current.createdBy, current.sourceOrganizationId))) {
+ await tx.delete(siteTransfers).where(eq(siteTransfers.id, transfer.id));
+ return { status: 403, error: "The person who sent this transfer can no longer transfer the site" };
+ }
+
const limitError = await targetSiteLimitError(tx, targetOrganizationId, siteLimit);
if (limitError) {
return { status: 403, error: limitError };
@@ -338,9 +359,11 @@ export async function acceptSiteTransfer(
await tx.delete(siteTransfers).where(eq(siteTransfers.id, transfer.id));
return { status: 409, error: "The site has moved since this transfer was sent" };
}
- // The Search Console connection holds the sender's Google credentials;
- // it does not go to someone else's organization.
- await tx.delete(gscConnections).where(eq(gscConnections.siteId, transfer.siteId));
+ // The site now belongs to someone else. A private link the old
+ // organization shared would keep exposing its analytics and replays, so
+ // the new owner starts without one. (applySiteMove already dropped the
+ // sender's Search Console connection.)
+ await tx.update(sites).set({ privateLinkKey: null }).where(eq(sites.siteId, transfer.siteId));
return null;
});
diff --git a/server/src/api/stripe/webhook.test.ts b/server/src/api/stripe/webhook.test.ts
index b74421c3c..e88d388b8 100644
--- a/server/src/api/stripe/webhook.test.ts
+++ b/server/src/api/stripe/webhook.test.ts
@@ -71,7 +71,8 @@ function createRequest(overrides: RequestOverrides = {}) {
return {
headers: overrides.headers ?? { "stripe-signature": SIGNATURE },
body: overrides.parsedBody,
- raw: rawBody === null ? {} : { body: rawBody },
+ // The webhook route's parser (keepRawJsonBody) puts the exact bytes here.
+ rawBody: rawBody === null ? undefined : Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(rawBody ?? ""),
log: requestLogger,
} as any;
}
@@ -126,7 +127,7 @@ describe("handleWebhook β€” signature verification", () => {
const reply = createReply();
await handleWebhook(createRequest({ headers: {} }), reply);
- expect(mocks.constructEvent).toHaveBeenCalledWith('{"id":"evt_raw"}', undefined, "whsec_test_secret");
+ expect(mocks.constructEvent).toHaveBeenCalledWith(Buffer.from('{"id":"evt_raw"}'), undefined, "whsec_test_secret");
expect(reply.statusCode).toBe(400);
expect(reply.payload).toContain("Webhook Error");
expectNoDbWrites();
@@ -173,9 +174,10 @@ describe("handleWebhook β€” signature verification", () => {
await handleWebhook(createRequest({ rawBody, parsedBody: { id: "evt_1", type: "tampered.event" } }), reply);
expect(mocks.constructEvent).toHaveBeenCalledTimes(1);
- expect(mocks.constructEvent).toHaveBeenCalledWith(rawBody, SIGNATURE, "whsec_test_secret");
+ expect(mocks.constructEvent).toHaveBeenCalledWith(Buffer.from(rawBody), SIGNATURE, "whsec_test_secret");
const [firstArg] = mocks.constructEvent.mock.calls[0];
- expect(typeof firstArg).toBe("string");
+ expect(Buffer.isBuffer(firstArg)).toBe(true);
+ expect(firstArg.toString("utf8")).toBe(rawBody);
expect(firstArg).not.toEqual({ id: "evt_1", type: "tampered.event" });
});
diff --git a/server/src/api/stripe/webhook.ts b/server/src/api/stripe/webhook.ts
index 05b237e7c..df067af37 100644
--- a/server/src/api/stripe/webhook.ts
+++ b/server/src/api/stripe/webhook.ts
@@ -22,8 +22,8 @@ export async function handleWebhook(request: FastifyRequest, reply: FastifyReply
let event: Stripe.Event;
try {
- // Use rawBody instead of request.body for signature verification
- const rawBody = (request.raw as any).body;
+ // Stripe signs the exact request bytes; the route's parser keeps them.
+ const rawBody = request.rawBody;
if (!rawBody) {
return reply.status(400).send("Webhook error: No raw body available");
}
diff --git a/server/src/index.ts b/server/src/index.ts
index 760e1b547..b10d0cc64 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -50,15 +50,6 @@ server.register(cors, {
});
server.addHook("onRequest", createRejectUntrustedOriginHook());
-// @fastify/rate-limit types FastifyContextConfig, which makes the homegrown
-// `rawBody` route flag (read by the Stripe webhook body parser) a type error
-// unless it is declared alongside.
-declare module "fastify" {
- interface FastifyContextConfig {
- rawBody?: boolean;
- }
-}
-
// Opt-in per-route rate limiting (routes declare config.rateLimit). Runs in
// preHandler so authenticated routes can key on the user instead of the IP.
server.register(rateLimit, {
diff --git a/server/src/lib/auth.ts b/server/src/lib/auth.ts
index fe84581d1..7a433fd46 100644
--- a/server/src/lib/auth.ts
+++ b/server/src/lib/auth.ts
@@ -5,7 +5,7 @@ import { createAccessControl } from "better-auth/plugins/access";
import { adminAc, defaultStatements, memberAc, ownerAc } from "better-auth/plugins/organization/access";
import { createOAuthPlugins, getAuthBaseUrl } from "./oauth.js";
import dotenv from "dotenv";
-import { and, asc, eq, inArray } from "drizzle-orm";
+import { and, asc, eq, inArray, ne } from "drizzle-orm";
import pg from "pg";
import { dash } from "@better-auth/infra";
import { apiKey } from "@better-auth/api-key";
@@ -29,6 +29,7 @@ import {
sendWelcomeEmail,
} from "./email/email.js";
import { getTrustedCorsOrigins } from "./cors.js";
+import { hasRenewingStripeSubscription } from "./subscriptionUtils.js";
import { createServiceLogger } from "./logger/logger.js";
dotenv.config();
@@ -105,6 +106,10 @@ const pluginList = [
},
organizationHooks: {
beforeDeleteOrganization: async ({ organization: org }) => {
+ await assertOrganizationNotBilling(
+ org.id,
+ "Cancel this organization's subscription in Billing before deleting it."
+ );
// apikey.referenceId has no FK (it holds user OR org ids), so
// org-owned keys are purged explicitly. Runs BEFORE the deletion and
// lets failures propagate: a failed purge aborts the deletion instead
@@ -170,6 +175,18 @@ const pluginList = [
},
};
},
+ afterAcceptInvitation: async ({ invitation: accepted, member: newMember, user: acceptingUser }) => {
+ // Runs only after better-auth accepted a pending invitation and created
+ // the membership. (A generic after-hook on /accept-invitation also runs
+ // for rejected requests, which let a member replay an old invitation id
+ // to win back sites an admin had since taken away.)
+ try {
+ await applyInvitationSiteAccess(accepted.id, newMember.id);
+ invalidateSitesAccessCache(acceptingUser.id);
+ } catch (error) {
+ authLogger.error({ err: error }, "Error applying invitation Site restrictions");
+ }
+ },
afterRemoveMember: async ({ member: removedMember, user: removedUser, organization: org }) => {
// Clear any pending/accepted invitations for this user+org so a stale
// invite can't be re-accepted and recreate access after removal.
@@ -216,29 +233,40 @@ const pluginList = [
},
},
},
+ // Billing and usage fields are server-owned. `input: false` keeps them
+ // out of the /organization/create and /update body schemas; without it
+ // any signed-in user could create an org with a plan override, a custom
+ // plan, or another org's Stripe customer id. stripeCustomerId is also
+ // withheld from org reads so members can't copy it.
organization: {
additionalFields: {
stripeCustomerId: {
type: "string",
required: false,
+ input: false,
+ returned: false,
},
monthlyEventCount: {
type: "number",
required: false,
defaultValue: 0,
+ input: false,
},
overMonthlyLimit: {
type: "boolean",
required: false,
defaultValue: false,
+ input: false,
},
planOverride: {
type: "string",
required: false,
+ input: false,
},
customPlan: {
type: "string",
required: false,
+ input: false,
// Column is snake_case (jsonb "custom_plan" in schema.ts). Without
// this, Better Auth 1.7's startup schema check looks for a
// "customPlan" column, reports SCHEMA_MISMATCH, and every
@@ -266,6 +294,88 @@ const pluginList = [
];
// Duck-typed: the api-key plugin can resolve its own copy of APIError.
+/**
+ * Refuses to remove an organization's last way to manage billing while its
+ * Stripe subscription would keep charging.
+ */
+async function assertOrganizationNotBilling(organizationId: string, message: string) {
+ if (!IS_CLOUD) return;
+ const [org] = await db
+ .select({ stripeCustomerId: schema.organization.stripeCustomerId })
+ .from(schema.organization)
+ .where(eq(schema.organization.id, organizationId))
+ .limit(1);
+ let renewing: boolean;
+ try {
+ renewing = await hasRenewingStripeSubscription(org?.stripeCustomerId ?? null);
+ } catch (error) {
+ authLogger.error({ err: error, organizationId }, "Could not check the subscription before deletion");
+ throw new APIError("SERVICE_UNAVAILABLE", {
+ message: "Couldn't check the subscription right now. Try again in a minute.",
+ });
+ }
+ if (renewing) {
+ throw new APIError("BAD_REQUEST", { message });
+ }
+}
+
+/**
+ * Copies an accepted invitation's site restriction onto the new member.
+ */
+async function applyInvitationSiteAccess(invitationId: string, memberId: string) {
+ const invitationRecord = await db
+ .select({
+ organizationId: invitation.organizationId,
+ hasRestrictedSiteAccess: invitation.hasRestrictedSiteAccess,
+ siteIds: invitation.siteIds,
+ siteRole: invitation.siteRole,
+ })
+ .from(invitation)
+ .where(eq(invitation.id, invitationId))
+ .limit(1);
+
+ if (invitationRecord.length === 0) return;
+ const { organizationId, hasRestrictedSiteAccess, siteIds, siteRole } = invitationRecord[0];
+ if (!hasRestrictedSiteAccess) return;
+
+ // Fail-safe ordering: flip the member to restricted BEFORE inserting the
+ // granted-site rows. If the insert step then fails, the member is left
+ // with hasRestrictedSiteAccess=true and zero rows in memberSiteAccess β€”
+ // i.e. locked out, which is safe. The previous transaction-based
+ // implementation would silently leave the member unrestricted (full
+ // org access) on any failure.
+ await db.update(member).set({ hasRestrictedSiteAccess: true }).where(eq(member.id, memberId));
+
+ // The ids were validated when the invitation was written, but a site
+ // can move organizations or be deleted while the invite sits pending
+ // (applySiteMove clears live grants for a moved site; it cannot reach
+ // into unaccepted invitations). Re-check against the organization as
+ // it stands now, so acceptance can only grant sites it still owns.
+ const invitedSiteIds = (siteIds || []) as number[];
+ const grantableSiteIds = await siteIdsInOrganization(invitedSiteIds, organizationId);
+
+ if (grantableSiteIds.length !== invitedSiteIds.length) {
+ authLogger.warn(
+ {
+ organizationId,
+ memberId,
+ droppedSiteIds: invitedSiteIds.filter(siteId => !grantableSiteIds.includes(siteId)),
+ },
+ "Invitation named sites the organization no longer owns; those grants were dropped"
+ );
+ }
+
+ if (grantableSiteIds.length > 0) {
+ await db.insert(memberSiteAccess).values(
+ grantableSiteIds.map(siteId => ({
+ memberId,
+ siteId,
+ role: isSiteGrantRole(siteRole) ? siteRole : null,
+ }))
+ );
+ }
+}
+
function isApiErrorLike(value: unknown): boolean {
return (
typeof value === "object" && value !== null && typeof (value as { statusCode?: unknown }).statusCode === "number"
@@ -328,6 +438,33 @@ export const auth = betterAuth({
},
deleteUser: {
enabled: true,
+ // Membership rows go with the user, so an organization they solely own
+ // would be left with a live subscription and nobody able to cancel it.
+ beforeDelete: async deletingUser => {
+ const owned = await db
+ .select({ organizationId: member.organizationId, name: schema.organization.name })
+ .from(member)
+ .innerJoin(schema.organization, eq(schema.organization.id, member.organizationId))
+ .where(and(eq(member.userId, deletingUser.id), eq(member.role, "owner")));
+ for (const { organizationId, name } of owned) {
+ const [otherOwner] = await db
+ .select({ id: member.id })
+ .from(member)
+ .where(
+ and(
+ eq(member.organizationId, organizationId),
+ eq(member.role, "owner"),
+ ne(member.userId, deletingUser.id)
+ )
+ )
+ .limit(1);
+ if (otherOwner) continue;
+ await assertOrganizationNotBilling(
+ organizationId,
+ `You're the only owner of "${name}", which has an active subscription. Cancel it in Billing or make someone else an owner before deleting your account.`
+ );
+ }
+ },
// apikey.referenceId no longer has a cascading FK to user.id (it holds
// user OR org ids), so the user's keys are purged explicitly. Keys of a
// user removed through other paths are unusable anyway β€” bearer auth
@@ -505,83 +642,6 @@ export const auth = betterAuth({
}
}),
after: createAuthMiddleware(async ctx => {
- // Handle invitation acceptance - copy site access from invitation to member
- if (ctx.path === "/organization/accept-invitation") {
- const body = ctx.body as { invitationId?: string } | null;
- const invitationId = body?.invitationId;
- if (!invitationId) return;
-
- try {
- const invitationRecord = await db
- .select({
- organizationId: invitation.organizationId,
- email: invitation.email,
- hasRestrictedSiteAccess: invitation.hasRestrictedSiteAccess,
- siteIds: invitation.siteIds,
- siteRole: invitation.siteRole,
- })
- .from(invitation)
- .where(eq(invitation.id, invitationId))
- .limit(1);
-
- if (invitationRecord.length === 0) return;
- const { organizationId, email, hasRestrictedSiteAccess, siteIds, siteRole } = invitationRecord[0];
- if (!hasRestrictedSiteAccess) return;
-
- const userRecord = await db.select({ id: user.id }).from(user).where(eq(user.email, email)).limit(1);
- if (userRecord.length === 0) return;
-
- const memberRecord = await db
- .select({ id: member.id })
- .from(member)
- .where(and(eq(member.organizationId, organizationId), eq(member.userId, userRecord[0].id)))
- .limit(1);
- if (memberRecord.length === 0) return;
- const memberId = memberRecord[0].id;
-
- // Fail-safe ordering: flip the member to restricted BEFORE inserting the
- // granted-site rows. If the insert step then fails, the member is left
- // with hasRestrictedSiteAccess=true and zero rows in memberSiteAccess β€”
- // i.e. locked out, which is safe. The previous transaction-based
- // implementation would silently leave the member unrestricted (full
- // org access) on any failure.
- await db.update(member).set({ hasRestrictedSiteAccess: true }).where(eq(member.id, memberId));
-
- // The ids were validated when the invitation was written, but a site
- // can move organizations or be deleted while the invite sits pending
- // (applySiteMove clears live grants for a moved site; it cannot reach
- // into unaccepted invitations). Re-check against the organization as
- // it stands now, so acceptance can only grant sites it still owns.
- const invitedSiteIds = (siteIds || []) as number[];
- const grantableSiteIds = await siteIdsInOrganization(invitedSiteIds, organizationId);
-
- if (grantableSiteIds.length !== invitedSiteIds.length) {
- authLogger.warn(
- {
- organizationId,
- memberId,
- droppedSiteIds: invitedSiteIds.filter(siteId => !grantableSiteIds.includes(siteId)),
- },
- "Invitation named sites the organization no longer owns; those grants were dropped"
- );
- }
-
- if (grantableSiteIds.length > 0) {
- await db.insert(memberSiteAccess).values(
- grantableSiteIds.map(siteId => ({
- memberId,
- siteId,
- role: isSiteGrantRole(siteRole) ? siteRole : null,
- }))
- );
- }
-
- invalidateSitesAccessCache(userRecord[0].id);
- } catch (error) {
- authLogger.error({ err: error }, "Error applying invitation Site restrictions");
- }
- }
-
// Handle self-removal via /organization/leave. Better-auth does NOT call
// organizationHooks.afterRemoveMember for this path, so the cleanup
// (invitation purge + access-cache invalidation) has to live here.
diff --git a/server/src/lib/rawBody.test.ts b/server/src/lib/rawBody.test.ts
new file mode 100644
index 000000000..0e9bab49f
--- /dev/null
+++ b/server/src/lib/rawBody.test.ts
@@ -0,0 +1,51 @@
+import Fastify from "fastify";
+import { describe, expect, it } from "vitest";
+import { keepRawJsonBody } from "./rawBody.js";
+
+async function buildServer() {
+ const server = Fastify();
+ await server.register(async scope => {
+ keepRawJsonBody(scope);
+ scope.post("/hook", async request => ({ body: request.body, raw: request.rawBody?.toString("utf8") }));
+ });
+ server.post("/other", async request => ({ hasRaw: request.rawBody !== undefined }));
+ return server;
+}
+
+describe("keepRawJsonBody", () => {
+ it("parses JSON and keeps the exact bytes, whitespace included", async () => {
+ const server = await buildServer();
+ const payload = '{ "id":"evt_1", "n": 1 }';
+
+ const response = await server.inject({
+ method: "POST",
+ url: "/hook",
+ headers: { "content-type": "application/json; charset=utf-8" },
+ payload,
+ });
+
+ expect(response.statusCode).toBe(200);
+ expect(response.json()).toEqual({ body: { id: "evt_1", n: 1 }, raw: payload });
+ });
+
+ it("answers 400 for malformed JSON", async () => {
+ const server = await buildServer();
+
+ const response = await server.inject({
+ method: "POST",
+ url: "/hook",
+ headers: { "content-type": "application/json" },
+ payload: "{nope",
+ });
+
+ expect(response.statusCode).toBe(400);
+ });
+
+ it("leaves routes outside its scope on the default parser", async () => {
+ const server = await buildServer();
+
+ const response = await server.inject({ method: "POST", url: "/other", payload: { a: 1 } });
+
+ expect(response.json()).toEqual({ hasRaw: false });
+ });
+});
diff --git a/server/src/lib/rawBody.ts b/server/src/lib/rawBody.ts
new file mode 100644
index 000000000..d5ccd84e0
--- /dev/null
+++ b/server/src/lib/rawBody.ts
@@ -0,0 +1,26 @@
+import type { FastifyInstance } from "fastify";
+
+declare module "fastify" {
+ interface FastifyRequest {
+ // Exact request bytes. Only set on routes in a scope that called keepRawJsonBody.
+ rawBody?: Buffer;
+ }
+}
+
+/**
+ * Parses JSON bodies in the calling plugin's scope while keeping the exact
+ * bytes on request.rawBody. Webhook senders (Stripe, AppSumo) sign those bytes,
+ * so a re-serialized body would never verify.
+ */
+export function keepRawJsonBody(fastify: FastifyInstance) {
+ fastify.addContentTypeParser("application/json", { parseAs: "buffer" }, (request, body, done) => {
+ const raw = body as Buffer;
+ request.rawBody = raw;
+ try {
+ done(null, JSON.parse(raw.toString("utf8")));
+ } catch (error) {
+ (error as { statusCode?: number }).statusCode = 400;
+ done(error as Error, undefined);
+ }
+ });
+}
diff --git a/server/src/lib/subscriptionUtils.test.ts b/server/src/lib/subscriptionUtils.test.ts
index edf43d85c..45068291a 100644
--- a/server/src/lib/subscriptionUtils.test.ts
+++ b/server/src/lib/subscriptionUtils.test.ts
@@ -85,6 +85,7 @@ import {
getReplayLimit,
getStripeSubscription,
hasHadStripeSubscription,
+ hasRenewingStripeSubscription,
invalidateAllStripeSubscriptionsCache,
invalidateStripeSubscriptionCache,
OverrideSubscriptionInfo,
@@ -971,3 +972,50 @@ describe("hasHadStripeSubscription", () => {
await expect(hasHadStripeSubscription("cus_history")).resolves.toBe(false);
});
});
+
+describe("hasRenewingStripeSubscription", () => {
+ const PERIOD_END = 1_800_000_000;
+ const sub = (status: string, extra: Record<string, unknown> = {}) => ({
+ id: `sub_${status}`,
+ status,
+ cancel_at_period_end: false,
+ cancel_at: null,
+ items: { data: [{ current_period_end: PERIOD_END }] },
+ ...extra,
+ });
+
+ it.each([
+ ["active", sub("active"), true],
+ ["trialing", sub("trialing"), true],
+ ["past_due (still retrying the card)", sub("past_due"), true],
+ ["active but set to cancel at period end", sub("active", { cancel_at_period_end: true }), false],
+ ["active, cancelling on its renewal date", sub("active", { cancel_at: PERIOD_END }), false],
+ ["active, cancelling only after more renewals", sub("active", { cancel_at: PERIOD_END + 86_400 * 365 }), true],
+ ["canceled", sub("canceled"), false],
+ ["incomplete_expired", sub("incomplete_expired"), false],
+ ])("%s β†’ %s", async (_label, subscription, expected) => {
+ mocks.list.mockResolvedValueOnce({ data: [subscription], has_more: false });
+
+ await expect(hasRenewingStripeSubscription(nextCustomer())).resolves.toBe(expected);
+ });
+
+ it("keeps paging past a full page of ended subscriptions", async () => {
+ mocks.list
+ .mockResolvedValueOnce({ data: [sub("canceled", { id: "sub_old" })], has_more: true })
+ .mockResolvedValueOnce({ data: [sub("active")], has_more: false });
+
+ await expect(hasRenewingStripeSubscription(nextCustomer())).resolves.toBe(true);
+ expect(mocks.list.mock.calls[1][0]).toMatchObject({ starting_after: "sub_old" });
+ });
+
+ it("is false without a customer id and never calls Stripe", async () => {
+ await expect(hasRenewingStripeSubscription(null)).resolves.toBe(false);
+ expect(mocks.list).not.toHaveBeenCalled();
+ });
+
+ it("throws when Stripe can't be reached, so deletion is refused rather than guessed", async () => {
+ mocks.list.mockRejectedValueOnce(new Error("stripe down"));
+
+ await expect(hasRenewingStripeSubscription(nextCustomer())).rejects.toThrow("stripe down");
+ });
+});
diff --git a/server/src/lib/subscriptionUtils.ts b/server/src/lib/subscriptionUtils.ts
index 5b7076808..a2f28ce14 100644
--- a/server/src/lib/subscriptionUtils.ts
+++ b/server/src/lib/subscriptionUtils.ts
@@ -407,6 +407,46 @@ export async function hasHadStripeSubscription(
* Uses a single list call (status "all") to halve request volume versus separate
* active/trialing queries.
*/
+/**
+ * Whether the customer has a Stripe subscription that will charge again: active, trialing or
+ * past due, and not ending before its next renewal. Deleting the organization (or its last owner)
+ * would leave such a subscription billing with nobody able to manage it. Throws when Stripe
+ * can't be reached, so callers refuse instead of guessing.
+ */
+export async function hasRenewingStripeSubscription(stripeCustomerId: string | null): Promise<boolean> {
+ if (!stripeCustomerId || !stripe) {
+ return false;
+ }
+ let startingAfter: string | undefined;
+ for (;;) {
+ const page = await (stripe as Stripe).subscriptions.list({
+ customer: stripeCustomerId,
+ status: "all",
+ limit: 100,
+ ...(startingAfter && { starting_after: startingAfter }),
+ });
+ if (page.data.some(willRenew)) {
+ return true;
+ }
+ if (!page.has_more || page.data.length === 0) {
+ return false;
+ }
+ startingAfter = page.data[page.data.length - 1].id;
+ }
+}
+
+function willRenew(sub: Stripe.Subscription): boolean {
+ if (sub.status !== "active" && sub.status !== "trialing" && sub.status !== "past_due") {
+ return false;
+ }
+ if (sub.cancel_at_period_end) {
+ return false;
+ }
+ // A scheduled cancel date only stops billing when it lands before the next renewal.
+ const periodEnd = sub.items?.data[0]?.current_period_end;
+ return !(sub.cancel_at && periodEnd && sub.cancel_at <= periodEnd);
+}
+
async function fetchStripeSubscription(stripeCustomerId: string): Promise<StripeSubscriptionInfo | null> {
// Fetch active + trialing in a single request and filter locally.
const subs = await (stripe as Stripe).subscriptions.list({
diff --git a/server/src/services/import/importQuotaManager.ts b/server/src/services/import/importQuotaManager.ts
index fb52a7510..514b1b6cc 100644
--- a/server/src/services/import/importQuotaManager.ts
+++ b/server/src/services/import/importQuotaManager.ts
@@ -2,6 +2,11 @@ import cluster from "node:cluster";
import { ImportQuotaTracker } from "./importQuotaTracker.js";
import { IS_CLOUD } from "../../lib/const.js";
+// How long an import may run before it no longer counts as active. Imports are only marked
+// complete by the client's final batch, so this is also when one whose client stopped early
+// (tab closed, network lost) is treated as abandoned and can be deleted.
+export const IMPORT_TIMEOUT_MS = 2 * 60 * 60 * 1000; // 2 hours
+
interface CachedTracker {
tracker: ImportQuotaTracker;
lastAccessed: number;
@@ -14,7 +19,6 @@ class ImportQuotaManager {
private readonly CONCURRENT_IMPORT_LIMIT = 1;
private readonly TRACKER_TTL_MS = 30 * 60 * 1000; // 30 minutes
- private readonly IMPORT_TIMEOUT_MS = 2 * 60 * 60 * 1000; // 2 hours
async getTracker(organizationId: string): Promise<ImportQuotaTracker> {
const now = Date.now();
@@ -53,7 +57,7 @@ class ImportQuotaManager {
const startedAt = this.activeImports.get(organizationId);
// If an import exists and hasn't timed out, limit reached
- if (startedAt && now - startedAt < this.IMPORT_TIMEOUT_MS && this.CONCURRENT_IMPORT_LIMIT === 1) {
+ if (startedAt && now - startedAt < IMPORT_TIMEOUT_MS && this.CONCURRENT_IMPORT_LIMIT === 1) {
return false;
}
@@ -78,7 +82,7 @@ class ImportQuotaManager {
// Cleanup timed-out imports
for (const [orgId, startedAt] of this.activeImports) {
- if (now - startedAt > this.IMPORT_TIMEOUT_MS) {
+ if (now - startedAt > IMPORT_TIMEOUT_MS) {
this.activeImports.delete(orgId);
}
}
diff --git a/server/src/services/import/importQuotaTracker.test.ts b/server/src/services/import/importQuotaTracker.test.ts
index 22eb21c52..502c45daf 100644
--- a/server/src/services/import/importQuotaTracker.test.ts
+++ b/server/src/services/import/importQuotaTracker.test.ts
@@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { ImportQuotaTracker } from "./importQuotaTracker.js";
import { db } from "../../db/postgres/postgres.js";
import { getBestSubscription } from "../../lib/subscriptionUtils.js";
+import { processResults } from "../../api/analytics/utils/utils.js";
vi.mock("../../db/clickhouse/clickhouse.js", () => ({
clickhouse: { query: vi.fn() },
@@ -236,6 +237,21 @@ describe("ImportQuotaTracker", () => {
).toEqual([0, 1]);
});
+ it("should count existing ClickHouse usage when processResults returns numeric months", async () => {
+ vi.mocked(db.select as any)
+ .mockReturnValueOnce(chainResolving([{ stripeCustomerId: "cus_123" }]))
+ .mockReturnValueOnce(chainResolving([{ siteId: 3000 }]));
+ vi.mocked(getBestSubscription).mockResolvedValueOnce({ source: "free", eventLimit: 5 } as any);
+ // processResults coerces numeric-looking strings, so "202406" arrives as 202406.
+ vi.mocked(processResults).mockResolvedValueOnce([{ month: 202406, count: 4 }] as any);
+
+ const tracker = await ImportQuotaTracker.create("org-1");
+
+ expect(
+ tracker.canImportBatch(["2024-06-01T10:00:00.000Z", "2024-06-02T10:00:00.000Z", "2024-05-02T10:00:00.000Z"])
+ ).toEqual([0, 2]);
+ });
+
it("should throw when the organization is not found", async () => {
vi.mocked(db.select as any).mockReturnValueOnce(chainResolving([]));
await expect(ImportQuotaTracker.create("missing-org")).rejects.toThrow("Organization missing-org not found");
diff --git a/server/src/services/import/importQuotaTracker.ts b/server/src/services/import/importQuotaTracker.ts
index 026df4854..15e25e7f0 100644
--- a/server/src/services/import/importQuotaTracker.ts
+++ b/server/src/services/import/importQuotaTracker.ts
@@ -140,7 +140,9 @@ export class ImportQuotaTracker {
const rows = await processResults<{ month: string; count: number }>(result);
for (const row of rows) {
- monthlyUsage.set(row.month, row.count);
+ // processResults turns "202405" into the number 202405; lookups use
+ // the "yyyyMM" string, so a numeric key would never match.
+ monthlyUsage.set(String(row.month), Number(row.count));
}
return monthlyUsage;
diff --git a/server/src/services/import/mappers/plausible.test.ts b/server/src/services/import/mappers/plausible.test.ts
index dafba3c49..8f42eab72 100644
--- a/server/src/services/import/mappers/plausible.test.ts
+++ b/server/src/services/import/mappers/plausible.test.ts
@@ -110,6 +110,13 @@ describe("PlausibleImportMapper", () => {
expect(result[0].props).toEqual({});
});
+ it.each(["[1]", '"x"', "123", "null"])("should drop props that aren't a JSON object (%s)", props => {
+ const result = PlausibleImportMapper.transform([makeEvent({ props })], 1, "import-1");
+
+ expect(result).toHaveLength(1);
+ expect(result[0].props).toEqual({});
+ });
+
it("should handle invalid JSON in props", () => {
const events = [makeEvent({ props: "{bad json" })];
const result = PlausibleImportMapper.transform(events, 1, "import-1");
diff --git a/server/src/services/import/mappers/plausible.ts b/server/src/services/import/mappers/plausible.ts
index ba9cdc597..c533e9a53 100644
--- a/server/src/services/import/mappers/plausible.ts
+++ b/server/src/services/import/mappers/plausible.ts
@@ -1,5 +1,5 @@
import { isClickHouseDateTime, toClickHouseDateTime } from "../../../db/clickhouse/dateTime.js";
-import { clearSelfReferrer, getAllUrlParams } from "../../tracker/utils.js";
+import { clearSelfReferrer, getAllUrlParams, parseEventProperties } from "../../tracker/utils.js";
import { getChannel } from "../../tracker/getChannel.js";
import { RybbitEvent } from "./rybbit.js";
import { z } from "zod";
@@ -59,12 +59,8 @@ export class PlausibleImportMapper {
if (!parsed.success) return acc;
const data = parsed.data;
- let parsedProps: Record<string, unknown> = {};
- try {
- parsedProps = JSON.parse(data.props || "{}");
- } catch {
- // ignore invalid JSON
- }
+ // Non-object JSON ("[1]", "\"x\"") would fail the whole ClickHouse insert.
+ const parsedProps = parseEventProperties(data.props) ?? {};
const referrer = clearSelfReferrer(data.referrer, data.hostname.replace(/^www\./, ""));
diff --git a/server/src/services/lifecycleEmails/platformDetect.test.ts b/server/src/services/lifecycleEmails/platformDetect.test.ts
index 41961f65f..1b31dd593 100644
--- a/server/src/services/lifecycleEmails/platformDetect.test.ts
+++ b/server/src/services/lifecycleEmails/platformDetect.test.ts
@@ -21,11 +21,17 @@ describe("isPublicUnicastAddress (SSRF guard)", () => {
"fe80::1",
"::ffff:127.0.0.1", // v4-mapped loopback
"::ffff:10.0.0.1",
+ // The same mapped addresses in the hex form URL parsing produces
+ "::ffff:7f00:1",
+ "::ffff:a9fe:a9fe", // 169.254.169.254
+ "0:0:0:0:0:ffff:a00:1",
+ "64:ff9b::7f00:1", // NAT64 loopback
+ "2001:db8::1", // documentation
])("rejects %s", address => {
expect(isPublicUnicastAddress(address)).toBe(false);
});
- it.each(["8.8.8.8", "1.1.1.1", "93.184.216.34", "2606:4700:4700::1111", "::ffff:8.8.8.8"])(
+ it.each(["8.8.8.8", "1.1.1.1", "93.184.216.34", "2606:4700:4700::1111", "::ffff:8.8.8.8", "::ffff:808:808"])(
"allows public %s",
address => {
expect(isPublicUnicastAddress(address)).toBe(true);
diff --git a/server/src/services/lifecycleEmails/platformDetect.ts b/server/src/services/lifecycleEmails/platformDetect.ts
index f42a978c8..d284222a0 100644
--- a/server/src/services/lifecycleEmails/platformDetect.ts
+++ b/server/src/services/lifecycleEmails/platformDetect.ts
@@ -1,5 +1,7 @@
import { lookup } from "node:dns/promises";
import { isIP } from "node:net";
+import { Address6 } from "ip-address";
+import { isPublicHttpAddress } from "../../lib/publicHttpFetch.js";
import { createServiceLogger } from "../../lib/logger/logger.js";
const logger = createServiceLogger("platform-detect");
@@ -43,44 +45,24 @@ const MAX_BODY_BYTES = 512 * 1024;
const FETCH_TIMEOUT_MS = 8000;
const MAX_REDIRECTS = 3;
+const V4_MAPPED = new Address6("::ffff:0:0/96");
+const NAT64 = new Address6("64:ff9b::/96");
+
/**
- * SSRF guard: reject any address that is not publicly routable. Covers
- * loopback, RFC1918, link-local (incl. cloud metadata 169.254.169.254),
- * CGNAT, benchmarking/documentation ranges, multicast/reserved, and their
- * IPv6 equivalents including v4-mapped forms.
+ * SSRF guard: reject any address that is not publicly routable. Delegates to
+ * the favicon fetcher's allowlist (public v4, global-unicast v6). v4-mapped
+ * and NAT64 addresses are judged by their embedded v4 address in every
+ * spelling: URL parsing turns [::ffff:169.254.169.254] into
+ * [::ffff:a9fe:a9fe], which a dotted-form pattern never matched.
*/
export function isPublicUnicastAddress(address: string): boolean {
- const family = isIP(address);
- if (family === 4) return isPublicV4(address);
- if (family === 6) {
- const lower = address.toLowerCase();
- // v4-mapped / v4-translated: judge the embedded v4
- const mapped = lower.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/) || lower.match(/^64:ff9b::(\d+\.\d+\.\d+\.\d+)$/);
- if (mapped) return isPublicV4(mapped[1]);
- if (lower === "::" || lower === "::1") return false;
- if (/^(fc|fd)/.test(lower)) return false; // unique local fc00::/7
- if (/^fe[89ab]/.test(lower)) return false; // link-local fe80::/10
- if (/^ff/.test(lower)) return false; // multicast
- return true;
+ if (isIP(address) === 6) {
+ const ip = new Address6(address);
+ if (ip.isInSubnet(V4_MAPPED) || ip.isInSubnet(NAT64)) {
+ return isPublicHttpAddress(ip.to4().correctForm());
+ }
}
- return false;
-}
-
-function isPublicV4(address: string): boolean {
- const parts = address.split(".").map(Number);
- if (parts.length !== 4 || parts.some(p => Number.isNaN(p) || p < 0 || p > 255)) return false;
- const [a, b] = parts;
- if (a === 0 || a === 10 || a === 127) return false;
- if (a === 100 && b >= 64 && b <= 127) return false; // CGNAT 100.64/10
- if (a === 169 && b === 254) return false; // link-local / metadata
- if (a === 172 && b >= 16 && b <= 31) return false;
- if (a === 192 && b === 168) return false;
- if (a === 192 && b === 0) return false; // 192.0.0/24 + 192.0.2/24 (docs)
- if (a === 198 && (b === 18 || b === 19)) return false; // benchmarking
- if (a === 198 && b === 51) return false; // 198.51.100/24 docs
- if (a === 203 && b === 113) return false; // 203.0.113/24 docs
- if (a >= 224) return false; // multicast + reserved + broadcast
- return true;
+ return isPublicHttpAddress(address);
}
/** Resolve a hostname and confirm every address it maps to is public. */
diff --git a/server/src/services/tracker/identifyService.test.ts b/server/src/services/tracker/identifyService.test.ts
index ed081c98a..96e435802 100644
--- a/server/src/services/tracker/identifyService.test.ts
+++ b/server/src/services/tracker/identifyService.test.ts
@@ -8,6 +8,8 @@ const mocks = vi.hoisted(() => ({
generateUserId: vi.fn(),
generateUserIdFromClientId: vi.fn(),
backfillAdd: vi.fn(),
+ isTrustedServerSideIngestion: vi.fn(),
+ resolveClientIp: vi.fn(),
}));
vi.mock("../../lib/siteConfig.js", () => ({ siteConfig: { getConfig: mocks.getConfig } }));
@@ -20,6 +22,8 @@ vi.mock("../userId/userIdService.js", () => ({
},
}));
vi.mock("./identityBackfillQueue.js", () => ({ identityBackfillQueue: { add: mocks.backfillAdd } }));
+vi.mock("./trackingRequest.js", () => ({ isTrustedServerSideIngestion: mocks.isTrustedServerSideIngestion }));
+vi.mock("./resolveClientIp.js", () => ({ resolveClientIp: mocks.resolveClientIp }));
vi.mock("../../lib/logger/logger.js", () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
return { logger, createServiceLogger: () => logger };
@@ -37,6 +41,8 @@ function replyStub() {
beforeEach(() => {
vi.clearAllMocks();
mocks.getConfig.mockResolvedValue({ siteId: 7, firstPartyProxy: false });
+ mocks.isSiteWithoutPlan.mockReturnValue(false);
+ mocks.resolveClientIp.mockReturnValue("198.51.100.7");
});
describe("handleIdentify β€” site with no plan", () => {
@@ -60,3 +66,39 @@ describe("handleIdentify β€” site with no plan", () => {
expect(mocks.backfillAdd).not.toHaveBeenCalled();
});
});
+
+describe("handleIdentify β€” ip_address / user_agent overrides", () => {
+ const body = {
+ site_id: "7",
+ user_id: "attacker_pick",
+ ip_address: "203.0.113.50",
+ user_agent: "Victim UA",
+ is_new_identify: false,
+ };
+
+ it("ignores them without a trusted ingest:write bearer and fingerprints the caller instead", async () => {
+ mocks.isTrustedServerSideIngestion.mockResolvedValue(false);
+
+ await handleIdentify({ body, headers: { "user-agent": "Caller UA" } } as any, replyStub());
+
+ expect(mocks.generateUserId).toHaveBeenCalledWith("198.51.100.7", "Caller UA", 7);
+ });
+
+ it("uses them when the request carries a trusted ingest:write bearer", async () => {
+ mocks.isTrustedServerSideIngestion.mockResolvedValue(true);
+
+ await handleIdentify({ body, headers: { "user-agent": "Server UA" } } as any, replyStub());
+
+ expect(mocks.generateUserId).toHaveBeenCalledWith("203.0.113.50", "Victim UA", 7);
+ });
+
+ it("skips the bearer check when no override is sent", async () => {
+ await handleIdentify(
+ { body: { site_id: "7", user_id: "u", is_new_identify: false }, headers: { "user-agent": "Caller UA" } } as any,
+ replyStub()
+ );
+
+ expect(mocks.isTrustedServerSideIngestion).not.toHaveBeenCalled();
+ expect(mocks.generateUserId).toHaveBeenCalledWith("198.51.100.7", "Caller UA", 7);
+ });
+});
diff --git a/server/src/services/tracker/identifyService.ts b/server/src/services/tracker/identifyService.ts
index cc17b3bd0..cc754280a 100644
--- a/server/src/services/tracker/identifyService.ts
+++ b/server/src/services/tracker/identifyService.ts
@@ -8,6 +8,7 @@ import { usageService } from "../usageService.js";
import { identityBackfillQueue } from "./identityBackfillQueue.js";
import { userIdService } from "../userId/userIdService.js";
import { resolveClientIp } from "./resolveClientIp.js";
+import { isTrustedServerSideIngestion } from "./trackingRequest.js";
import { createServiceLogger } from "../../lib/logger/logger.js";
const logger = createServiceLogger("identify-service");
@@ -87,11 +88,18 @@ export async function handleIdentify(request: FastifyRequest, reply: FastifyRepl
return reply.status(200).send("Site has no plan, identify not stored");
}
+ // Like tracking, only a bearer with ingest:write may speak for another
+ // visitor. Otherwise anyone who knew a visitor's IP and user agent could
+ // point that visitor's fingerprint at a user id of their choosing.
+ const trustedOverride =
+ (ip_address !== undefined || user_agent !== undefined) && (await isTrustedServerSideIngestion(request, siteId));
+
const anonymousId = anonymous_id
? await userIdService.generateUserIdFromClientId(anonymous_id, siteId)
: await userIdService.generateUserId(
- ip_address || resolveClientIp(request, { firstPartyProxy: siteConfiguration.firstPartyProxy }),
- user_agent || request.headers["user-agent"] || "",
+ (trustedOverride && ip_address) ||
+ resolveClientIp(request, { firstPartyProxy: siteConfiguration.firstPartyProxy }),
+ (trustedOverride && user_agent) || request.headers["user-agent"] || "",
siteId
);
diff --git a/server/src/services/tracker/pageviewQueue.test.ts b/server/src/services/tracker/pageviewQueue.test.ts
index 6a60b02fd..88a128f13 100644
--- a/server/src/services/tracker/pageviewQueue.test.ts
+++ b/server/src/services/tracker/pageviewQueue.test.ts
@@ -138,6 +138,29 @@ describe("pageviewQueue", () => {
expect(row.timestamp_ms).toBe(expected);
});
+ it.each([['"file.pdf"'], ["[1,2]"], ["123"], ["null"], ["not json"]])(
+ "drops props that aren't a JSON object (%s) and keeps the event",
+ async properties => {
+ await pageviewQueue.add(makePayload({ type: "custom_event", event_name: "download", properties }));
+ await pageviewQueue.add(makePayload({ type: "custom_event", event_name: "ok", properties: '{"plan":"pro"}' }));
+ await vi.advanceTimersByTimeAsync(1000);
+
+ const rows = mocks.insert.mock.calls[0][0].values;
+ expect(rows).toHaveLength(2);
+ expect(rows[0].props).toBeUndefined();
+ expect(rows[1].props).toEqual({ plan: "pro" });
+ }
+ );
+
+ it("inserts with settings that keep one bad row from failing the batch", async () => {
+ await flush();
+
+ expect(mocks.insert.mock.calls[0][0].clickhouse_settings).toMatchObject({
+ type_json_skip_duplicated_paths: 1,
+ input_format_allow_errors_num: "100",
+ });
+ });
+
it("keeps the repeated daylight-saving hour distinct", async () => {
Settings.defaultZone = "Europe/Berlin";
await pageviewQueue.add(makePayload({ timestamp: "2026-10-25T02:30:00.999+02:00" }));
diff --git a/server/src/services/tracker/pageviewQueue.ts b/server/src/services/tracker/pageviewQueue.ts
index 687745a1b..728b4d2ca 100644
--- a/server/src/services/tracker/pageviewQueue.ts
+++ b/server/src/services/tracker/pageviewQueue.ts
@@ -6,7 +6,13 @@ import { createServiceLogger } from "../../lib/logger/logger.js";
import { getDeviceType } from "../../utils.js";
import { isDatacenterAsn } from "./botBlocking/datacenterAsns.js";
import { getChannel } from "./getChannel.js";
-import { clearSelfReferrer, getAllUrlParams, TotalTrackingPayload } from "./utils.js";
+import {
+ clearSelfReferrer,
+ EVENTS_INSERT_SETTINGS,
+ getAllUrlParams,
+ parseEventProperties,
+ TotalTrackingPayload,
+} from "./utils.js";
type TotalPayload = TotalTrackingPayload & {
sessionId: string;
@@ -15,14 +21,6 @@ type TotalPayload = TotalTrackingPayload & {
const PAGEVIEW_BATCH_SIZE = 5000;
const PAGEVIEW_FLUSH_INTERVAL_MS = 1000;
-const getParsedProperties = (properties: string | undefined) => {
- try {
- return properties ? JSON.parse(properties) : undefined;
- } catch (error) {
- return undefined;
- }
-};
-
class PageviewQueue {
private queue: TotalPayload[] = [];
private batchSize = PAGEVIEW_BATCH_SIZE;
@@ -128,7 +126,7 @@ class PageviewQueue {
lon: longitude || 0,
type: pv.type || "pageview",
event_name: pv.event_name || "",
- props: getParsedProperties(pv.properties),
+ props: parseEventProperties(pv.properties),
url_parameters: allUrlParams,
// Performance metrics (only included for performance events).
// ?? not ||: 0 is a legitimate measurement (a perfect CLS score is 0)
@@ -155,6 +153,7 @@ class PageviewQueue {
table: "events",
values: processedPageviews,
format: "JSONEachRow",
+ clickhouse_settings: EVENTS_INSERT_SETTINGS,
});
} catch (error) {
this.logger.error({ err: error }, "Error processing pageview queue");
diff --git a/server/src/services/tracker/trackingRequest.ts b/server/src/services/tracker/trackingRequest.ts
index e6fd98a39..e99a7219d 100644
--- a/server/src/services/tracker/trackingRequest.ts
+++ b/server/src/services/tracker/trackingRequest.ts
@@ -61,7 +61,7 @@ export interface TrackingRequest {
* An under-scoped bearer degrades to untrusted client-side traffic (no 4xx) β€”
* this is a public ingestion endpoint, not an authenticated API.
*/
-async function isTrustedServerSideIngestion(request: FastifyRequest, siteId: number): Promise<boolean> {
+export async function isTrustedServerSideIngestion(request: FastifyRequest, siteId: number): Promise<boolean> {
const authHeader = request.headers["authorization"];
if (typeof authHeader !== "string" || !authHeader.startsWith("Bearer ")) {
return false;
diff --git a/server/src/services/tracker/utils.ts b/server/src/services/tracker/utils.ts
index 19d6c37bb..63f7d1e4e 100644
--- a/server/src/services/tracker/utils.ts
+++ b/server/src/services/tracker/utils.ts
@@ -157,3 +157,26 @@ export async function createBasePayload(trackingRequest: TrackingRequest): Promi
storeIp: site.trackIp,
} as any;
}
+
+// The props column is ClickHouse JSON, which only accepts objects. Validation
+// lets any JSON through (e.g. rybbit.event("download", "file.pdf") sends a
+// string), and one non-object row fails the whole insert, so drop those props
+// here and keep the event.
+export function parseEventProperties(properties: string | undefined): Record<string, unknown> | undefined {
+ if (!properties) return undefined;
+ try {
+ const parsed = JSON.parse(properties);
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : undefined;
+ } catch (error) {
+ return undefined;
+ }
+}
+
+// A tracking batch mixes every site's events, so one row ClickHouse can't parse
+// must not take the rest down with it. Conflicting paths like {"a.b":1,"a":{"b":2}} are
+// merged instead of rejected, and up to 100 other unparseable rows are skipped.
+// A failure on more rows than that still throws.
+export const EVENTS_INSERT_SETTINGS = {
+ type_json_skip_duplicated_paths: 1,
+ input_format_allow_errors_num: "100",
+};