bidstage/lib/launch-notify.ts
caelum0x 9b66375f8f fix: address review findings on reversal auditability and session persistence
HIGH — the reversal clamp no longer erases the alerting/audit trail: a
reversal that applies nothing (already fully reversed, or clamped to zero)
now records a 'clamped_reversal' adjustment row with zero applied amount
and the provider-claimed cumulative, and partial clamps store the
provider-claimed cumulative on the refund row, so provider-side
over-refunds are queryable instead of silently absorbed (migration 0024
extends the adjustment_type/amount CHECKs accordingly).

MEDIUM — persistProviderCheckoutSession now throws on a zero-row UPDATE
instead of returning nulls, so a missing checkout intent fails the request
rather than handing the buyer an unsettleable provider session.

LOW — launch-notify origin check uses the configured appUrl (same policy
as checkout); repository URLs are stored WHATWG-normalized (parsed.href);
the launch_notifications email CHECK now mirrors the application regex.

Integration: +1 over-refund audit scenario; refund-after-dispute now
asserts the no-op refund id is recorded (8/8 green vs real Postgres 16).
2026-09-05 17:29:18 +03:00

57 lines
2.1 KiB
TypeScript

/**
* Validation for the launch-notification capture path.
*
* While CHECKOUT_ENABLED=false (pre Creem approval) a founder who tries to pay
* hits a dead end; this module backs the "email me when checkout opens" capture
* so that pre-launch demand is recorded instead of lost. Inputs arrive from an
* unauthenticated public endpoint, so everything is validated strictly here and
* the route only ever stores normalized values.
*/
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
const MAX_EMAIL_LENGTH = 254;
const MAX_REPOSITORY_URL_LENGTH = 500;
export const LAUNCH_NOTIFY_SOURCES = ["checkout_disabled", "landing"] as const;
export type LaunchNotifySource = (typeof LAUNCH_NOTIFY_SOURCES)[number];
export function isLaunchNotifySource(value: unknown): value is LaunchNotifySource {
return typeof value === "string"
&& (LAUNCH_NOTIFY_SOURCES as readonly string[]).includes(value);
}
export function normalizeLaunchNotifyEmail(value: unknown): string {
if (typeof value !== "string") {
throw new Error("A valid email address is required");
}
const email = value.trim().toLowerCase();
if (email.length === 0 || email.length > MAX_EMAIL_LENGTH || !EMAIL_PATTERN.test(email)) {
throw new Error("A valid email address is required");
}
return email;
}
export function normalizeLaunchNotifyRepositoryUrl(value: unknown): string | null {
if (value === null || value === undefined) return null;
if (typeof value !== "string") {
throw new Error("The repository URL must be a web address");
}
const url = value.trim();
if (url.length === 0) return null;
if (url.length > MAX_REPOSITORY_URL_LENGTH) {
throw new Error("The repository URL is too long");
}
let parsed: URL;
try {
parsed = new URL(url);
} catch {
throw new Error("The repository URL must be a web address");
}
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
throw new Error("The repository URL must be a web address");
}
// Store the WHATWG-normalized form (lowercased scheme/host, canonical
// encoding) so later comparisons never depend on how the founder typed it.
return parsed.href;
}