1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 |
|
7 | * Update a zone ruleset |
8 | * Updates a zone ruleset, creating a new version. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | ruleset_id: string, |
13 | zone_id: string, |
14 | body: { |
15 | description?: string; |
16 | id: string & { [k: string]: unknown }; |
17 | kind?: "managed" | "custom" | "root" | "zone"; |
18 | last_updated: string; |
19 | name?: string; |
20 | phase?: |
21 | | "ddos_l4" |
22 | | "ddos_l7" |
23 | | "http_config_settings" |
24 | | "http_custom_errors" |
25 | | "http_log_custom_fields" |
26 | | "http_ratelimit" |
27 | | "http_request_cache_settings" |
28 | | "http_request_dynamic_redirect" |
29 | | "http_request_firewall_custom" |
30 | | "http_request_firewall_managed" |
31 | | "http_request_late_transform" |
32 | | "http_request_origin" |
33 | | "http_request_redirect" |
34 | | "http_request_sanitize" |
35 | | "http_request_sbfm" |
36 | | "http_request_select_configuration" |
37 | | "http_request_transform" |
38 | | "http_response_compression" |
39 | | "http_response_firewall_managed" |
40 | | "http_response_headers_transform" |
41 | | "magic_transit" |
42 | | "magic_transit_ids_managed" |
43 | | "magic_transit_managed"; |
44 | version: string; |
45 | [k: string]: unknown; |
46 | } & { |
47 | rules: ( |
48 | | ({ |
49 | action?: string; |
50 | action_parameters?: { [k: string]: unknown }; |
51 | categories?: string[]; |
52 | description?: string; |
53 | enabled?: boolean & boolean; |
54 | expression?: string; |
55 | id?: string; |
56 | last_updated: string; |
57 | logging?: { enabled: boolean; [k: string]: unknown }; |
58 | ref?: string; |
59 | version: string; |
60 | [k: string]: unknown; |
61 | } & { |
62 | action?: "block"; |
63 | action_parameters?: { |
64 | response?: { |
65 | content: string; |
66 | content_type: string; |
67 | status_code: number; |
68 | [k: string]: unknown; |
69 | }; |
70 | [k: string]: unknown; |
71 | }; |
72 | description?: { [k: string]: unknown }; |
73 | [k: string]: unknown; |
74 | }) |
75 | | ({ |
76 | action?: string; |
77 | action_parameters?: { [k: string]: unknown }; |
78 | categories?: string[]; |
79 | description?: string; |
80 | enabled?: boolean & boolean; |
81 | expression?: string; |
82 | id?: string; |
83 | last_updated: string; |
84 | logging?: { enabled: boolean; [k: string]: unknown }; |
85 | ref?: string; |
86 | version: string; |
87 | [k: string]: unknown; |
88 | } & { |
89 | action?: "execute"; |
90 | action_parameters?: { |
91 | id: string & { [k: string]: unknown }; |
92 | matched_data?: { public_key: string; [k: string]: unknown }; |
93 | overrides?: { |
94 | action?: string & { [k: string]: unknown }; |
95 | categories?: { |
96 | action?: string & { [k: string]: unknown }; |
97 | category: string & { [k: string]: unknown }; |
98 | enabled?: boolean & { [k: string]: unknown }; |
99 | sensitivity_level?: |
100 | | "default" |
101 | | "medium" |
102 | | "low" |
103 | | ("eoff" & { [k: string]: unknown }); |
104 | [k: string]: unknown; |
105 | }[]; |
106 | enabled?: boolean & { [k: string]: unknown }; |
107 | rules?: { |
108 | action?: string & { [k: string]: unknown }; |
109 | enabled?: boolean & { [k: string]: unknown }; |
110 | id: string & { [k: string]: unknown }; |
111 | score_threshold?: number; |
112 | sensitivity_level?: |
113 | | "default" |
114 | | "medium" |
115 | | "low" |
116 | | ("eoff" & { [k: string]: unknown }); |
117 | [k: string]: unknown; |
118 | }[]; |
119 | sensitivity_level?: |
120 | | "default" |
121 | | "medium" |
122 | | "low" |
123 | | ("eoff" & { [k: string]: unknown }); |
124 | [k: string]: unknown; |
125 | }; |
126 | [k: string]: unknown; |
127 | }; |
128 | description?: { [k: string]: unknown }; |
129 | [k: string]: unknown; |
130 | }) |
131 | | ({ |
132 | action?: string; |
133 | action_parameters?: { [k: string]: unknown }; |
134 | categories?: string[]; |
135 | description?: string; |
136 | enabled?: boolean & boolean; |
137 | expression?: string; |
138 | id?: string; |
139 | last_updated: string; |
140 | logging?: { enabled: boolean; [k: string]: unknown }; |
141 | ref?: string; |
142 | version: string; |
143 | [k: string]: unknown; |
144 | } & { |
145 | action?: "log"; |
146 | action_parameters?: {}; |
147 | description?: { [k: string]: unknown }; |
148 | [k: string]: unknown; |
149 | }) |
150 | | ({ |
151 | action?: string; |
152 | action_parameters?: { [k: string]: unknown }; |
153 | categories?: string[]; |
154 | description?: string; |
155 | enabled?: boolean & boolean; |
156 | expression?: string; |
157 | id?: string; |
158 | last_updated: string; |
159 | logging?: { enabled: boolean; [k: string]: unknown }; |
160 | ref?: string; |
161 | version: string; |
162 | [k: string]: unknown; |
163 | } & { |
164 | action?: "skip"; |
165 | action_parameters?: { |
166 | phases?: ( |
167 | | "ddos_l4" |
168 | | "ddos_l7" |
169 | | "http_config_settings" |
170 | | "http_custom_errors" |
171 | | "http_log_custom_fields" |
172 | | "http_ratelimit" |
173 | | "http_request_cache_settings" |
174 | | "http_request_dynamic_redirect" |
175 | | "http_request_firewall_custom" |
176 | | "http_request_firewall_managed" |
177 | | "http_request_late_transform" |
178 | | "http_request_origin" |
179 | | "http_request_redirect" |
180 | | "http_request_sanitize" |
181 | | "http_request_sbfm" |
182 | | "http_request_select_configuration" |
183 | | "http_request_transform" |
184 | | "http_response_compression" |
185 | | "http_response_firewall_managed" |
186 | | "http_response_headers_transform" |
187 | | "magic_transit" |
188 | | "magic_transit_ids_managed" |
189 | | ("magic_transit_managed" & { [k: string]: unknown }) |
190 | )[]; |
191 | products?: |
192 | | "bic" |
193 | | "hot" |
194 | | "rateLimit" |
195 | | "securityLevel" |
196 | | "uaBlock" |
197 | | "waf" |
198 | | "zoneLockdown"[]; |
199 | rules?: { [k: string]: (string & { [k: string]: unknown })[] }; |
200 | ruleset?: "current"; |
201 | rulesets?: (string & { [k: string]: unknown })[]; |
202 | [k: string]: unknown; |
203 | }; |
204 | description?: { [k: string]: unknown }; |
205 | [k: string]: unknown; |
206 | }) |
207 | )[]; |
208 | [k: string]: unknown; |
209 | } |
210 | ) { |
211 | const url = new URL( |
212 | `https://api.cloudflare.com/client/v4/zones/${zone_id}/rulesets/${ruleset_id}` |
213 | ); |
214 |
|
215 | const response = await fetch(url, { |
216 | method: "PUT", |
217 | headers: { |
218 | "X-AUTH-EMAIL": auth.email, |
219 | "X-AUTH-KEY": auth.key, |
220 | "Content-Type": "application/json", |
221 | Authorization: "Bearer " + auth.token, |
222 | }, |
223 | body: JSON.stringify(body), |
224 | }); |
225 | if (!response.ok) { |
226 | const text = await response.text(); |
227 | throw new Error(`${response.status} ${text}`); |
228 | } |
229 | return await response.json(); |
230 | } |
231 |
|