Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/e.c.$outboundEmailId.tsx
T
2026-07-08 16:15:37 +02:00

46 lines
1.8 KiB
TypeScript

/**
* AK-P14a · P14 Email — click-tracking redirect.
*
* GET /e/c/:outboundEmailId?u=<absolute-http(s)-url>
*
* Records an OutboundEmailClick (IP stored only as a truncated sha256 hash) and
* 302-redirects to the target. The target MUST be an absolute http(s) URL —
* `safeRedirectTarget` rejects `javascript:` / `data:` / relative paths so this
* unauthenticated endpoint can never become an open-redirect. A missing /
* invalid target returns 400; an unknown email id still redirects (tracking is
* best-effort) when the target is valid.
*
* The marketing send path (AK-P14b) rewrites campaign links through this route;
* this is the tracking sink only.
*/
import type { LoaderFunctionArgs } from "react-router";
import { anonymizeTrackingIp, recordEmailClick, safeRedirectTarget } from "@hivegrid/db/email-tracking.server";
import { db } from "~/utils/db.server";
import { getClientIPAddress } from "~/utils/server/IpUtils";
import { createScopedLogger } from "~/utils/logger.server";
const log = createScopedLogger("email.tracking.click");
export async function loader({ params, request }: LoaderFunctionArgs) {
const url = new URL(request.url);
const target = safeRedirectTarget(url.searchParams.get("u"));
if (!target) {
return new Response("Invalid or missing redirect target.", { status: 400 });
}
const outboundEmailId = params.outboundEmailId;
if (outboundEmailId) {
try {
const ipHash = anonymizeTrackingIp(getClientIPAddress(request));
await recordEmailClick({ outboundEmailId, link: target, ipHash }, db as never);
} catch (error) {
// Tracking must never block the redirect.
log.error({ outboundEmailId, err: error instanceof Error ? error.message : String(error) }, "email click tracking failed");
}
}
return new Response(null, { status: 302, headers: { Location: target } });
}