File
Blob: ui/operator/WAFConfiguration.svelte
| 1 | <script lang="ts"> |
| 2 | import Button from '../shared/Button.svelte'; |
| 3 | import SaveNotice from '../shared/SaveNotice.svelte'; |
| 4 | import { createEditor } from '../shared/editor.svelte'; |
| 5 | import { INPUT_CLASSES } from '../shared/styles'; |
| 6 | import type { WAFChannel, WAFDefaultMode } from '../shared/waf'; |
| 7 | import SnapshotTime from './SnapshotTime.svelte'; |
| 8 | import { requestWAFConfig, type WAFConfig } from './waf-api'; |
| 9 | |
| 10 | const editor = createEditor({ |
| 11 | key: () => '/_internal/api/config/waf', |
| 12 | defaults: { enabled: false, default_mode: 'BLOCK', channel: 'LTS', pinned_version: '' } as WAFConfig, |
| 13 | load: (signal, after) => requestWAFConfig(signal, undefined, after), |
| 14 | save: (value, version, signal) => requestWAFConfig(signal, JSON.stringify({ expected_version: version, config: value })), |
| 15 | draft: snapshot => ({ ...snapshot.config! }), |
| 16 | editable: snapshot => !snapshot.error && snapshot.config !== null, |
| 17 | }); |
| 18 | const snapshot = $derived(editor.query.data); |
| 19 | const draft = $derived(editor.values); |
| 20 | const readError = $derived(editor.query.error?.message ?? ''); |
| 21 | const pending = $derived(Boolean(snapshot?.version && snapshot.status.applied_version !== snapshot.version)); |
| 22 | const channelResumes = $derived(Boolean(snapshot?.config?.pinned_version && draft.channel !== snapshot.config.channel && !draft.pinned_version)); |
| 23 | const pinChanged = $derived(draft.pinned_version !== (snapshot?.config?.pinned_version ?? '')); |
| 24 | |
| 25 | function setPin(version = '') { |
| 26 | editor.form.setFieldValue('pinned_version', version); |
| 27 | editor.edited(); |
| 28 | } |
| 29 | </script> |
| 30 | |
| 31 | <div class="mb-2 flex flex-wrap items-center justify-between gap-3"> |
| 32 | <h1 class="text-[22px] leading-tight font-semibold tracking-tight sm:text-2xl">Web protection</h1> |
| 33 | <Button onclick={() => editor.refresh()} disabled={editor.query.isFetching || editor.saving}>{editor.query.isFetching ? 'Refreshing…' : 'Refresh'}</Button> |
| 34 | </div> |
| 35 | |
| 36 | {#if snapshot} |
| 37 | <p class="text-sm text-muted wrap-anywhere"> |
| 38 | Server <code class="text-foreground">{snapshot.server.address}</code> · <SnapshotTime value={snapshot.observed_at} /> |
| 39 | </p> |
| 40 | <div class="mt-4 border-y border-line py-3 text-sm"> |
| 41 | <p class="flex flex-wrap gap-x-4 gap-y-1"> |
| 42 | <span>Local protection: <strong class="font-semibold">{snapshot.status.enabled ? snapshot.status.ready ? 'Enabled' : 'Not ready' : 'Off'}</strong></span> |
| 43 | <span>Rules: <code>{snapshot.status.active_version || 'Not loaded'}</code></span> |
| 44 | <span>Engine: <code>{snapshot.status.engine_version || 'Unavailable'}</code></span> |
| 45 | </p> |
| 46 | {#if pending}<p class="mt-1 text-warning">Saved settings are pending on this server.</p>{/if} |
| 47 | {#if snapshot.status.updating}<p class="mt-1 text-muted">Checking rules…</p>{/if} |
| 48 | {#if snapshot.status.last_error} |
| 49 | <details class="mt-1 text-warning"> |
| 50 | <summary class="min-h-8 cursor-pointer py-1">Protection update needs attention</summary> |
| 51 | <p class="mt-1 max-w-[72ch] wrap-anywhere">{snapshot.status.last_error}</p> |
| 52 | </details> |
| 53 | {/if} |
| 54 | </div> |
| 55 | {/if} |
| 56 | |
| 57 | {#if readError} |
| 58 | <div class="my-4 rounded-md border border-danger/40 bg-surface px-4 py-3" role="alert"> |
| 59 | <p class="font-medium text-danger">{snapshot ? 'Refresh failed. Showing previous state.' : 'Could not load web protection.'}</p> |
| 60 | <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> |
| 61 | </div> |
| 62 | {/if} |
| 63 | |
| 64 | {#if snapshot} |
| 65 | {#if editor.readOnly} |
| 66 | <div class="mt-4 rounded-md border border-warning/40 bg-surface px-4 py-3" role="alert"> |
| 67 | <p class="font-medium text-warning">Saved settings cannot be edited by this server.</p> |
| 68 | <p class="mt-1 max-w-[72ch] text-sm wrap-anywhere">{snapshot.error || 'Use a compatible Specter version.'}</p> |
| 69 | </div> |
| 70 | {:else if editor.initialized} |
| 71 | {#if editor.stale} |
| 72 | <div class="mt-4 rounded-md border border-warning/40 bg-surface px-4 py-3" role="alert"> |
| 73 | <p class="font-medium text-warning">Saved settings changed. Your draft is unchanged.</p> |
| 74 | {#if snapshot.config} |
| 75 | <p class="mt-1 text-sm">Latest: {snapshot.config.enabled ? 'Enabled' : 'Off'} · {snapshot.config.default_mode === 'BLOCK' ? 'Block' : 'Monitor'} · {snapshot.config.channel === 'LTS' ? 'LTS' : 'Current stable'}{snapshot.config.pinned_version ? ` · Pinned ${snapshot.config.pinned_version}` : ''}</p> |
| 76 | {/if} |
| 77 | <Button class="mt-2" size="sm" onclick={() => editor.accept()} disabled={editor.query.isFetching || editor.saving || Boolean(readError)}>Load latest and discard draft</Button> |
| 78 | </div> |
| 79 | {/if} |
| 80 | <SaveNotice outcome={editor.outcome} saved={editor.outcome?.kind === 'saved' ? `Saved on ${editor.outcome.snapshot.server.address}.` : ''} failed="Could not save web protection." /> |
| 81 | |
| 82 | <form onsubmit={editor.submit} class="mt-5 max-w-2xl" aria-label="Web protection settings"> |
| 83 | <fieldset disabled={editor.saving}> |
| 84 | <legend class="sr-only">Shared web protection</legend> |
| 85 | <label class="flex min-h-10 w-fit cursor-pointer items-center gap-3 font-medium"> |
| 86 | <input type="checkbox" class="size-4 accent-accent-500" checked={draft.enabled} onchange={(event) => { editor.form.setFieldValue('enabled', event.currentTarget.checked); editor.edited(); }} aria-describedby="waf-enabled-help" /> |
| 87 | Enabled |
| 88 | </label> |
| 89 | <p id="waf-enabled-help" class="mt-1 text-sm text-muted">Turning this off disables protection for every hostname.</p> |
| 90 | <div class="mt-4 grid gap-4 sm:grid-cols-2"> |
| 91 | <div> |
| 92 | <label for="waf-mode" class="mb-1 block text-sm font-medium">Default mode</label> |
| 93 | <select id="waf-mode" class={INPUT_CLASSES} value={draft.default_mode} onchange={(event) => { editor.form.setFieldValue('default_mode', event.currentTarget.value as WAFDefaultMode); editor.edited(); }} disabled={!draft.enabled} aria-describedby="waf-mode-help"> |
| 94 | <option value="BLOCK">Block</option><option value="MONITOR">Monitor</option> |
| 95 | </select> |
| 96 | <p id="waf-mode-help" class="mt-1 text-xs text-muted">Hostname owners can override this default.</p> |
| 97 | </div> |
| 98 | <div> |
| 99 | <label for="waf-channel" class="mb-1 block text-sm font-medium">Rules channel</label> |
| 100 | <select id="waf-channel" class={INPUT_CLASSES} value={draft.channel} onchange={(event) => { editor.form.setFieldValue('channel', event.currentTarget.value as WAFChannel); setPin(); }}> |
| 101 | <option value="LTS">LTS</option><option value="STABLE">Current stable</option> |
| 102 | </select> |
| 103 | </div> |
| 104 | </div> |
| 105 | {#if channelResumes}<p class="mt-3 text-sm text-warning">Saving this channel resumes automatic updates.</p>{/if} |
| 106 | |
| 107 | <details class="mt-5 border-t border-line pt-2"> |
| 108 | <summary class="min-h-10 cursor-pointer py-2 font-medium">Advanced</summary> |
| 109 | <p class="mt-1 text-sm text-muted">Updates: {pinChanged ? 'after saving, ' : ''}{draft.pinned_version ? `pinned to ${draft.pinned_version}` : 'automatic'}.</p> |
| 110 | <div class="mt-3 flex flex-wrap gap-2"> |
| 111 | {#if snapshot.status.previous_version} |
| 112 | <Button size="sm" class="max-w-full whitespace-normal wrap-anywhere" onclick={() => setPin(snapshot?.status.previous_version)} disabled={draft.pinned_version === snapshot.status.previous_version}>Use previous version ({snapshot.status.previous_version})</Button> |
| 113 | {/if} |
| 114 | {#if draft.pinned_version}<Button size="sm" onclick={() => setPin()}>Resume automatic updates</Button>{/if} |
| 115 | </div> |
| 116 | </details> |
| 117 | </fieldset> |
| 118 | <div class="mt-4 flex flex-wrap items-center gap-2"> |
| 119 | <Button variant="primary" type="submit" disabled={!editor.canSave}>{editor.saving ? 'Saving…' : 'Save settings'}</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 | {/if} |
| 125 | {:else if editor.query.isFetching} |
| 126 | <div class="mt-5 space-y-3" role="status"> |
| 127 | <span class="sr-only">Loading web protection…</span> |
| 128 | <div class="h-4 w-48 max-w-full rounded bg-surface" aria-hidden="true"></div> |
| 129 | <div class="h-36 rounded-md border border-line bg-chrome" aria-hidden="true"></div> |
| 130 | </div> |
| 131 | {/if} |