Repository Public
1// Webhooks : destinations internes permises (« hôte:port » exact), seule exception à la garde SSRF. Ajout et retrait2// exigent une confirmation récente par clé d'accès (sensitive()).3import { useId, useState, type FormEvent } from "react"4import { normalizeHostPort, type AllowEntry } from "../../../admin/webhooks"5import { t } from "../../../i18n"6import { api, apiPost } from "../../../jmap/http"7import { toast } from "../../../state/app"8import { ConfirmDialog, Icon } from "../../kit"9import { Section } from "../../Page"10import { isCancelled, sensitive, toastFail } from "../kit"11import { errorText, fmtWhen } from "../shared"1213const URL_ = "/api/admin/webhook-allowlist"14const codes = (): Record<string, string> => ({ invalid_request: t("adm.webhooks.allow.error.format"), not_found: t("adm.webhooks.allow.error.gone") })1516export function Allowlist({ entries, onChanged }: { entries: readonly AllowEntry[]; onChanged: () => void }) {17 const id = useId()18 const [hostPort, setHostPort] = useState("")19 const [note, setNote] = useState("")20 const [busy, setBusy] = useState(false)21 const [error, setError] = useState<string | null>(null)22 const [removing, setRemoving] = useState<AllowEntry | null>(null)2324 const add = async (ev: FormEvent) => {25 ev.preventDefault()26 if (busy) return27 const hp = normalizeHostPort(hostPort)28 if (!hp) return setError(t("adm.webhooks.allow.error.format"))29 setBusy(true)30 setError(null)31 try {32 // le serveur reçoit la saisie d'origine (crochets IPv6 compris) et la normalise lui-même33 await sensitive(() => apiPost(URL_, { host_port: hostPort.trim().toLowerCase(), note: note.trim() }))34 toast({ kind: "success", text: t("adm.webhooks.allow.added", { hostPort: hp }) })35 setHostPort("")36 setNote("")37 onChanged()38 } catch (e) {39 if (!isCancelled(e)) setError(errorText(e, codes()))40 } finally {41 setBusy(false)42 }43 }44 const remove = async (entry: AllowEntry) => {45 setBusy(true)46 try {47 await sensitive(() => api(`${URL_}?host_port=${encodeURIComponent(entry.host_port)}`, { method: "DELETE" }))48 toast({ kind: "success", text: t("adm.webhooks.allow.removed", { hostPort: entry.host_port }) })49 onChanged()50 } catch (e) {51 toastFail(e, codes())52 } finally {53 setBusy(false)54 }55 }5657 return (58 <Section title={t("adm.webhooks.allow.title")} description={t("adm.webhooks.allow.desc")}>59 <div data-testid="webhook-allowlist" className="stack">60 <ul className="adm-wh-rules small">61 <li>{t("adm.webhooks.ssrf.public")}</li>62 <li>{t("adm.webhooks.ssrf.redirects")}</li>63 <li>{t("adm.webhooks.ssrf.recheck")}</li>64 <li>{t("adm.webhooks.ssrf.exception")}</li>65 </ul>66 {entries.length === 0 ? (67 <p className="muted">{t("adm.webhooks.allow.empty")}</p>68 ) : (69 <div className="table-wrap">70 <table className="table cards-sm">71 <caption className="sr-only">{t("adm.webhooks.allow.title")}</caption>72 <thead>73 <tr>74 <th scope="col">{t("adm.webhooks.allow.col.hostPort")}</th>75 <th scope="col">{t("adm.webhooks.allow.col.note")}</th>76 <th scope="col">{t("adm.webhooks.allow.col.added")}</th>77 <th scope="col">78 <span className="sr-only">{t("admin.col.actions")}</span>79 </th>80 </tr>81 </thead>82 <tbody>83 {entries.map((e) => (84 <tr key={e.host_port} data-testid="allow-row" data-host-port={e.host_port}>85 <td data-label={t("adm.webhooks.allow.col.hostPort")} className="mono adm-break">86 {e.host_port}87 </td>88 <td data-label={t("adm.webhooks.allow.col.note")} className="adm-break">89 {e.note || null}90 </td>91 <td data-label={t("adm.webhooks.allow.col.added")}>{fmtWhen(e.created_at)}</td>92 <td>93 <button type="button" className="btn small danger" onClick={() => setRemoving(e)} disabled={busy}>94 <Icon name="trash" small />95 <span>{t("adm.webhooks.allow.remove")}</span>96 </button>97 </td>98 </tr>99 ))}100 </tbody>101 </table>102 </div>103 )}104 <form className="adm-form" onSubmit={(e) => void add(e)} noValidate>105 <div className="field">106 <label htmlFor={`${id}-hp`}>{t("adm.webhooks.allow.field.hostPort")}</label>107 <input id={`${id}-hp`} className="input mono" value={hostPort} onChange={(e) => setHostPort(e.target.value)} maxLength={260} autoComplete="off" autoCapitalize="none" spellCheck={false} placeholder="10.67.0.12:8080" aria-describedby={`${id}-hp-hint`} />108 <span className="hint" id={`${id}-hp-hint`}>109 {t("adm.webhooks.allow.field.hostPortHint")}110 </span>111 </div>112 <div className="field">113 <label htmlFor={`${id}-note`}>{t("adm.webhooks.allow.field.note")}</label>114 <input id={`${id}-note`} className="input" value={note} onChange={(e) => setNote(e.target.value)} maxLength={200} autoComplete="off" />115 </div>116 <div className="adm-form-foot stack">117 {error ? (118 <p className="adm-error" role="alert" data-testid="allow-error">119 {error}120 </p>121 ) : null}122 <div className="row wrap">123 <button type="submit" className="btn" disabled={busy || !hostPort.trim()} data-testid="allow-add">124 <Icon name="plus" small />125 <span>{t("adm.webhooks.allow.add")}</span>126 </button>127 </div>128 </div>129 </form>130 </div>131 {removing ? (132 <ConfirmDialog133 title={t("adm.webhooks.allow.remove.title")}134 body={<p>{t("adm.webhooks.allow.remove.body", { hostPort: removing.host_port })}</p>}135 confirmLabel={t("adm.webhooks.allow.remove")}136 danger137 // l'invite de clé d'accès s'ouvre après la fermeture de ce dialogue138 onConfirm={() => void remove(removing)}139 onClose={() => setRemoving(null)}140 />141 ) : null}142 </Section>143 )144}145