Repository navigation
Expand file tree
/
Copy pathscript.go
More file actions
448 lines (409 loc) · 17.2 KB
/
Copy pathscript.go
File metadata and controls
448 lines (409 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package page_shield
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/cloudflare/cloudflare-go/v7/internal/apijson"
"github.com/cloudflare/cloudflare-go/v7/internal/apiquery"
"github.com/cloudflare/cloudflare-go/v7/internal/param"
"github.com/cloudflare/cloudflare-go/v7/internal/requestconfig"
"github.com/cloudflare/cloudflare-go/v7/option"
"github.com/cloudflare/cloudflare-go/v7/packages/pagination"
"github.com/cloudflare/cloudflare-go/v7/shared"
)
// ScriptService contains methods and other services that help with interacting
// with the cloudflare API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewScriptService] method instead.
type ScriptService struct {
Options []option.RequestOption
}
// NewScriptService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewScriptService(opts ...option.RequestOption) (r *ScriptService) {
r = &ScriptService{}
r.Options = opts
return
}
// Lists scripts detected on webpages in the zone, with filtering and pagination.
func (r *ScriptService) List(ctx context.Context, params ScriptListParams, opts ...option.RequestOption) (res *pagination.SinglePage[ScriptListResponse], err error) {
var raw *http.Response
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if params.ZoneID.Value == "" {
err = errors.New("missing required zone_id parameter")
return nil, err
}
path := fmt.Sprintf("zones/%s/page_shield/scripts", params.ZoneID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// Lists scripts detected on webpages in the zone, with filtering and pagination.
func (r *ScriptService) ListAutoPaging(ctx context.Context, params ScriptListParams, opts ...option.RequestOption) *pagination.SinglePageAutoPager[ScriptListResponse] {
return pagination.NewSinglePageAutoPager(r.List(ctx, params, opts...))
}
// Returns a script detected on the zone by script ID.
func (r *ScriptService) Get(ctx context.Context, scriptID string, query ScriptGetParams, opts ...option.RequestOption) (res *ScriptGetResponse, err error) {
var env ScriptGetResponseEnvelope
opts = slices.Concat(r.Options, opts)
if query.ZoneID.Value == "" {
err = errors.New("missing required zone_id parameter")
return nil, err
}
if scriptID == "" {
err = errors.New("missing required script_id parameter")
return nil, err
}
path := fmt.Sprintf("zones/%s/page_shield/scripts/%s", query.ZoneID, scriptID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &env, opts...)
if err != nil {
return nil, err
}
res = &env.Result
return res, nil
}
type ScriptListResponse struct {
// Identifier
ID string `json:"id" api:"required"`
AddedAt time.Time `json:"added_at" api:"required" format:"date-time"`
FirstSeenAt time.Time `json:"first_seen_at" api:"required" format:"date-time"`
Host string `json:"host" api:"required"`
LastSeenAt time.Time `json:"last_seen_at" api:"required" format:"date-time"`
URL string `json:"url" api:"required"`
URLContainsCDNCGIPath bool `json:"url_contains_cdn_cgi_path" api:"required"`
// The cryptomining score of the JavaScript content.
CryptominingScore int64 `json:"cryptomining_score" api:"nullable"`
// The dataflow score of the JavaScript content. This field has been deprecated in
// favour of js_integrity_score.
//
// Deprecated: deprecated
DataflowScore int64 `json:"dataflow_score" api:"nullable"`
DomainReportedMalicious bool `json:"domain_reported_malicious"`
// The timestamp of when the script was last fetched.
FetchedAt string `json:"fetched_at" api:"nullable"`
FirstPageURL string `json:"first_page_url"`
// The computed hash of the analyzed script.
Hash string `json:"hash" api:"nullable"`
// The integrity score of the JavaScript content.
JSIntegrityScore int64 `json:"js_integrity_score" api:"nullable"`
// The magecart score of the JavaScript content.
MagecartScore int64 `json:"magecart_score" api:"nullable"`
MaliciousDomainCategories []string `json:"malicious_domain_categories"`
MaliciousURLCategories []string `json:"malicious_url_categories"`
// The malware score of the JavaScript content.
MalwareScore int64 `json:"malware_score" api:"nullable"`
// The obfuscation score of the JavaScript content. This field has been deprecated
// in favour of js_integrity_score.
//
// Deprecated: deprecated
ObfuscationScore int64 `json:"obfuscation_score" api:"nullable"`
PageURLs []string `json:"page_urls"`
URLReportedMalicious bool `json:"url_reported_malicious"`
JSON scriptListResponseJSON `json:"-"`
}
// scriptListResponseJSON contains the JSON metadata for the struct
// [ScriptListResponse]
type scriptListResponseJSON struct {
ID apijson.Field
AddedAt apijson.Field
FirstSeenAt apijson.Field
Host apijson.Field
LastSeenAt apijson.Field
URL apijson.Field
URLContainsCDNCGIPath apijson.Field
CryptominingScore apijson.Field
DataflowScore apijson.Field
DomainReportedMalicious apijson.Field
FetchedAt apijson.Field
FirstPageURL apijson.Field
Hash apijson.Field
JSIntegrityScore apijson.Field
MagecartScore apijson.Field
MaliciousDomainCategories apijson.Field
MaliciousURLCategories apijson.Field
MalwareScore apijson.Field
ObfuscationScore apijson.Field
PageURLs apijson.Field
URLReportedMalicious apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ScriptListResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r scriptListResponseJSON) RawJSON() string {
return r.raw
}
type ScriptGetResponse struct {
// Identifier
ID string `json:"id" api:"required"`
AddedAt time.Time `json:"added_at" api:"required" format:"date-time"`
FirstSeenAt time.Time `json:"first_seen_at" api:"required" format:"date-time"`
Host string `json:"host" api:"required"`
LastSeenAt time.Time `json:"last_seen_at" api:"required" format:"date-time"`
URL string `json:"url" api:"required"`
URLContainsCDNCGIPath bool `json:"url_contains_cdn_cgi_path" api:"required"`
// The cryptomining score of the JavaScript content.
CryptominingScore int64 `json:"cryptomining_score" api:"nullable"`
// The dataflow score of the JavaScript content. This field has been deprecated in
// favour of js_integrity_score.
//
// Deprecated: deprecated
DataflowScore int64 `json:"dataflow_score" api:"nullable"`
DomainReportedMalicious bool `json:"domain_reported_malicious"`
// The timestamp of when the script was last fetched.
FetchedAt string `json:"fetched_at" api:"nullable"`
FirstPageURL string `json:"first_page_url"`
// The computed hash of the analyzed script.
Hash string `json:"hash" api:"nullable"`
// The integrity score of the JavaScript content.
JSIntegrityScore int64 `json:"js_integrity_score" api:"nullable"`
// The magecart score of the JavaScript content.
MagecartScore int64 `json:"magecart_score" api:"nullable"`
MaliciousDomainCategories []string `json:"malicious_domain_categories"`
MaliciousURLCategories []string `json:"malicious_url_categories"`
// The malware score of the JavaScript content.
MalwareScore int64 `json:"malware_score" api:"nullable"`
// The obfuscation score of the JavaScript content. This field has been deprecated
// in favour of js_integrity_score.
//
// Deprecated: deprecated
ObfuscationScore int64 `json:"obfuscation_score" api:"nullable"`
PageURLs []string `json:"page_urls"`
URLReportedMalicious bool `json:"url_reported_malicious"`
Versions []ScriptGetResponseVersion `json:"versions" api:"nullable"`
JSON scriptGetResponseJSON `json:"-"`
}
// scriptGetResponseJSON contains the JSON metadata for the struct
// [ScriptGetResponse]
type scriptGetResponseJSON struct {
ID apijson.Field
AddedAt apijson.Field
FirstSeenAt apijson.Field
Host apijson.Field
LastSeenAt apijson.Field
URL apijson.Field
URLContainsCDNCGIPath apijson.Field
CryptominingScore apijson.Field
DataflowScore apijson.Field
DomainReportedMalicious apijson.Field
FetchedAt apijson.Field
FirstPageURL apijson.Field
Hash apijson.Field
JSIntegrityScore apijson.Field
MagecartScore apijson.Field
MaliciousDomainCategories apijson.Field
MaliciousURLCategories apijson.Field
MalwareScore apijson.Field
ObfuscationScore apijson.Field
PageURLs apijson.Field
URLReportedMalicious apijson.Field
Versions apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ScriptGetResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r scriptGetResponseJSON) RawJSON() string {
return r.raw
}
// The version of the analyzed script.
type ScriptGetResponseVersion struct {
// The cryptomining score of the JavaScript content.
CryptominingScore int64 `json:"cryptomining_score" api:"nullable"`
// The dataflow score of the JavaScript content. This field has been deprecated in
// favour of js_integrity_score.
//
// Deprecated: deprecated
DataflowScore int64 `json:"dataflow_score" api:"nullable"`
// The timestamp of when the script was last fetched.
FetchedAt string `json:"fetched_at" api:"nullable"`
// The computed hash of the analyzed script.
Hash string `json:"hash" api:"nullable"`
// The integrity score of the JavaScript content.
JSIntegrityScore int64 `json:"js_integrity_score" api:"nullable"`
// The magecart score of the JavaScript content.
MagecartScore int64 `json:"magecart_score" api:"nullable"`
// The malware score of the JavaScript content.
MalwareScore int64 `json:"malware_score" api:"nullable"`
// The obfuscation score of the JavaScript content. This field has been deprecated
// in favour of js_integrity_score.
//
// Deprecated: deprecated
ObfuscationScore int64 `json:"obfuscation_score" api:"nullable"`
JSON scriptGetResponseVersionJSON `json:"-"`
}
// scriptGetResponseVersionJSON contains the JSON metadata for the struct
// [ScriptGetResponseVersion]
type scriptGetResponseVersionJSON struct {
CryptominingScore apijson.Field
DataflowScore apijson.Field
FetchedAt apijson.Field
Hash apijson.Field
JSIntegrityScore apijson.Field
MagecartScore apijson.Field
MalwareScore apijson.Field
ObfuscationScore apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ScriptGetResponseVersion) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r scriptGetResponseVersionJSON) RawJSON() string {
return r.raw
}
type ScriptListParams struct {
// Identifier
ZoneID param.Field[string] `path:"zone_id" api:"required"`
// The direction used to sort returned scripts.
Direction param.Field[ScriptListParamsDirection] `query:"direction"`
// When true, excludes scripts seen in a `/cdn-cgi` path from the returned scripts.
// The default value is true.
ExcludeCDNCGI param.Field[bool] `query:"exclude_cdn_cgi"`
// When true, excludes duplicate scripts. We consider a script duplicate of another
// if their javascript content matches and they share the same url host and zone
// hostname. In such case, we return the most recent script for the URL host and
// zone hostname combination.
ExcludeDuplicates param.Field[bool] `query:"exclude_duplicates"`
// Excludes scripts whose URL contains one of the URL-encoded URLs separated by
// commas.
ExcludeURLs param.Field[string] `query:"exclude_urls"`
// Export the list of scripts as a file, limited to 50000 entries.
Export param.Field[ScriptListParamsExport] `query:"export"`
// Includes scripts that match one or more URL-encoded hostnames separated by
// commas.
//
// Wildcards are supported at the start and end of each hostname to support starts
// with, ends with and contains. If no wildcards are used, results will be filtered
// by exact match
Hosts param.Field[string] `query:"hosts"`
// The field used to sort returned scripts.
OrderBy param.Field[ScriptListParamsOrderBy] `query:"order_by"`
// The current page number of the paginated results.
//
// We additionally support a special value "all". When "all" is used, the API will
// return all the scripts with the applied filters in a single page. This feature
// is best-effort and it may only work for zones with a low number of scripts
Page param.Field[string] `query:"page"`
// Includes scripts that match one or more page URLs (separated by commas) where
// they were last seen
//
// Wildcards are supported at the start and end of each page URL to support starts
// with, ends with and contains. If no wildcards are used, results will be filtered
// by exact match
PageURL param.Field[string] `query:"page_url"`
// The number of results per page.
PerPage param.Field[float64] `query:"per_page"`
// When true, malicious scripts appear first in the returned scripts.
PrioritizeMalicious param.Field[bool] `query:"prioritize_malicious"`
// Filters the returned scripts using a comma-separated list of scripts statuses.
// Accepted values: `active`, `infrequent`, and `inactive`. The default value is
// `active`.
Status param.Field[string] `query:"status"`
// Includes scripts whose URL contain one or more URL-encoded URLs separated by
// commas.
URLs param.Field[string] `query:"urls"`
}
// URLQuery serializes [ScriptListParams]'s query parameters as `url.Values`.
func (r ScriptListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatRepeat,
NestedFormat: apiquery.NestedQueryFormatDots,
})
}
// The direction used to sort returned scripts.
type ScriptListParamsDirection string
const (
ScriptListParamsDirectionAsc ScriptListParamsDirection = "asc"
ScriptListParamsDirectionDesc ScriptListParamsDirection = "desc"
)
func (r ScriptListParamsDirection) IsKnown() bool {
switch r {
case ScriptListParamsDirectionAsc, ScriptListParamsDirectionDesc:
return true
}
return false
}
// Export the list of scripts as a file, limited to 50000 entries.
type ScriptListParamsExport string
const (
ScriptListParamsExportCsv ScriptListParamsExport = "csv"
)
func (r ScriptListParamsExport) IsKnown() bool {
switch r {
case ScriptListParamsExportCsv:
return true
}
return false
}
// The field used to sort returned scripts.
type ScriptListParamsOrderBy string
const (
ScriptListParamsOrderByFirstSeenAt ScriptListParamsOrderBy = "first_seen_at"
ScriptListParamsOrderByLastSeenAt ScriptListParamsOrderBy = "last_seen_at"
)
func (r ScriptListParamsOrderBy) IsKnown() bool {
switch r {
case ScriptListParamsOrderByFirstSeenAt, ScriptListParamsOrderByLastSeenAt:
return true
}
return false
}
type ScriptGetParams struct {
// Identifier
ZoneID param.Field[string] `path:"zone_id" api:"required"`
}
type ScriptGetResponseEnvelope struct {
Result ScriptGetResponse `json:"result" api:"required,nullable"`
// Whether the API call was successful
Success ScriptGetResponseEnvelopeSuccess `json:"success" api:"required"`
Errors []shared.ResponseInfo `json:"errors"`
Messages []shared.ResponseInfo `json:"messages"`
JSON scriptGetResponseEnvelopeJSON `json:"-"`
}
// scriptGetResponseEnvelopeJSON contains the JSON metadata for the struct
// [ScriptGetResponseEnvelope]
type scriptGetResponseEnvelopeJSON struct {
Result apijson.Field
Success apijson.Field
Errors apijson.Field
Messages apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ScriptGetResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r scriptGetResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type ScriptGetResponseEnvelopeSuccess bool
const (
ScriptGetResponseEnvelopeSuccessTrue ScriptGetResponseEnvelopeSuccess = true
)
func (r ScriptGetResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case ScriptGetResponseEnvelopeSuccessTrue:
return true
}
return false
}

