File
Blob: ui/client/waf-api.ts
| 1 | import { RequestError, requestJSON } from '../shared/http'; |
| 2 | import { WAF_MODES, type WAFMode } from '../shared/waf'; |
| 3 | |
| 4 | export interface HostWAFPolicy { |
| 5 | mode: WAFMode; |
| 6 | exclusions: { rule_id: number; path: string }[]; |
| 7 | } |
| 8 | |
| 9 | export interface HostWAFSnapshot { |
| 10 | hostname: string; |
| 11 | version: string; |
| 12 | policy: HostWAFPolicy; |
| 13 | } |
| 14 | |
| 15 | export async function requestHostWAF(hostname: string, signal: AbortSignal, body?: string): Promise<HostWAFSnapshot> { |
| 16 | const data = await requestJSON<HostWAFSnapshot>(`/api/waf?hostname=${encodeURIComponent(hostname)}`, { |
| 17 | signal, body, method: body === undefined ? 'GET' : 'PUT', timeout: body === undefined ? 12_000 : 15_000, |
| 18 | }).catch((cause: unknown) => { |
| 19 | if (body !== undefined && cause instanceof RequestError && cause.status >= 500) { |
| 20 | throw new RequestError(cause.message, 'outcome_unknown', cause.data, cause.status); |
| 21 | } |
| 22 | throw cause; |
| 23 | }); |
| 24 | const exclusions = data?.policy?.exclusions; |
| 25 | if (typeof data?.hostname !== 'string' || typeof data.version !== 'string' |
| 26 | || !WAF_MODES.includes(data.policy?.mode) || !Array.isArray(exclusions) |
| 27 | || !exclusions.every(item => item && Number.isInteger(item.rule_id) && typeof item.path === 'string')) { |
| 28 | throw new RequestError('Unexpected server response.', body === undefined ? 'request_failed' : 'outcome_unknown'); |
| 29 | } |
| 30 | return data; |
| 31 | } |