Files
sass-mohfarawati/app/_admin/site-settings/actions.ts
T
moh dc21c33867 ADDED - Admin SEO page, robots/sitemap hardening and media/maintenance security fixes
SEO
- New Settings > SEO admin page (seo_settings in app_config): indexing switch,
  Google/Bing verification, X handle, JSON-LD identity (Person/Organization,
  sameAs), per-locale keywords, readiness checklist and open links for
  sitemap.xml / robots.txt / manifest.
- robots.txt is now dynamic: disallows admin, api, success and coming-soon
  paths; blocks everything while indexing is off or maintenance is on.
- sitemap.xml carries hreflang alternates per URL, lists only categories with
  published projects, and is empty while hidden.
- Metadata: robots + verification meta, og:locale in de_DE/en_US/ar_AR form,
  alternateLocale, twitter site/creator, project cover as OG image with
  article type, noindex on /success and /coming-soon.
- JSON-LD: WebSite + publisher graph on all public pages, CreativeWork per
  project (view-mode independent).

Security
- Maintenance bypass now requires a correctly signed admin cookie; the
  middleware previously only checked the cookie existed. Token helpers moved
  to lib/admin-session-token.ts (shared by proxy.ts and lib/admin-auth.ts).
- Media uploads: magic-byte validation against the declared type, SVG
  sanitization (script/handlers/foreignObject/javascript: rejected), upload
  folder sanitized, kind inferred from the real file.
- Media route: fixed prefix-based path check that accepted sibling
  directories, unknown extensions return 404, nosniff header, CSP sandbox on
  SVG, gif content type added.
