caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

76 lines
2.6 KiB
TypeScript

import { z } from "zod";
import { WEBHOOK_EVENTS } from "@/lib/webhooks/types";
// Zod schemas validate all input at the /api/webhooks boundary. Never trust the
// raw request body — parse it here first.
const eventEnum = z.enum(WEBHOOK_EVENTS);
// Reject URLs that could be used for SSRF — private/loopback IPv4 ranges,
// link-local (169.254.x.x / AWS metadata), IPv6 loopback, and bare localhost.
// http:// is still accepted for local dev receivers, but only for non-internal
// hostnames so the intent (dev convenience) is narrow. Production deployments
// should additionally enforce HTTPS at the load-balancer or middleware layer.
function isPrivateUrl(raw: string): boolean {
let url: URL;
try {
url = new URL(raw);
} catch {
return false; // already rejected by .url() above
}
const host = url.hostname.toLowerCase();
// Loopback / localhost
if (host === "localhost" || host === "::1" || host === "0.0.0.0") return true;
// IPv4: loopback (127.x), private class A/B/C, link-local (169.254.x)
const v4 = host.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
if (v4) {
const [, a, b] = v4.map(Number);
if (a === 127) return true;
if (a === 10) return true;
if (a === 172 && b >= 16 && b <= 31) return true;
if (a === 192 && b === 168) return true;
if (a === 169 && b === 254) return true;
}
// IPv6 private ranges
if (host.startsWith("[::") || host === "::1") return true;
if (/^\[f[cde]/i.test(host)) return true; // fc00::/7 unique-local, fe80:: link-local
return false;
}
const webhookUrl = z
.string()
.trim()
.url("Must be a valid URL.")
.max(2048, "URL is too long.")
.refine(
(value) => value.startsWith("https://") || value.startsWith("http://"),
"URL must use http or https."
)
.refine(
(value) => !isPrivateUrl(value),
"Webhook URL must not target private or loopback addresses."
);
export const createWebhookSchema = z.object({
url: webhookUrl,
// At least one event so the webhook does something; de-duplicated downstream.
events: z.array(eventEnum).min(1, "Select at least one event."),
});
export const updateWebhookSchema = z
.object({
url: webhookUrl.optional(),
events: z.array(eventEnum).min(1, "Select at least one event.").optional(),
status: z.enum(["active", "disabled"]).optional(),
})
.refine(
(value) =>
value.url !== undefined ||
value.events !== undefined ||
value.status !== undefined,
"Provide at least one field to update."
);
export type CreateWebhookInput = z.infer<typeof createWebhookSchema>;
export type UpdateWebhookInput = z.infer<typeof updateWebhookSchema>;