-
Notifications
You must be signed in to change notification settings - Fork 8
/
format.go
255 lines (196 loc) · 5.66 KB
/
format.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
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
package conflate
import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
"encoding/xml"
"errors"
"fmt"
"regexp"
"strings"
"github.com/xeipuuv/gojsonschema"
"golang.org/x/net/html"
)
var (
errRequiredString = errors.New("the value is not a string")
errUnsupportedType = errors.New("called with unsupported type")
)
func initFormatCheckers() {
// annoyingly the format checker list is a global variable
gojsonschema.FormatCheckers.Add(newXMLFormatChecker("xml"))
gojsonschema.FormatCheckers.Add(newXMLTemplateFormatChecker("xml-template"))
gojsonschema.FormatCheckers.Add(newHTMLFormatChecker("html-template"))
gojsonschema.FormatCheckers.Add(newRegexFormatChecker("regex"))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs1-private-key", pkcs1PrivateKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs1-public-key", pkcs1PublicKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs8-private-key", pkcs8PrivateKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs8-public-key", pkixPublicKey)) // deprecated, use pkix-public-key
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkix-public-key", pkixPublicKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("x509-certificate", x509Certificate))
}
// ----------------
type formatErrors map[string]error
var formatErrs = formatErrors{}
func (errs formatErrors) clear() {
formatErrs = formatErrors{}
}
func (errs formatErrors) add(name, value interface{}, err error) {
errs[errs.key(name, value)] = err
}
func (errs formatErrors) get(name, value interface{}) error {
return errs[errs.key(name, value)]
}
func (errs formatErrors) key(name, value interface{}) string {
return fmt.Sprintf("%v#%v", name, value)
}
// ----------------
type xmlFormatChecker struct{ name string }
//nolint:unparam // left for extensibility
func newXMLFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, xmlFormatChecker{name: name}
}
func (f xmlFormatChecker) IsFormat(input interface{}) bool {
var ferr error
if s, ok := input.(string); ok {
if err := xml.Unmarshal([]byte(s), new(interface{})); err != nil {
ferr = fmt.Errorf("failed to parse xml: %w", err)
}
} else {
ferr = errRequiredString
}
if ferr != nil {
formatErrs.add(f.name, input, ferr)
return false
}
return true
}
// ----------------
type xmlTemplateFormatChecker struct {
tags *regexp.Regexp
name string
}
func newXMLTemplateFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, xmlTemplateFormatChecker{name: name, tags: regexp.MustCompile(`{{[^{}]*}}`)}
}
func (f xmlTemplateFormatChecker) IsFormat(input interface{}) bool {
var ferr error
if s, ok := input.(string); ok {
s = f.tags.ReplaceAllString(s, "")
if s != "" {
var v interface{}
if err := xml.Unmarshal([]byte(s), &v); err != nil {
ferr = fmt.Errorf("failed to parse xml: %w", err)
}
}
} else {
ferr = errRequiredString
}
if ferr != nil {
formatErrs.add(f.name, input, ferr)
return false
}
return true
}
// ----------------
type htmlFormatChecker struct {
tags *regexp.Regexp
name string
}
func newHTMLFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, htmlFormatChecker{name: name, tags: regexp.MustCompile(`{{[^{}]*}}`)}
}
func (f htmlFormatChecker) IsFormat(input interface{}) bool {
var ferr error
if s, ok := input.(string); ok {
s = f.tags.ReplaceAllString(s, "")
if _, err := html.Parse(strings.NewReader(s)); err != nil {
ferr = fmt.Errorf("failed to parse html: %w", err)
}
} else {
ferr = errRequiredString
}
if ferr != nil {
formatErrs.add(f.name, input, ferr)
return false
}
return true
}
// ----------------
type cryptoFormatChecker struct {
name string
cType cryptoType
}
type cryptoType int
const (
pkcs1PrivateKey cryptoType = 1 + iota
pkcs1PublicKey
pkcs8PrivateKey
pkixPublicKey
x509Certificate
)
func newCryptoFormatChecker(name string, cType cryptoType) (string, gojsonschema.FormatChecker) {
return name, cryptoFormatChecker{
name: name,
cType: cType,
}
}
func (f cryptoFormatChecker) IsFormat(input interface{}) bool {
s, ok := input.(string)
if !ok {
formatErrs.add(f.name, input, errRequiredString)
return false
}
var err error
var data []byte
block, _ := pem.Decode([]byte(s))
if block != nil {
data = block.Bytes
} else {
// Try to directly base64 decode if not valid PEM
data, err = base64.StdEncoding.DecodeString(s)
if err != nil {
formatErrs.add(f.name, input, fmt.Errorf("failed to decode the data: %w", err))
return false
}
}
switch f.cType {
case pkcs1PrivateKey:
_, err = x509.ParsePKCS1PrivateKey(data)
case pkcs1PublicKey:
_, err = x509.ParsePKCS1PublicKey(data)
case pkcs8PrivateKey:
_, err = x509.ParsePKCS8PrivateKey(data)
case pkixPublicKey:
_, err = x509.ParsePKIXPublicKey(data)
case x509Certificate:
_, err = x509.ParseCertificate(data)
default:
err = fmt.Errorf("%v %w", f.name, errUnsupportedType)
}
if err != nil {
formatErrs.add(f.name, input, fmt.Errorf("failed to parse key: %w", err))
return false
}
return true
}
// ----------------
type regexFormatChecker struct{ name string }
//nolint:unparam // left for extensibility
func newRegexFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, regexFormatChecker{name: name}
}
func (f regexFormatChecker) IsFormat(input interface{}) bool {
var ferr error
if s, ok := input.(string); ok {
if _, err := regexp.Compile(s); err != nil {
ferr = fmt.Errorf("failed to parse regular expression: %w", err)
}
} else {
ferr = errRequiredString
}
if ferr != nil {
formatErrs.add(f.name, input, ferr)
return false
}
return true
}