- External media URLs: protocol-relative (//host) URLs rejected.

Portfolio
- Project and category slugs share /portfolio/[slug]; saving now rejects a
  slug already used on the other side instead of silently shadowing it.

Tooling/docs
- Lint: ignore scripts/legacy-prisma-seed.cjs, drop unused import.
- New docs/SEO.md; FEATURES, ARCHITECTURE (Drizzle instead of Prisma), admin
  spec and CLAUDE.md updated.
- Tests for all of the above (unit + integration); suite green.
2026-09-20 21:36:16 +02:00

403 lines
14 KiB
TypeScript

"use server";
import { MediaUsageType } from "@/lib/db/enums";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { isRedirectError } from "next/dist/client/components/redirect-error";
import {
SITE_SETTINGS_DEFAULT_OG_IMAGE_FIELD_KEY,
SITE_SETTINGS_ENTITY_ID,
SITE_SETTINGS_ENTITY_TYPE,
SITE_SETTINGS_FAVICON_FIELD_KEY,
SITE_SETTINGS_LOGO_DARK_FIELD_KEY,
SITE_SETTINGS_LOGO_LIGHT_FIELD_KEY,
getSeoSettings,
getSiteSettings,
updateSeoSettings,
updateSiteSettings,
} from "@/lib/app-config";
import { getAdminAppPath, toInternalAdminPath } from "@/lib/admin-routing";
import { withFlash } from "@/lib/admin-feedback";
import {
PAGE_TITLE_TOKEN,
normalizeSiteDefaultLocale,
normalizeSitePrimaryColor,
type SiteSettings,
} from "@/lib/site-settings";
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
import {
normalizeKeywords,
normalizeSameAs,
normalizeStructuredDataType,
normalizeTwitterHandle,
normalizeVerificationToken,
type SeoSettings,
} from "@/lib/seo-settings";
import { isCheckedFormValue } from "@/lib/form-data";
import { replaceEntityMediaUsages } from "@/lib/media";
import { resolveMediaSelection } from "@/lib/media-service";
import { routing } from "@/i18n/routing";
import { getLocalizedPath } from "@/lib/locale";
import { removeManagedMediaFile } from "@/lib/media-storage";
import { mediaFieldInputSchema } from "@/lib/media-validation";
import { inArray } from "drizzle-orm";
import { db } from "@/lib/db";
import { mediaAsset } from "@/lib/db/schema";
async function ensureAdmin() {
if (!(await isAdminAuthenticated())) {
await clearAdminSessionCookie();
redirect(getAdminAppPath("/"));
}
}
function parseJsonObject(rawValue: FormDataEntryValue | null, key: string) {
if (typeof rawValue !== "string" || rawValue.trim() === "") {
return undefined;
}
try {
const parsed = JSON.parse(rawValue);
if (!parsed || Array.isArray(parsed) || typeof parsed !== "object") {
throw new Error(`${key} must be an object.`);
}
return parsed;
} catch {
throw new Error(`Invalid ${key} payload.`);
}
}
async function cleanupCreatedMedia(assetIds: string[], uploadedPaths: string[]) {
if (assetIds.length > 0) {
await db.delete(mediaAsset).where(inArray(mediaAsset.id, Array.from(new Set(assetIds))));
}
for (const filePath of Array.from(new Set(uploadedPaths.filter(Boolean)))) {
await removeManagedMediaFile(filePath);
}
}
async function revalidateSiteSettingsPages(defaultLocale: SiteSettings["defaultLocale"]) {
revalidatePath("/", "layout");
revalidatePath(toInternalAdminPath("/"));
revalidatePath(toInternalAdminPath("/site-settings"));
revalidatePath("/coming-soon");
const publicPaths = ["/", "/about", "/portfolio", "/contact", "/success", "/coming-soon"];
for (const locale of routing.locales) {
revalidatePath(getLocalizedPath(locale, "/", defaultLocale), "layout");
for (const path of publicPaths) {
revalidatePath(getLocalizedPath(locale, path, defaultLocale));
}
}
}
export async function saveSiteSettingsAction(formData: FormData) {
return saveSiteBrandSettingsAction(formData);
}
export async function saveSiteBrandSettingsAction(formData: FormData) {
await ensureAdmin();
const createdMediaAssetIds: string[] = [];
const uploadedPaths: string[] = [];
try {
const currentSettings = await getSiteSettings();
const siteLogoLightMedia = parseJsonObject(formData.get("siteLogoLightMedia"), "siteLogoLightMedia");
const siteLogoDarkMedia = parseJsonObject(formData.get("siteLogoDarkMedia"), "siteLogoDarkMedia");
const faviconMedia = parseJsonObject(formData.get("faviconMedia"), "faviconMedia");
const defaultOgImageMedia = parseJsonObject(
formData.get("defaultOgImageMedia"),
"defaultOgImageMedia",
);
const parsedSettings: SiteSettings = {
...currentSettings,
brand: {
primaryColor: normalizeSitePrimaryColor(formData.get("primaryColor")),
},
};
const siteLogoLightSelection = siteLogoLightMedia
? await resolveMediaSelection({
media: mediaFieldInputSchema.parse(siteLogoLightMedia),
uploadFile: formData.get("siteLogoLightFile"),
folder: "site-settings",
fallbackLabel: `${parsedSettings.locales.de.siteName || parsedSettings.locales.en.siteName} logo light`,
required: false,
})
: {
assetId: null,
url: "",
createdAssetId: null,
uploadedUrl: null,
};
const siteLogoDarkSelection = siteLogoDarkMedia
? await resolveMediaSelection({
media: mediaFieldInputSchema.parse(siteLogoDarkMedia),
uploadFile: formData.get("siteLogoDarkFile"),
folder: "site-settings",
fallbackLabel: `${parsedSettings.locales.de.siteName || parsedSettings.locales.en.siteName} logo dark`,
required: false,
})
: {
assetId: null,
url: "",
createdAssetId: null,
uploadedUrl: null,
};
const faviconSelection = faviconMedia
? await resolveMediaSelection({
media: mediaFieldInputSchema.parse(faviconMedia),
uploadFile: formData.get("faviconFile"),
folder: "site-settings",
fallbackLabel: `${parsedSettings.locales.de.siteName || parsedSettings.locales.en.siteName} favicon`,
required: false,
})
: {
assetId: null,
url: "",
createdAssetId: null,
uploadedUrl: null,
};
const defaultOgImageSelection = defaultOgImageMedia
? await resolveMediaSelection({
media: mediaFieldInputSchema.parse(defaultOgImageMedia),
uploadFile: formData.get("defaultOgImageFile"),
folder: "site-settings",
fallbackLabel: `${parsedSettings.locales.de.siteName || parsedSettings.locales.en.siteName} og-image`,
required: false,
})
: {
assetId: null,
url: "",
createdAssetId: null,
uploadedUrl: null,
};
if (siteLogoLightSelection.createdAssetId) {
createdMediaAssetIds.push(siteLogoLightSelection.createdAssetId);
}
if (siteLogoLightSelection.uploadedUrl) {
uploadedPaths.push(siteLogoLightSelection.uploadedUrl);
}
if (siteLogoDarkSelection.createdAssetId) {
createdMediaAssetIds.push(siteLogoDarkSelection.createdAssetId);
}
if (siteLogoDarkSelection.uploadedUrl) {
uploadedPaths.push(siteLogoDarkSelection.uploadedUrl);
}
if (faviconSelection.createdAssetId) {
createdMediaAssetIds.push(faviconSelection.createdAssetId);
}
if (faviconSelection.uploadedUrl) {
uploadedPaths.push(faviconSelection.uploadedUrl);
}
if (defaultOgImageSelection.createdAssetId) {
createdMediaAssetIds.push(defaultOgImageSelection.createdAssetId);
}
if (defaultOgImageSelection.uploadedUrl) {
uploadedPaths.push(defaultOgImageSelection.uploadedUrl);
}
await updateSiteSettings(parsedSettings);
await replaceEntityMediaUsages({
entityType: SITE_SETTINGS_ENTITY_TYPE,
entityId: SITE_SETTINGS_ENTITY_ID,
usages: [
...(siteLogoLightSelection.assetId
? [
{
assetId: siteLogoLightSelection.assetId,
usageType: MediaUsageType.GENERIC,
fieldKey: SITE_SETTINGS_LOGO_LIGHT_FIELD_KEY,
},
]
: []),
...(siteLogoDarkSelection.assetId
? [
{
assetId: siteLogoDarkSelection.assetId,
usageType: MediaUsageType.GENERIC,
fieldKey: SITE_SETTINGS_LOGO_DARK_FIELD_KEY,
},
]
: []),
...(faviconSelection.assetId
? [
{
assetId: faviconSelection.assetId,
usageType: MediaUsageType.GENERIC,
fieldKey: SITE_SETTINGS_FAVICON_FIELD_KEY,
},
]
: []),
...(defaultOgImageSelection.assetId
? [
{
assetId: defaultOgImageSelection.assetId,
usageType: MediaUsageType.GENERIC,
fieldKey: SITE_SETTINGS_DEFAULT_OG_IMAGE_FIELD_KEY,
},
]
: []),
],
});
await revalidateSiteSettingsPages(parsedSettings.defaultLocale);
redirect(withFlash(getAdminAppPath("/site-settings/brand"), { success: "Einstellungen gespeichert." }));
} catch (error) {
if (isRedirectError(error)) {
throw error;
}
await cleanupCreatedMedia(createdMediaAssetIds, uploadedPaths);
const message =
error instanceof Error
? error.message
: "Einstellungen konnten nicht gespeichert werden.";
redirect(withFlash(getAdminAppPath("/site-settings/brand"), { error: message }));
}
}
export async function saveSiteLocalizationSettingsAction(formData: FormData) {
await ensureAdmin();
try {
const requestedDefaultLocale = String(formData.get("defaultLocale") ?? "");
const currentSettings = await getSiteSettings();
const parsedSettings: SiteSettings = {
...currentSettings,
defaultLocale: normalizeSiteDefaultLocale(requestedDefaultLocale),
locales: {
ar: {
siteName: String(formData.get("siteNameAr") ?? "").trim(),
titleTemplate: String(formData.get("titleTemplateAr") ?? "").trim(),
siteDescription: String(formData.get("siteDescriptionAr") ?? "").trim(),
subhead: String(formData.get("subheadAr") ?? "").trim(),
},
en: {
siteName: String(formData.get("siteNameEn") ?? "").trim(),
titleTemplate: String(formData.get("titleTemplateEn") ?? "").trim(),
siteDescription: String(formData.get("siteDescriptionEn") ?? "").trim(),
subhead: String(formData.get("subheadEn") ?? "").trim(),
},
de: {
siteName: String(formData.get("siteNameDe") ?? "").trim(),
titleTemplate: String(formData.get("titleTemplateDe") ?? "").trim(),
siteDescription: String(formData.get("siteDescriptionDe") ?? "").trim(),
subhead: String(formData.get("subheadDe") ?? "").trim(),
},
},
};
for (const locale of routing.locales) {
if (!parsedSettings.locales[locale].siteName) {
throw new Error(`Site Name fuer ${locale} ist erforderlich.`);
}
if (
!parsedSettings.locales[locale].titleTemplate ||
!parsedSettings.locales[locale].titleTemplate.includes(PAGE_TITLE_TOKEN)
) {
throw new Error(`Title Template fuer ${locale} muss {pageTitle} enthalten.`);
}
}
await updateSiteSettings(parsedSettings);
await revalidateSiteSettingsPages(parsedSettings.defaultLocale);
redirect(withFlash(getAdminAppPath("/site-settings/localization"), { success: "Einstellungen gespeichert." }));
} catch (error) {
if (isRedirectError(error)) {
throw error;
}
const message =
error instanceof Error
? error.message
: "Einstellungen konnten nicht gespeichert werden.";
redirect(withFlash(getAdminAppPath("/site-settings/localization"), { error: message }));
}
}
export async function saveSeoSettingsAction(formData: FormData) {
await ensureAdmin();
try {
const currentSettings = await getSeoSettings();
const googleRaw = String(formData.get("googleSiteVerification") ?? "").trim();
const bingRaw = String(formData.get("bingSiteVerification") ?? "").trim();
const twitterRaw = String(formData.get("twitterHandle") ?? "").trim();
const googleSiteVerification = normalizeVerificationToken(googleRaw);
const bingSiteVerification = normalizeVerificationToken(bingRaw);
const twitterHandle = normalizeTwitterHandle(twitterRaw);
if (googleRaw && !googleSiteVerification) {
throw new Error("Google Verification Code darf nur Buchstaben, Zahlen, - und _ enthalten.");
}
if (bingRaw && !bingSiteVerification) {
throw new Error("Bing Verification Code darf nur Buchstaben, Zahlen, - und _ enthalten.");
}
if (twitterRaw && !twitterHandle) {
throw new Error("X/Twitter Handle ist ungueltig (max. 15 Zeichen, Buchstaben/Zahlen/_).");
}
const parsedSettings: SeoSettings = {
...currentSettings,
allowIndexing: isCheckedFormValue(formData.get("allowIndexing")),
googleSiteVerification,
bingSiteVerification,
twitterHandle,
structuredDataType: normalizeStructuredDataType(formData.get("structuredDataType")),
structuredDataName: String(formData.get("structuredDataName") ?? "").trim().slice(0, 120),
structuredDataJobTitle: String(formData.get("structuredDataJobTitle") ?? "").trim().slice(0, 160),
sameAs: normalizeSameAs(formData.get("sameAs")),
locales: {
ar: { keywords: normalizeKeywords(formData.get("keywordsAr")) },
en: { keywords: normalizeKeywords(formData.get("keywordsEn")) },
de: { keywords: normalizeKeywords(formData.get("keywordsDe")) },
},
};
await updateSeoSettings(parsedSettings);
const siteSettings = await getSiteSettings();
await revalidateSiteSettingsPages(siteSettings.defaultLocale);
revalidatePath("/sitemap.xml");
revalidatePath("/robots.txt");
revalidatePath(toInternalAdminPath("/site-settings/seo"));
redirect(withFlash(getAdminAppPath("/site-settings/seo"), { success: "SEO Einstellungen gespeichert." }));
} catch (error) {
if (isRedirectError(error)) {
throw error;
}
const message =
error instanceof Error ? error.message : "SEO Einstellungen konnten nicht gespeichert werden.";
redirect(withFlash(getAdminAppPath("/site-settings/seo"), { error: message }));
}
}