mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
fix(admin): harden the Danger-Zone wipe to match the Backups Restore gate
The global data wipe auto-supplied confirm:'DELETE ALL' from a single-click dialog, leaving the server's typed-token defence dead and making the most destructive action a weaker gate than the less-destructive restore. Mirror RestoreRowDialog: require typing 'DELETE ALL' into an Input that arms the destructive button, reset on close. Also attach an Idempotency-Key header to the wipe so a double-submit cannot re-run the transaction, add the documented post-requireAdmin() 'admin-data-delete:<userId>' rate-limit bucket alongside the anonymous IP throttle, and switch BackupsSection off the non-reactive formatDateTime onto the locale-reactive useFormatters() hook its siblings use.
This commit is contained in:
1 parent
e23f57448a
commit
88c4a8428f
9 files changed
+81
-7
No files matched your search
@@ -39,7 +39,6 @@ import { Button } from "@/components/ui/button";
|
||||
import { EmptyState } from "@/components/ui/empty-state";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { formatDateTime } from "@/lib/format";
|
||||
import { SettingsCardHeader } from "@/components/settings/_card-header";
|
||||
import { useFormatters, useTranslations } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
@@ -70,6 +69,7 @@ function RestoreRowDialog({
|
||||
onConfirm: () => void;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const fmt = useFormatters();
|
||||
const [open, setOpen] = useState(false);
|
||||
const [typed, setTyped] = useState("");
|
||||
const matched = typed.trim() === "RESTORE";
|
||||
@@ -108,7 +108,7 @@ function RestoreRowDialog({
|
||||
<AlertDialogDescription>
|
||||
{t("admin.section.backups.restoreDescription", {
|
||||
username: row.username,
|
||||
when: formatDateTime(row.createdAt),
|
||||
when: fmt.dateTime(row.createdAt),
|
||||
})}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
@@ -501,7 +501,7 @@ export function BackupsSection() {
|
||||
{formatBytes(row.sizeBytes, fmt)}
|
||||
</td>
|
||||
<td className="text-muted-foreground px-3 py-2 text-right text-xs whitespace-nowrap">
|
||||
{formatDateTime(row.createdAt)}
|
||||
{fmt.dateTime(row.createdAt)}
|
||||
</td>
|
||||
<td className="px-3 py-2 text-right whitespace-nowrap">
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
|
||||
@@ -15,17 +15,36 @@ import {
|
||||
AlertDialogTrigger,
|
||||
} from "@/components/ui/alert-dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { SettingsCardHeader } from "@/components/settings/_card-header";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { apiDelete } from "@/lib/api/api-fetch";
|
||||
|
||||
/**
|
||||
* The literal an admin must type to arm the wipe. Mirrors the Backups
|
||||
* `RESTORE` gate — and matches the server's typed-token defence at
|
||||
* `src/app/api/admin/data/route.ts` (which 422s any other `confirm`).
|
||||
*/
|
||||
const WIPE_CONFIRM_TOKEN = "DELETE ALL";
|
||||
|
||||
export function DangerZoneSection() {
|
||||
const { t } = useTranslations();
|
||||
const queryClient = useQueryClient();
|
||||
const [wipeMsg, setWipeMsg] = useState<string | null>(null);
|
||||
// Typed-confirmation gate. The destructive button stays disabled until
|
||||
// the admin types `DELETE ALL` verbatim, matching the Backups Restore
|
||||
// dialog (which forces typing `RESTORE`) so the FAR more destructive
|
||||
// global wipe is not the weaker gate. Reset whenever the dialog closes.
|
||||
const [open, setOpen] = useState(false);
|
||||
const [typed, setTyped] = useState("");
|
||||
const matched = typed.trim() === WIPE_CONFIRM_TOKEN;
|
||||
|
||||
const wipeAllData = useMutation({
|
||||
mutationFn: async () => {
|
||||
// Idempotency-Key prevents a double-submit from re-running the
|
||||
// destructive transaction, matching the Backups restore path.
|
||||
const idempotencyKey = `admin-data-wipe-${crypto.randomUUID()}`;
|
||||
return apiDelete<{
|
||||
measurements: number;
|
||||
intakeEvents: number;
|
||||
@@ -36,7 +55,11 @@ export function DangerZoneSection() {
|
||||
notificationChannels: number;
|
||||
pushSubscriptions: number;
|
||||
telegramScheduledDeletions: number;
|
||||
}>("/api/admin/data", { confirm: "DELETE ALL" });
|
||||
}>(
|
||||
"/api/admin/data",
|
||||
{ confirm: WIPE_CONFIRM_TOKEN },
|
||||
{ headers: { "Idempotency-Key": idempotencyKey } },
|
||||
);
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries();
|
||||
@@ -72,7 +95,13 @@ export function DangerZoneSection() {
|
||||
title={t("admin.deleteAllData")}
|
||||
description={t("admin.deleteAllDescription")}
|
||||
status={
|
||||
<AlertDialog>
|
||||
<AlertDialog
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
setOpen(next);
|
||||
if (!next) setTyped("");
|
||||
}}
|
||||
>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button
|
||||
variant="destructive"
|
||||
@@ -97,13 +126,36 @@ export function DangerZoneSection() {
|
||||
{t("admin.deleteAllConfirmDescription")}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="wipe-confirm-prompt">
|
||||
{t("admin.deleteAllPromptLabel", {
|
||||
token: WIPE_CONFIRM_TOKEN,
|
||||
})}
|
||||
</Label>
|
||||
<Input
|
||||
id="wipe-confirm-prompt"
|
||||
value={typed}
|
||||
onChange={(e) => setTyped(e.target.value)}
|
||||
autoComplete="off"
|
||||
spellCheck={false}
|
||||
placeholder={WIPE_CONFIRM_TOKEN}
|
||||
/>
|
||||
</div>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>{t("common.cancel")}</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
variant="destructive"
|
||||
disabled={wipeAllData.isPending}
|
||||
disabled={!matched || wipeAllData.isPending}
|
||||
aria-busy={wipeAllData.isPending || undefined}
|
||||
onClick={() => wipeAllData.mutate()}
|
||||
onClick={(e) => {
|
||||
if (!matched) {
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
setOpen(false);
|
||||
setTyped("");
|
||||
wipeAllData.mutate();
|
||||
}}
|
||||
>
|
||||
{wipeAllData.isPending && (
|
||||
<Loader2 className="mr-1 h-3.5 w-3.5 animate-spin motion-reduce:animate-none" />
|
||||
|
||||
Reference in new issue
Block a user