/
schema.go
76 lines (64 loc) · 2.15 KB
/
schema.go
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
package models
import (
"encoding/base64"
"errors"
"fmt"
"path/filepath"
"strings"
)
const (
FileDataFilenameFormat = "%s;$$:$$;"
FileDataFormat = "data:application/octet-stream;base64,%s"
)
var (
ErrorInvalidDataFormat = errors.New("invalid data format")
)
type SchemaAdvancedSetting struct {
ID string `json:"id"`
IllegalHttpMethods bool `json:"illegal_http_methods,omitempty"`
BodySize int `json:"body_size,omitempty"`
URLSize int `json:"url_size,omitempty"`
HeaderSize int `json:"header_size,omitempty"`
MaxObjectDepth int `json:"max_object_depth,omitempty"`
}
type SchemaAPIAttacks struct {
ID string `json:"id"`
MinimumSeverity string `json:"minimum_severity"`
AdvancedSetting []SchemaAdvancedSetting `json:"advanced_setting"`
}
type SchemaIPS struct {
ID string `json:"id"`
PerformanceImpact string `json:"performance_impact"`
SeverityLevel string `json:"severity_level"`
ProtectionsFromYear string `json:"protections_from_year"`
HighConfidence string `json:"high_confidence"`
MediumConfidence string `json:"medium_confidence"`
LowConfidence string `json:"low_confidence"`
}
type FileSchema struct {
ID string `json:"id,omitempty"`
Filename string `json:"filename,omitempty"`
Data string `json:"data"`
}
func NewFileSchemaEncode(filename, fileData string) FileSchema {
b64Data := base64.StdEncoding.EncodeToString([]byte(fileData))
data := fmt.Sprintf(FileDataFormat, b64Data)
filenameFmt := fmt.Sprintf(FileDataFilenameFormat, filepath.Base(filename))
return FileSchema{
Filename: filename,
Data: filenameFmt + data,
}
}
func NewFileSchemaDecode(filename, b64Data string) (*FileSchema, error) {
if _, bEncodedData, found := strings.Cut(b64Data, "base64,"); found {
bDecodedData, err := base64.StdEncoding.DecodeString(bEncodedData)
if err != nil {
return nil, fmt.Errorf("failed decoding base64 string %s: %w", bEncodedData, err)
}
return &FileSchema{
Filename: filename,
Data: string(bDecodedData),
}, nil
}
return nil, ErrorInvalidDataFormat
}