Files
Arnaud 3042d289a0 security: fix full audit findings (v0.32.0)
Full list of the audit's confirmed findings and their fixes:

- Stored XSS via unescaped JSON-LD on the public recipe page
  (app/r/[id]/page.tsx) — escape < before injecting.
- CSP allowed unsafe-eval in production — now dev-only (Next prod
  never eval()s; only its HMR does).
- avatarUrl accepted any URL with no ownership check — now takes an
  avatarKey issued by avatar-presign, validated server-side, same
  pattern as recipe/review photos.
- No session revocation on password change/reset — both now revoke
  other sessions (revokeOtherSessions: true, revokeSessionsOnPasswordReset).
- Rate-limit bypass via spoofable X-Forwarded-For — take the last
  (proxy-appended) hop instead of the first (client-supplied) one,
  matching the single-Traefik-hop topology.
- Webhook signing secrets stored plaintext — now AES-256-GCM
  encrypted like every other secret in this app, with a legacy-
  plaintext fallback for pre-existing rows (bare hex has no ":", our
  ciphertext format always does).
- Better Auth's own rate limiter defaulted to in-memory storage,
  ineffective across replicas — now backed by the same Redis as
  lib/rate-limit.ts (secondaryStorage), with storeSessionInDatabase
  explicit so session storage itself doesn't move as a side effect.
- Presigned upload URLs didn't bind the declared file size to the
  actual upload, letting a client under-declare size (and quota
  charge) then PUT an arbitrarily large object — switched to S3
  presigned POST with a signed content-length-range condition,
  enforced by the storage server itself.
- generateMetadata() on the recipe page skipped the visibility
  filter the page body uses, leaking a private recipe's title via
  <title> to any signed-in user with the id.
- Block/unblock had no rate limit, unlike follow/unfollow.
- AI quota was charged even when a user's own BYOK key was used
  (their own credentials/billing) — added an isByok flag through
  the config-resolution chain and skip the charge when set. Also
  wired BYOK into generate/generate-from-idea/translate/import-url,
  which never looked it up at all before.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-14 15:05:05 +02:00

169 lines
5.3 KiB
TypeScript

"use client";
import { useState } from "react";
import { toast } from "sonner";
import { Button, buttonVariants } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useTranslations } from "next-intl";
import { authClient } from "@/lib/auth/client";
import { TwoFactorSettings } from "@/components/settings/two-factor-settings";
export function SecurityForm({
currentEmail,
twoFactorEnabled,
hasPassword,
}: {
currentEmail: string;
twoFactorEnabled: boolean;
hasPassword: boolean;
}) {
const t = useTranslations("settingsForm");
const [newEmail, setNewEmail] = useState("");
const [sendingEmail, setSendingEmail] = useState(false);
const [currentPassword, setCurrentPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [changingPassword, setChangingPassword] = useState(false);
async function handleChangeEmail(e: React.FormEvent) {
e.preventDefault();
if (!newEmail.trim()) return;
setSendingEmail(true);
try {
const { error } = await authClient.changeEmail({
newEmail: newEmail.trim(),
callbackURL: "/settings",
});
if (error) {
toast.error(error.message ?? t("emailChangeFailed"));
} else {
toast.success(t("emailChangeSent"));
setNewEmail("");
}
} finally {
setSendingEmail(false);
}
}
async function handleChangePassword(e: React.FormEvent) {
e.preventDefault();
if (newPassword !== confirmPassword) {
toast.error(t("passwordMismatch"));
return;
}
setChangingPassword(true);
try {
const { error } = await authClient.changePassword({
currentPassword,
newPassword,
revokeOtherSessions: true,
});
if (error) {
toast.error(error.message ?? t("passwordChangeFailed"));
} else {
toast.success(t("passwordChanged"));
setCurrentPassword("");
setNewPassword("");
setConfirmPassword("");
}
} finally {
setChangingPassword(false);
}
}
return (
<div className="space-y-6">
<section className="rounded-xl border p-6 space-y-4">
<div>
<h2 className="font-semibold text-lg">{t("changeEmail")}</h2>
<p className="text-sm text-muted-foreground mt-1">{t("changeEmailDescription")}</p>
</div>
<form onSubmit={handleChangeEmail} className="space-y-3">
<div className="space-y-2">
<Label htmlFor="new-email">{t("newEmail")}</Label>
<Input
id="new-email"
type="email"
value={newEmail}
onChange={(e) => setNewEmail(e.target.value)}
placeholder={currentEmail}
required
/>
</div>
<Button type="submit" disabled={sendingEmail || !newEmail.trim()}>
{sendingEmail ? t("sendingVerification") : t("sendVerification")}
</Button>
</form>
</section>
<section className="rounded-xl border p-6 space-y-4">
<div>
<h2 className="font-semibold text-lg">{t("changePassword")}</h2>
</div>
<form onSubmit={handleChangePassword} className="space-y-3">
<div className="space-y-2">
<Label htmlFor="current-password">{t("currentPassword")}</Label>
<Input
id="current-password"
type="password"
value={currentPassword}
onChange={(e) => setCurrentPassword(e.target.value)}
autoComplete="current-password"
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="new-password">{t("newPassword")}</Label>
<Input
id="new-password"
type="password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
autoComplete="new-password"
minLength={8}
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="confirm-password">{t("confirmPassword")}</Label>
<Input
id="confirm-password"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
autoComplete="new-password"
minLength={8}
required
/>
</div>
<Button
type="submit"
disabled={changingPassword || !currentPassword || !newPassword || !confirmPassword}
>
{changingPassword ? t("changingPassword") : t("changePasswordButton")}
</Button>
</form>
</section>
<TwoFactorSettings initialEnabled={twoFactorEnabled} hasPassword={hasPassword} />
<section className="rounded-xl border p-6 space-y-4">
<div>
<h2 className="font-semibold text-lg">{t("downloadData")}</h2>
<p className="text-sm text-muted-foreground mt-1">{t("downloadDataDescription")}</p>
</div>
<a
href="/api/v1/users/me/export"
download
className={buttonVariants({ variant: "outline" })}
>
{t("downloadDataButton")}
</a>
</section>
</div>
);
}