|
| 1 | +// Per-project "Security" tab — exposed-services / port-drift scans |
| 2 | +// (docs/uptime-monitoring-prd.md §12). Shows open findings (ports open to the |
| 3 | +// public internet that aren't in the accepted baseline) and recent scan history. |
| 4 | +// The actual scanning runs on the off-Railway prober droplet; this page requests |
| 5 | +// scans and displays their results. Degrades to an empty state if the migration |
| 6 | +// isn't applied yet. |
| 7 | + |
| 8 | +import { notFound } from "next/navigation"; |
| 9 | +import { createClient } from "@/lib/supabase/server"; |
| 10 | +import { RequestScanButton } from "./request-scan-button"; |
| 11 | + |
| 12 | +interface FindingRow { |
| 13 | + id: string; |
| 14 | + port: number; |
| 15 | + service: string | null; |
| 16 | + severity: "low" | "medium" | "high"; |
| 17 | + state: "open" | "acknowledged" | "baseline" | "muted"; |
| 18 | + first_seen_at: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface ScanRow { |
| 22 | + id: string; |
| 23 | + host: string; |
| 24 | + status: "queued" | "running" | "done" | "failed"; |
| 25 | + open_ports: number[] | null; |
| 26 | + created_at: string; |
| 27 | + completed_at: string | null; |
| 28 | +} |
| 29 | + |
| 30 | +const SEV_COLOR: Record<FindingRow["severity"], string> = { |
| 31 | + high: "var(--color-fail)", |
| 32 | + medium: "var(--color-warn)", |
| 33 | + low: "var(--color-muted)", |
| 34 | +}; |
| 35 | + |
| 36 | +function fmt(ts: string): string { |
| 37 | + return new Date(ts).toLocaleString(); |
| 38 | +} |
| 39 | + |
| 40 | +export default async function ProjectSecurityPage({ |
| 41 | + params, |
| 42 | +}: { |
| 43 | + params: Promise<{ id: string }>; |
| 44 | +}) { |
| 45 | + const { id } = await params; |
| 46 | + const supabase = await createClient(); |
| 47 | + |
| 48 | + const { data: project } = await supabase |
| 49 | + .from("projects") |
| 50 | + .select("id, url") |
| 51 | + .eq("id", id) |
| 52 | + .maybeSingle(); |
| 53 | + if (!project) notFound(); |
| 54 | + |
| 55 | + let host = project.url; |
| 56 | + try { |
| 57 | + host = new URL(project.url).host; |
| 58 | + } catch { |
| 59 | + /* keep raw url */ |
| 60 | + } |
| 61 | + |
| 62 | + // Both queries degrade to [] if the migration isn't applied yet. |
| 63 | + const { data: findingsData } = await supabase |
| 64 | + .from("port_findings") |
| 65 | + .select("id, port, service, severity, state, first_seen_at") |
| 66 | + .eq("project_id", id) |
| 67 | + .neq("state", "muted") |
| 68 | + .order("severity", { ascending: false }); |
| 69 | + const openFindings = (findingsData ?? []).filter( |
| 70 | + (f: FindingRow) => f.state === "open", |
| 71 | + ); |
| 72 | + |
| 73 | + const { data: scansData } = await supabase |
| 74 | + .from("port_scans") |
| 75 | + .select("id, host, status, open_ports, created_at, completed_at") |
| 76 | + .eq("project_id", id) |
| 77 | + .order("created_at", { ascending: false }) |
| 78 | + .limit(10); |
| 79 | + const scans = (scansData ?? []) as ScanRow[]; |
| 80 | + |
| 81 | + return ( |
| 82 | + <div className="flex flex-col gap-6"> |
| 83 | + <div className="flex flex-wrap items-start justify-between gap-3"> |
| 84 | + <div> |
| 85 | + <h2 className="text-lg font-semibold">Exposed Services</h2> |
| 86 | + <p className="max-w-2xl text-sm text-[var(--color-muted)]"> |
| 87 | + Detects ports open to the public internet on{" "} |
| 88 | + <span className="font-mono">{host}</span> that aren't in your |
| 89 | + accepted baseline — e.g. a database or cache accidentally exposed. |
| 90 | + Scans run only against your own verified host. |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + <RequestScanButton projectId={id} /> |
| 94 | + </div> |
| 95 | + |
| 96 | + {/* Open findings */} |
| 97 | + <section className="flex flex-col gap-2"> |
| 98 | + <h3 className="text-sm font-semibold text-[var(--color-muted)]"> |
| 99 | + Open findings |
| 100 | + </h3> |
| 101 | + {openFindings.length === 0 ? ( |
| 102 | + <div className="rounded-lg border border-[var(--color-border)] bg-[var(--color-card)] p-6 text-sm text-[var(--color-muted)]"> |
| 103 | + No open findings. Run a scan to establish a baseline of the ports |
| 104 | + that are currently open, then we'll alert you when a{" "} |
| 105 | + <em>new</em> one appears. |
| 106 | + </div> |
| 107 | + ) : ( |
| 108 | + <ul className="flex flex-col gap-2"> |
| 109 | + {openFindings.map((f: FindingRow) => ( |
| 110 | + <li |
| 111 | + key={f.id} |
| 112 | + className="flex items-center justify-between rounded-lg border border-[var(--color-border)] bg-[var(--color-card)] px-4 py-3" |
| 113 | + > |
| 114 | + <div className="flex items-center gap-3"> |
| 115 | + <span |
| 116 | + className="rounded px-2 py-0.5 text-xs font-semibold uppercase" |
| 117 | + style={{ color: SEV_COLOR[f.severity] }} |
| 118 | + > |
| 119 | + {f.severity} |
| 120 | + </span> |
| 121 | + <span className="font-mono text-sm"> |
| 122 | + {host}:{f.port} |
| 123 | + {f.service ? ( |
| 124 | + <span className="text-[var(--color-muted)]"> |
| 125 | + {" "} |
| 126 | + ({f.service}) |
| 127 | + </span> |
| 128 | + ) : null} |
| 129 | + </span> |
| 130 | + </div> |
| 131 | + <span className="text-xs text-[var(--color-muted)]"> |
| 132 | + first seen {fmt(f.first_seen_at)} |
| 133 | + </span> |
| 134 | + </li> |
| 135 | + ))} |
| 136 | + </ul> |
| 137 | + )} |
| 138 | + </section> |
| 139 | + |
| 140 | + {/* Scan history */} |
| 141 | + <section className="flex flex-col gap-2"> |
| 142 | + <h3 className="text-sm font-semibold text-[var(--color-muted)]"> |
| 143 | + Recent scans |
| 144 | + </h3> |
| 145 | + {scans.length === 0 ? ( |
| 146 | + <div className="rounded-lg border border-[var(--color-border)] bg-[var(--color-card)] p-6 text-sm text-[var(--color-muted)]"> |
| 147 | + No scans yet. |
| 148 | + </div> |
| 149 | + ) : ( |
| 150 | + <div className="overflow-x-auto rounded-lg border border-[var(--color-border)]"> |
| 151 | + <table className="w-full text-sm"> |
| 152 | + <thead className="bg-[var(--color-card)] text-left text-[var(--color-muted)]"> |
| 153 | + <tr> |
| 154 | + <th className="px-4 py-2 font-medium">Requested</th> |
| 155 | + <th className="px-4 py-2 font-medium">Status</th> |
| 156 | + <th className="px-4 py-2 font-medium">Open ports</th> |
| 157 | + </tr> |
| 158 | + </thead> |
| 159 | + <tbody> |
| 160 | + {scans.map((s) => ( |
| 161 | + <tr |
| 162 | + key={s.id} |
| 163 | + className="border-t border-[var(--color-border)]" |
| 164 | + > |
| 165 | + <td className="px-4 py-2">{fmt(s.created_at)}</td> |
| 166 | + <td className="px-4 py-2">{s.status}</td> |
| 167 | + <td className="px-4 py-2 font-mono"> |
| 168 | + {s.open_ports && s.open_ports.length > 0 |
| 169 | + ? s.open_ports.join(", ") |
| 170 | + : s.status === "done" |
| 171 | + ? "none" |
| 172 | + : "—"} |
| 173 | + </td> |
| 174 | + </tr> |
| 175 | + ))} |
| 176 | + </tbody> |
| 177 | + </table> |
| 178 | + </div> |
| 179 | + )} |
| 180 | + </section> |
| 181 | + </div> |
| 182 | + ); |
| 183 | +} |
0 commit comments