File
Blob: ui/client/WebProtection.svelte
| 1 | <script lang="ts"> |
| 2 | import { tick } from 'svelte'; |
| 3 | import { createQuery } from '@tanstack/svelte-query'; |
| 4 | import Button from '../shared/Button.svelte'; |
| 5 | import SaveNotice from '../shared/SaveNotice.svelte'; |
| 6 | import { createEditor } from '../shared/editor.svelte'; |
| 7 | import { INPUT_CLASSES } from '../shared/styles'; |
| 8 | import type { WAFMode } from '../shared/waf'; |
| 9 | import { hostnameQueryOptions } from './api'; |
| 10 | import { requestHostWAF } from './waf-api'; |
| 11 | |
| 12 | type Draft = { mode: WAFMode; exclusions: { rule: string; path: string }[] }; |
| 13 | let hostname = $state(''); |
| 14 | let form = $state<HTMLFormElement>(); |
| 15 | const hostnamesQuery = createQuery(() => hostnameQueryOptions); |
| 16 | const hostnames = $derived(hostnamesQuery.data?.map(item => item.hostname) ?? null); |
| 17 | const loadingHostnames = $derived(hostnamesQuery.isFetching); |
| 18 | const hostnameError = $derived(hostnamesQuery.error?.message ?? ''); |
| 19 | const choices = $derived([...(hostnames ?? [])].sort((a, b) => a.localeCompare(b))); |
| 20 | const registered = $derived(choices.includes(hostname)); |
| 21 | const editor = createEditor({ |
| 22 | key: () => hostname ? `/api/waf?hostname=${encodeURIComponent(hostname)}` : '', |
| 23 | defaults: { mode: 'INHERIT', exclusions: [] } as Draft, |
| 24 | load: signal => requestHostWAF(hostname, signal), |
| 25 | save: (value, version, signal) => requestHostWAF(hostname, signal, JSON.stringify({ |
| 26 | expected_version: version, |
| 27 | policy: { mode: value.mode, exclusions: value.exclusions.map(({ rule, path }) => ({ rule_id: Number(rule), path })) }, |
| 28 | })), |
| 29 | draft: snapshot => ({ mode: snapshot.policy.mode, exclusions: snapshot.policy.exclusions.map(item => ({ rule: String(item.rule_id), path: item.path })) }), |
| 30 | }); |
| 31 | const snapshot = $derived(editor.query.data); |
| 32 | const draft = $derived(editor.values); |
| 33 | const readError = $derived(editor.query.error?.message ?? ''); |
| 34 | const placeholder = $derived(hostnames === null |
| 35 | ? hostnameError && !loadingHostnames ? 'Hostnames unavailable' : 'Loading hostnames…' |
| 36 | : choices.length ? 'Select hostname…' : 'No registered hostnames'); |
| 37 | |
| 38 | async function refresh() { |
| 39 | if (!editor.saving) await Promise.allSettled([hostname ? editor.refresh() : Promise.resolve(), hostnamesQuery.refetch()]); |
| 40 | } |
| 41 | |
| 42 | async function addExclusion() { |
| 43 | const index = draft.exclusions.length; |
| 44 | editor.form.pushFieldValue('exclusions', { rule: '', path: '' }); |
| 45 | editor.edited(); |
| 46 | await tick(); |
| 47 | form?.querySelector<HTMLInputElement>(`#waf-rule-${index}`)?.focus(); |
| 48 | } |
| 49 | </script> |
| 50 | |
| 51 | <section class="mt-6 min-w-0 border-t border-line pt-5" aria-labelledby="protection-heading"> |
| 52 | <div class="mb-3.5 flex flex-wrap items-center justify-between gap-3"> |
| 53 | <h2 id="protection-heading" class="text-lg leading-snug font-semibold tracking-tight">Web protection</h2> |
| 54 | <Button size="sm" onclick={refresh} disabled={editor.query.isFetching || editor.saving || loadingHostnames}>{editor.query.isFetching || loadingHostnames ? 'Refreshing…' : 'Refresh'}</Button> |
| 55 | </div> |
| 56 | <div class="max-w-lg"> |
| 57 | <label for="protected-hostname" class="mb-1.5 block text-sm font-medium">Hostname</label> |
| 58 | <select id="protected-hostname" class={INPUT_CLASSES} bind:value={hostname} disabled={editor.query.isFetching || editor.saving || editor.dirty || !choices.length}> |
| 59 | <option value="" disabled>{placeholder}</option> |
| 60 | {#if hostname && !registered}<option value={hostname}>{hostname} (no longer registered)</option>{/if} |
| 61 | {#each choices as host (host)}<option value={host}>{host}</option>{/each} |
| 62 | </select> |
| 63 | {#if editor.dirty}<p class="mt-1 text-xs text-muted">Save or discard changes before choosing another hostname.</p>{/if} |
| 64 | </div> |
| 65 | {#if hostnameError}<p class="mt-2 text-sm text-warning">{hostnames === null ? 'Could not load hostnames. Refresh to retry.' : 'Using the previous hostname list.'}</p> |
| 66 | {:else if hostnames !== null && !choices.length}<p class="mt-2 text-sm text-muted"><a href="#configuration" class="text-accent-300 underline underline-offset-4 hover:text-accent-200">Add a tunnel</a> to register a hostname.</p>{/if} |
| 67 | {#if hostname && !registered && hostnames !== null}<p class="mt-2 text-sm text-warning">This hostname is no longer registered to this client.</p>{/if} |
| 68 | |
| 69 | {#if readError} |
| 70 | <div class="mt-3 rounded-md border border-danger/40 bg-chrome px-3.5 py-3" role="alert"> |
| 71 | <p class="font-medium text-danger">{snapshot ? 'Refresh failed. Showing previous policy.' : 'Could not load hostname protection.'}</p> |
| 72 | <details class="mt-1"><summary class="min-h-8 cursor-pointer py-1 text-muted">Request error</summary><p class="mt-1 max-w-[72ch] text-sm wrap-anywhere">{readError}</p></details> |
| 73 | </div> |
| 74 | {/if} |
| 75 | {#if editor.stale} |
| 76 | <div class="mt-3 rounded-md border border-warning/40 bg-chrome px-3.5 py-3" role="alert"> |
| 77 | <p class="font-medium text-warning">Saved policy changed. Your draft is unchanged.</p> |
| 78 | <Button class="mt-2" size="sm" onclick={() => editor.accept()} disabled={editor.query.isFetching || editor.saving || Boolean(readError)}>Load latest and discard draft</Button> |
| 79 | </div> |
| 80 | {/if} |
| 81 | <SaveNotice outcome={editor.outcome} saved="Hostname policy saved." failed="Could not save hostname protection." class="mt-3 bg-chrome px-3.5 py-3" /> |
| 82 | |
| 83 | {#if editor.initialized} |
| 84 | <form bind:this={form} onsubmit={(event) => { if (registered) return editor.submit(event); event.preventDefault(); }} class="mt-4" aria-label={`Protection for ${hostname}`}> |
| 85 | <fieldset disabled={editor.saving || !registered}> |
| 86 | <legend class="sr-only">Hostname policy</legend> |
| 87 | <div class="max-w-lg"> |
| 88 | <label for="hostname-waf-mode" class="mb-1.5 block text-sm font-medium">Mode</label> |
| 89 | <select id="hostname-waf-mode" class={INPUT_CLASSES} value={draft.mode} onchange={(event) => { editor.form.setFieldValue('mode', event.currentTarget.value as WAFMode); editor.edited(); }}> |
| 90 | <option value="INHERIT">Server default</option><option value="BLOCK">Block</option><option value="MONITOR">Monitor</option><option value="OFF">Off</option> |
| 91 | </select> |
| 92 | </div> |
| 93 | <div class="mt-4 mb-2 flex flex-wrap items-center justify-between gap-2"> |
| 94 | <h3 class="font-semibold">Rule exceptions</h3> |
| 95 | <Button size="sm" onclick={addExclusion}>Add exception</Button> |
| 96 | </div> |
| 97 | {#if draft.exclusions.length} |
| 98 | <div class="border-t border-line"> |
| 99 | {#each draft.exclusions as item, index} |
| 100 | <div class="grid items-start gap-3 border-b border-line py-3 sm:grid-cols-[9rem_minmax(0,1fr)_auto]"> |
| 101 | <div> |
| 102 | <label for={`waf-rule-${index}`} class="mb-1 block text-sm font-medium">Rule ID</label> |
| 103 | <input id={`waf-rule-${index}`} class={INPUT_CLASSES + ' font-mono'} value={item.rule} oninput={(event) => { editor.form.setFieldValue(`exclusions[${index}].rule`, event.currentTarget.value); editor.edited(); }} inputmode="numeric" pattern={'[0-9]{6}'} maxlength="6" required placeholder="941100" title="Enter the six-digit rule ID from the blocked request." aria-label={`Rule ID for exception ${index + 1}`} /> |
| 104 | </div> |
| 105 | <div class="min-w-0"> |
| 106 | <label for={`waf-path-${index}`} class="mb-1 block text-sm font-medium">Exact path <span class="font-normal text-muted">(optional)</span></label> |
| 107 | <input id={`waf-path-${index}`} class={INPUT_CLASSES + ' font-mono'} value={item.path} oninput={(event) => { editor.form.setFieldValue(`exclusions[${index}].path`, event.currentTarget.value); editor.edited(); }} autocomplete="off" autocapitalize="none" spellcheck={false} placeholder="/api/save" aria-describedby="waf-path-help" /> |
| 108 | </div> |
| 109 | <Button size="sm" variant="ghost" class="justify-self-start sm:mt-6" onclick={() => { editor.form.removeFieldValue('exclusions', index); editor.edited(); }} aria-label={`Remove exception ${item.rule || index + 1} and enable the rule again`}>{item.rule ? 'Enable again' : 'Remove'}</Button> |
| 110 | </div> |
| 111 | {/each} |
| 112 | </div> |
| 113 | <p id="waf-path-help" class="mt-2 text-xs text-muted">Use the escaped path without a query string. An empty path applies to every path on this hostname.</p> |
| 114 | {:else} |
| 115 | <p class="text-sm text-muted">No rule exceptions.</p> |
| 116 | {/if} |
| 117 | </fieldset> |
| 118 | <div class="mt-4 flex flex-wrap items-center gap-2"> |
| 119 | <Button variant="primary" type="submit" disabled={!editor.canSave || !registered}>{editor.saving ? 'Saving…' : 'Save policy'}</Button> |
| 120 | {#if editor.dirty && !editor.stale}<Button variant="ghost" onclick={() => editor.accept()} disabled={editor.saving}>Discard changes</Button>{/if} |
| 121 | {#if editor.dirty}<span class="text-sm text-muted">Unsaved changes</span>{/if} |
| 122 | </div> |
| 123 | </form> |
| 124 | {:else if editor.query.isFetching} |
| 125 | <div class="mt-4 h-24 rounded-md border border-line bg-chrome" role="status"><span class="sr-only">Loading hostname protection…</span></div> |
| 126 | {/if} |
| 127 | </section> |