Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(ui): WAF panel + tab on the app detail page

Juan Denis70883d50a9c2
diff --git a/frontend/src/components/apps/WafPanel.jsx b/frontend/src/components/apps/WafPanel.jsx
new file mode 100644
index 00000000..4af2c653
--- /dev/null
+++ b/frontend/src/components/apps/WafPanel.jsx
@@ -0,0 +1,402 @@
+import { useState, useEffect, useCallback } from 'react';
+import { ShieldCheck, ListChecks, X, RefreshCw } from 'lucide-react';
+import api from '../../services/api';
+import { useToast } from '../../contexts/ToastContext';
+import { Pill, SegControl } from '../ds';
+import { Button } from '@/components/ui/button';
+import { Input } from '@/components/ui/input';
+import { Label } from '@/components/ui/label';
+
+// WAF modes with one-line descriptions shown under the selector.
+const MODE_OPTIONS = [
+ { value: 'off', label: 'Off' },
+ { value: 'detect', label: 'Detect-only' },
+ { value: 'block', label: 'Block' },
+];
+
+const MODE_HINTS = {
+ off: 'ModSecurity is disabled for this app — no rules are evaluated.',
+ detect: 'Rules are evaluated and matches are logged, but requests are never blocked.',
+ block: 'Malicious requests that exceed the anomaly threshold are rejected.',
+};
+
+const MODE_PILL = {
+ off: 'gray',
+ detect: 'amber',
+ block: 'green',
+};
+
+// ModSecurity severities → pill colors (best-effort; tolerate unknown values).
+const SEVERITY_PILL = {
+ critical: 'red',
+ emergency: 'red',
+ alert: 'red',
+ error: 'red',
+ warning: 'amber',
+ notice: 'amber',
+ info: 'cyan',
+ debug: 'gray',
+};
+
+function severityKind(severity) {
+ if (!severity) return 'gray';
+ return SEVERITY_PILL[String(severity).toLowerCase()] || 'gray';
+}
+
+const DEFAULT_POLICY = {
+ mode: 'off',
+ paranoia_level: 1,
+ anomaly_threshold: 5,
+ disabled_rule_ids: [],
+};
+
+const WafPanel = ({ app, onChanged }) => {
+ const toast = useToast();
+
+ const [policy, setPolicy] = useState(DEFAULT_POLICY);
+ const [status, setStatus] = useState(null);
+ const [loading, setLoading] = useState(true);
+
+ const [installing, setInstalling] = useState(false);
+ const [saving, setSaving] = useState(false);
+ const [applying, setApplying] = useState(false);
+
+ const [ruleInput, setRuleInput] = useState('');
+
+ const [events, setEvents] = useState([]);
+ const [eventsLoading, setEventsLoading] = useState(true);
+
+ const loadPolicy = useCallback(async () => {
+ try {
+ const data = await api.getWafPolicy(app.id);
+ const merged = { ...DEFAULT_POLICY, ...(data || {}) };
+ // Normalize disabled rule ids to a clean numeric array.
+ merged.disabled_rule_ids = Array.isArray(merged.disabled_rule_ids)
+ ? merged.disabled_rule_ids.map(Number).filter((n) => Number.isFinite(n))
+ : [];
+ setPolicy(merged);
+ } catch (err) {
+ console.error('Failed to load WAF policy:', err);
+ }
+ }, [app.id]);
+
+ const loadStatus = useCallback(async () => {
+ try {
+ const data = await api.getWafStatus();
+ setStatus(data);
+ } catch (err) {
+ console.error('Failed to load WAF status:', err);
+ }
+ }, []);
+
+ const loadEvents = useCallback(async () => {
+ setEventsLoading(true);
+ try {
+ const data = await api.getWafEvents(app.id, 50);
+ setEvents(Array.isArray(data) ? data : (data?.events || []));
+ } catch (err) {
+ console.error('Failed to load WAF events:', err);
+ setEvents([]);
+ } finally {
+ setEventsLoading(false);
+ }
+ }, [app.id]);
+
+ useEffect(() => {
+ let active = true;
+ (async () => {
+ setLoading(true);
+ await Promise.all([loadPolicy(), loadStatus()]);
+ if (active) setLoading(false);
+ })();
+ loadEvents();
+ return () => { active = false; };
+ }, [loadPolicy, loadStatus, loadEvents]);
+
+ function setField(key, value) {
+ setPolicy((prev) => ({ ...prev, [key]: value }));
+ }
+
+ async function handleInstall() {
+ setInstalling(true);
+ try {
+ const result = await api.installWaf();
+ if (result?.success === false) {
+ toast.error(result.message || result.error || 'Failed to install ModSecurity');
+ } else {
+ toast.success(result?.message || 'ModSecurity installed.');
+ }
+ await loadStatus();
+ } catch (err) {
+ toast.error(err.message || 'Failed to install ModSecurity');
+ } finally {
+ setInstalling(false);
+ }
+ }
+
+ function handleAddRule() {
+ const id = parseInt(ruleInput, 10);
+ if (!Number.isFinite(id)) {
+ setRuleInput('');
+ return;
+ }
+ setPolicy((prev) => {
+ if (prev.disabled_rule_ids.includes(id)) return prev;
+ return { ...prev, disabled_rule_ids: [...prev.disabled_rule_ids, id] };
+ });
+ setRuleInput('');
+ }
+
+ function handleRuleKeyDown(e) {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ handleAddRule();
+ }
+ }
+
+ function handleRemoveRule(id) {
+ setField('disabled_rule_ids', policy.disabled_rule_ids.filter((r) => r !== id));
+ }
+
+ function buildPayload() {
+ return {
+ mode: policy.mode,
+ paranoia_level: parseInt(policy.paranoia_level, 10) || 1,
+ anomaly_threshold: parseInt(policy.anomaly_threshold, 10) || 0,
+ disabled_rule_ids: policy.disabled_rule_ids,
+ };
+ }
+
+ async function handleSave() {
+ setSaving(true);
+ try {
+ const payload = buildPayload();
+ const data = await api.updateWafPolicy(app.id, payload);
+ if (data) {
+ const merged = { ...DEFAULT_POLICY, ...data };
+ merged.disabled_rule_ids = Array.isArray(merged.disabled_rule_ids)
+ ? merged.disabled_rule_ids.map(Number).filter((n) => Number.isFinite(n))
+ : payload.disabled_rule_ids;
+ setPolicy(merged);
+ }
+ toast.success('WAF policy saved.');
+ onChanged?.();
+ } catch (err) {
+ toast.error(err.message || 'Failed to save WAF policy');
+ } finally {
+ setSaving(false);
+ }
+ }
+
+ async function handleApply() {
+ setApplying(true);
+ try {
+ const result = await api.applyWaf(app.id);
+ if (result?.success === false) {
+ toast.error(result.message || result.error || 'Failed to apply WAF rules');
+ } else if (result?.manual_include) {
+ toast.warning(
+ `Rules written, but no vhost was found. Include it manually: ${result.manual_include}`,
+ 12000,
+ );
+ } else {
+ toast.success(result?.message || 'WAF rules applied and nginx reloaded.');
+ }
+ } catch (err) {
+ toast.error(err.message || 'Failed to apply WAF rules');
+ } finally {
+ setApplying(false);
+ }
+ }
+
+ const installed = status?.installed;
+ const modeHint = MODE_HINTS[policy.mode] || '';
+
+ return (
+ <div className="waf-panel">
+ {/* Install banner — only when ModSecurity is reported absent */}
+ {status && !installed && (
+ <div className="waf-panel__banner">
+ <div className="waf-panel__banner-text">
+ <strong>ModSecurity is not installed on this server</strong>
+ <span>
+ Install ModSecurity and the OWASP Core Rule Set to enable the web
+ application firewall for this app.
+ </span>
+ </div>
+ <Button size="sm" onClick={handleInstall} disabled={installing}>
+ {installing ? 'Installing…' : 'Install'}
+ </Button>
+ </div>
+ )}
+
+ {/* Policy controls */}
+ <div className="app-panel waf-panel__section">
+ <div className="app-panel-header">
+ <ShieldCheck />
+ <span>Firewall Policy</span>
+ <span className="app-panel-header-actions">
+ {!loading && (
+ <Pill kind={MODE_PILL[policy.mode] || 'gray'}>
+ {MODE_OPTIONS.find((m) => m.value === policy.mode)?.label || 'Off'}
+ </Pill>
+ )}
+ </span>
+ </div>
+ <div className="app-panel-body">
+ <p className="app-panel-hint">
+ ModSecurity with the OWASP Core Rule Set inspects incoming requests for
+ common attacks (SQL injection, XSS, and more) before they reach this app.
+ </p>
+
+ <div className="waf-panel__mode">
+ <Label>Mode</Label>
+ <SegControl
+ options={MODE_OPTIONS}
+ value={policy.mode}
+ onChange={(v) => setField('mode', v)}
+ />
+ <span className="waf-panel__mode-hint">{modeHint}</span>
+ </div>
+
+ <div className="container-ops__grid">
+ <div className="container-ops__input">
+ <Label htmlFor={`waf-paranoia-${app.id}`}>Paranoia level (1–4)</Label>
+ <Input
+ id={`waf-paranoia-${app.id}`}
+ type="number"
+ min={1}
+ max={4}
+ value={policy.paranoia_level}
+ onChange={(e) => setField('paranoia_level', e.target.value)}
+ disabled={loading}
+ />
+ </div>
+ <div className="container-ops__input">
+ <Label htmlFor={`waf-threshold-${app.id}`}>Anomaly threshold</Label>
+ <Input
+ id={`waf-threshold-${app.id}`}
+ type="number"
+ min={0}
+ value={policy.anomaly_threshold}
+ onChange={(e) => setField('anomaly_threshold', e.target.value)}
+ disabled={loading}
+ />
+ </div>
+ </div>
+
+ <div className="waf-panel__rules">
+ <Label htmlFor={`waf-rule-${app.id}`}>Disabled CRS rule IDs</Label>
+ <span className="container-ops__field-hint">
+ Suppress specific Core Rule Set rules that cause false positives for
+ this app.
+ </span>
+ <div className="waf-panel__rules-input">
+ <Input
+ id={`waf-rule-${app.id}`}
+ type="number"
+ value={ruleInput}
+ onChange={(e) => setRuleInput(e.target.value)}
+ onKeyDown={handleRuleKeyDown}
+ placeholder="e.g. 942100"
+ disabled={loading}
+ />
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={handleAddRule}
+ disabled={loading || !ruleInput.trim()}
+ >
+ Add
+ </Button>
+ </div>
+ {policy.disabled_rule_ids.length > 0 ? (
+ <div className="waf-panel__chips">
+ {policy.disabled_rule_ids.map((id) => (
+ <span key={id} className="waf-panel__chip">
+ <span className="mono">{id}</span>
+ <button
+ type="button"
+ className="waf-panel__chip-remove"
+ onClick={() => handleRemoveRule(id)}
+ aria-label={`Remove rule ${id}`}
+ >
+ <X size={13} />
+ </button>
+ </span>
+ ))}
+ </div>
+ ) : (
+ <span className="container-ops__field-hint">No rules disabled.</span>
+ )}
+ </div>
+
+ <div className="app-detail-actions container-ops__actions">
+ <Button size="sm" onClick={handleSave} disabled={saving || loading}>
+ {saving ? 'Saving…' : 'Save policy'}
+ </Button>
+ <Button variant="outline" size="sm" onClick={handleApply} disabled={applying || loading}>
+ {applying ? 'Applying…' : 'Re-apply'}
+ </Button>
+ </div>
+ </div>
+ </div>
+
+ {/* Recent events */}
+ <div className="app-panel waf-panel__section">
+ <div className="app-panel-header">
+ <ListChecks />
+ <span>Recent events</span>
+ <span className="app-panel-header-actions">
+ <Button variant="ghost" size="sm" onClick={loadEvents} disabled={eventsLoading}>
+ <RefreshCw size={14} />
+ {eventsLoading ? 'Loading…' : 'Refresh'}
+ </Button>
+ </span>
+ </div>
+ <div className="app-panel-body">
+ {events.length > 0 ? (
+ <div className="waf-panel__table-wrap">
+ <table className="waf-panel__table">
+ <thead>
+ <tr>
+ <th>Rule ID</th>
+ <th>Severity</th>
+ <th>Message</th>
+ <th>URI</th>
+ <th>Client</th>
+ <th>Time</th>
+ </tr>
+ </thead>
+ <tbody>
+ {events.map((ev, i) => (
+ <tr key={ev.id ?? `${ev.rule_id ?? 'rule'}-${i}`}>
+ <td className="mono">{ev.rule_id ?? '—'}</td>
+ <td>
+ {ev.severity ? (
+ <Pill kind={severityKind(ev.severity)}>{ev.severity}</Pill>
+ ) : '—'}
+ </td>
+ <td className="waf-panel__msg">{ev.message || '—'}</td>
+ <td className="mono waf-panel__uri">{ev.uri || '—'}</td>
+ <td className="mono">{ev.client_ip || ev.client || '—'}</td>
+ <td className="waf-panel__time">
+ {ev.timestamp ? new Date(ev.timestamp).toLocaleString() : '—'}
+ </td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ ) : (
+ <p className="app-panel-hint">
+ {eventsLoading ? 'Loading events…' : 'No WAF events recorded.'}
+ </p>
+ )}
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default WafPanel;
diff --git a/frontend/src/pages/ApplicationDetail.jsx b/frontend/src/pages/ApplicationDetail.jsx
index 444c47e7..e835fd32 100644
--- a/frontend/src/pages/ApplicationDetail.jsx
+++ b/frontend/src/pages/ApplicationDetail.jsx
@@ -14,6 +14,7 @@ import PrivateURLSection from '../components/PrivateURLSection';
import LinkedAppsSection from '../components/LinkedAppsSection';
import LinkAppModal from '../components/LinkAppModal';
import ContainerOpsPanel from '../components/apps/ContainerOpsPanel';
+import WafPanel from '../components/apps/WafPanel';
import { getServiceType, getStatusConfig } from '../utils/serviceTypes';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
@@ -21,7 +22,7 @@ import { Textarea } from '@/components/ui/textarea';
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs';
import { Pill, EnvTag } from '@/components/ds';
-const VALID_TABS = ['overview', 'environment', 'packages', 'gunicorn', 'commands', 'ops', 'build', 'deploy', 'logs', 'settings'];
+const VALID_TABS = ['overview', 'environment', 'packages', 'gunicorn', 'commands', 'ops', 'waf', 'build', 'deploy', 'logs', 'settings'];
// statusInfo.dotClass → ds Pill kind
const STATUS_PILL = {
@@ -113,6 +114,9 @@ const ApplicationDetail = () => {
const isPythonApp = ['flask', 'django'].includes(app.app_type);
const isDockerApp = app.app_type === 'docker';
+ // WAF protects the nginx vhost in front of an app, so it applies to any
+ // nginx-served app type (Docker or Python) — broader than Container Ops.
+ const isNginxServedApp = isDockerApp || isPythonApp;
const isRunning = app.status === 'running';
const typeInfo = getServiceType(app.app_type);
const statusInfo = getStatusConfig(app.status);
@@ -213,6 +217,9 @@ const ApplicationDetail = () => {
{isDockerApp && (
<TabsTrigger value="ops">Container Ops</TabsTrigger>
)}
+ {isNginxServedApp && (
+ <TabsTrigger value="waf">WAF</TabsTrigger>
+ )}
<TabsTrigger value="build">Build</TabsTrigger>
<TabsTrigger value="deploy">Deploy</TabsTrigger>
<TabsTrigger value="logs">Logs</TabsTrigger>
@@ -245,6 +252,11 @@ const ApplicationDetail = () => {
<ContainerOpsPanel app={app} onChanged={loadApp} />
</TabsContent>
)}
+ {isNginxServedApp && (
+ <TabsContent value="waf">
+ <WafPanel app={app} onChanged={loadApp} />
+ </TabsContent>
+ )}
<TabsContent value="build">
<BuildTab appId={app.id} appPath={app.path} />
</TabsContent>
diff --git a/frontend/src/services/api/containerOps.js b/frontend/src/services/api/containerOps.js
index 46c901f2..cc042fa4 100644
--- a/frontend/src/services/api/containerOps.js
+++ b/frontend/src/services/api/containerOps.js
@@ -51,3 +51,28 @@ export async function evaluateScale(appId) {
export async function getGpuInfo() {
return this.request('/gpu/');
}
+
+// --- WAF (per-app ModSecurity) ---
+export async function getWafPolicy(appId) {
+ return this.request(`/waf/applications/${appId}/policy`);
+}
+
+export async function updateWafPolicy(appId, data) {
+ return this.request(`/waf/applications/${appId}/policy`, { method: 'PUT', body: data });
+}
+
+export async function applyWaf(appId) {
+ return this.request(`/waf/applications/${appId}/apply`, { method: 'POST' });
+}
+
+export async function getWafEvents(appId, limit = 50) {
+ return this.request(`/waf/applications/${appId}/events?limit=${limit}`);
+}
+
+export async function getWafStatus() {
+ return this.request('/waf/status');
+}
+
+export async function installWaf() {
+ return this.request('/waf/install', { method: 'POST' });
+}
diff --git a/frontend/src/styles/pages/_applications.scss b/frontend/src/styles/pages/_applications.scss
index a0de5a11..cb86abc1 100644
--- a/frontend/src/styles/pages/_applications.scss
+++ b/frontend/src/styles/pages/_applications.scss
@@ -1537,3 +1537,164 @@
}
}
}
+
+// ============================================
+// WAF PANEL (ModSecurity policy / events)
+// ============================================
+
+.waf-panel {
+ display: flex;
+ flex-direction: column;
+ gap: $spacing-lg;
+
+ &__section {
+ // reuses .app-panel shell; nothing extra needed by default
+ }
+
+ // install notice shown when ModSecurity is absent
+ &__banner {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: $spacing-md;
+ padding: $spacing-md;
+ border: 1px solid var(--border-soft);
+ border-radius: $radius-md;
+ background: var(--surface-2, var(--bg-card));
+ }
+
+ &__banner-text {
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+ min-width: 0;
+
+ span {
+ font-size: 12.5px;
+ color: var(--text-dim);
+ line-height: 1.4;
+ }
+ }
+
+ &__mode {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ margin-top: $space-3;
+
+ .sk-seg {
+ align-self: flex-start;
+ }
+ }
+
+ &__mode-hint {
+ font-size: 12.5px;
+ color: var(--text-dim);
+ line-height: 1.4;
+ }
+
+ &__rules {
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+ margin-top: $spacing-md;
+ padding-top: $spacing-md;
+ border-top: 1px solid var(--border-soft);
+ }
+
+ &__rules-input {
+ display: flex;
+ align-items: center;
+ gap: $spacing-sm;
+ margin-top: 4px;
+
+ .input,
+ input {
+ max-width: 200px;
+ }
+ }
+
+ &__chips {
+ display: flex;
+ flex-wrap: wrap;
+ gap: $spacing-sm;
+ margin-top: 8px;
+ }
+
+ &__chip {
+ display: inline-flex;
+ align-items: center;
+ gap: 6px;
+ padding: 3px 6px 3px 10px;
+ border: 1px solid var(--border-soft);
+ border-radius: $radius-full;
+ background: var(--surface-2, var(--bg-card));
+ font-size: 12.5px;
+ }
+
+ &__chip-remove {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ padding: 0;
+ border: none;
+ border-radius: 50%;
+ width: 18px;
+ height: 18px;
+ background: transparent;
+ color: var(--text-faint);
+ cursor: pointer;
+ transition: color 0.15s ease, background 0.15s ease;
+
+ &:hover {
+ color: var(--danger, #ef4444);
+ background: var(--border-soft);
+ }
+ }
+
+ &__table-wrap {
+ overflow-x: auto;
+ }
+
+ &__table {
+ width: 100%;
+ border-collapse: collapse;
+ font-size: 13px;
+
+ th,
+ td {
+ text-align: left;
+ padding: $space-3 $spacing-md;
+ border-bottom: 1px solid var(--border-soft);
+ vertical-align: top;
+ }
+
+ th {
+ font-size: 11.5px;
+ font-weight: 600;
+ text-transform: uppercase;
+ letter-spacing: 0.04em;
+ color: var(--text-faint);
+ }
+
+ tbody tr:last-child td {
+ border-bottom: none;
+ }
+ }
+
+ &__msg {
+ max-width: 320px;
+ }
+
+ &__uri {
+ max-width: 220px;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+
+ &__time {
+ white-space: nowrap;
+ color: var(--text-dim);
+ }
+}