forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
384 lines (337 loc) · 9.23 KB
/
util.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
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
package growthbook
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
)
// Returns an array of floats with numVariations items that are all
// equal and sum to 1.
func getEqualWeights(numVariations int) []float64 {
if numVariations < 0 {
numVariations = 0
}
equal := make([]float64, numVariations)
for i := range equal {
equal[i] = 1.0 / float64(numVariations)
}
return equal
}
// Checks if an experiment variation is being forced via a URL query
// string.
//
// As an example, if the id is "my-test" and url is
// http://localhost/?my-test=1, this function returns 1.
func getQueryStringOverride(id string, url *url.URL, numVariations int) *int {
v, ok := url.Query()[id]
if !ok || len(v) > 1 {
return nil
}
vi, err := strconv.Atoi(v[0])
if err != nil {
return nil
}
if vi < 0 || vi >= numVariations {
return nil
}
return &vi
}
func decrypt(encrypted string, encKey string) (string, error) {
key, err := base64.StdEncoding.DecodeString(encKey)
if err != nil {
return "", err
}
splits := strings.Split(encrypted, ".")
if len(splits) != 2 {
return "", errors.New("invalid format for key")
}
iv, err := base64.StdEncoding.DecodeString(splits[0])
if err != nil {
return "", err
}
cipherText, err := base64.StdEncoding.DecodeString(splits[1])
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
if len(iv) != block.BlockSize() {
return "", errors.New("invalid IV length")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(cipherText, cipherText)
cipherText, err = unpad(cipherText)
if err != nil {
return "", err
}
return string(cipherText), nil
}
// Remove PKCS #7 padding.
func unpad(buf []byte) ([]byte, error) {
bufLen := len(buf)
if bufLen == 0 {
return nil, errors.New("crypto/padding: invalid padding size")
}
pad := buf[bufLen-1]
if pad == 0 {
return nil, errors.New("crypto/padding: invalid last byte of padding")
}
padLen := int(pad)
if padLen > bufLen || padLen > 16 {
return nil, errors.New("crypto/padding: invalid padding size")
}
for _, v := range buf[bufLen-padLen : bufLen-1] {
if v != pad {
return nil, errors.New("crypto/padding: invalid padding")
}
}
return buf[:bufLen-padLen], nil
}
// This function imitates Javascript's "truthiness" evaluation for Go
// values of unknown type.
func truthy(v interface{}) bool {
if v == nil {
return false
}
switch v.(type) {
case string:
return v.(string) != ""
case bool:
return v.(bool)
case int:
return v.(int) != 0
case uint:
return v.(uint) != 0
case float32:
return v.(float32) != 0
case float64:
return v.(float64) != 0
}
return true
}
// This function converts slices of concrete types to []interface{}.
// This is needed to handle the common case where a user passes an
// attribute as a []string (or []int), and this needs to be compared
// against feature data deserialized from JSON, which always results
// in []interface{} slices.
func fixSliceTypes(vin interface{}) interface{} {
// Convert all type-specific slices to interface{} slices.
v := reflect.ValueOf(vin)
rv := vin
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
srv := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
elem := v.Index(i).Interface()
srv[i] = elem
}
rv = srv
}
return rv
}
func isURLTargeted(url *url.URL, targets []URLTarget) bool {
if len(targets) == 0 {
return false
}
hasIncludeRules := false
isIncluded := false
for _, t := range targets {
match := evalURLTarget(url, t.Type, t.Pattern)
if !t.Include {
if match {
return false
}
} else {
hasIncludeRules = true
if match {
isIncluded = true
}
}
}
return isIncluded || !hasIncludeRules
}
func evalURLTarget(url *url.URL, typ URLTargetType, pattern string) bool {
if typ == RegexURLTarget {
regex := getURLRegexp(pattern)
if regex == nil {
return false
}
return regex.MatchString(url.String()) || regex.MatchString(url.Path)
} else if typ == SimpleURLTarget {
return evalSimpleURLTarget(url, pattern)
}
return false
}
type comp struct {
actual string
expected string
isPath bool
}
func evalSimpleURLTarget(actual *url.URL, pattern string) bool {
// If a protocol is missing, but a host is specified, add `https://`
// to the front. Use "_____" as the wildcard since `*` is not a valid
// hostname in some browsers
schemeRe := regexp.MustCompile(`(?i)^([^:/?]*)\.`)
pattern = schemeRe.ReplaceAllString(pattern, "https://$1.")
wildcardRe := regexp.MustCompile(`\*`)
pattern = wildcardRe.ReplaceAllLiteralString(pattern, "_____")
expected, err := url.Parse(pattern)
if err != nil {
logError("Failed to parse URL pattern: ", pattern)
return false
}
if expected.Host == "" {
expected.Host = "_____"
}
// Compare each part of the URL separately
comps := []comp{
{actual.Host, expected.Host, false},
{actual.Path, expected.Path, true},
}
// We only want to compare hashes if it's explicitly being targeted
if expected.Fragment != "" {
comps = append(comps, comp{actual.Fragment, expected.Fragment, false})
}
actualParams, err := url.ParseQuery(actual.RawQuery)
if err != nil {
logError("Failed to parse actual URL query parameters: ", actual.RawQuery)
return false
}
expectedParams, err := url.ParseQuery(expected.RawQuery)
if err != nil {
logError("Failed to parse expected URL query parameters: ", expected.RawQuery)
return false
}
for param, expectedValue := range expectedParams {
actualValue := ""
if actualParams.Has(param) {
actualValue = actualParams[param][0]
}
comps = append(comps, comp{actualValue, expectedValue[0], false})
}
// If any comparisons fail, the whole thing fails
for _, comp := range comps {
if !evalSimpleURLPart(comp.actual, comp.expected, comp.isPath) {
return false
}
}
return true
}
func evalSimpleURLPart(actual string, pattern string, isPath bool) bool {
// Escape special regex characters.
specialRe := regexp.MustCompile(`([*.+?^${}()|[\]\\])`)
escaped := specialRe.ReplaceAllString(pattern, "\\$1")
escaped = strings.Replace(escaped, "_____", ".*", -1)
if isPath {
// When matching pathname, make leading/trailing slashes optional
slashRe := regexp.MustCompile(`(^\/|\/$)`)
escaped = slashRe.ReplaceAllLiteralString(escaped, "")
escaped = "\\/?" + escaped + "\\/?"
}
escaped = "(?i)^" + escaped + "$"
regex, err := regexp.Compile(escaped)
if err != nil {
logError("Failed to compile regexp: ", escaped)
return false
}
return regex.MatchString(actual)
}
func getURLRegexp(regexString string) *regexp.Regexp {
retval, err := regexp.Compile(regexString)
if err == nil {
return retval
}
logError("Failed to compile URL regexp:", err)
return nil
}
func jsonString(v interface{}, typeName string, fieldName string) (string, bool) {
tmp, ok := v.(string)
if ok {
return tmp, true
}
logError("Invalid JSON data type", typeName, fieldName)
return "", false
}
func jsonBool(v interface{}, typeName string, fieldName string) (bool, bool) {
tmp, ok := v.(bool)
if ok {
return tmp, true
}
logError("Invalid JSON data type", typeName, fieldName)
return false, false
}
func jsonInt(v interface{}, typeName string, fieldName string) (int, bool) {
tmp, ok := v.(float64)
if ok {
return int(tmp), true
}
logError("Invalid JSON data type", typeName, fieldName)
return 0, false
}
func jsonFloat(v interface{}, typeName string, fieldName string) (float64, bool) {
tmp, ok := v.(float64)
if ok {
return tmp, true
}
logError("Invalid JSON data type", typeName, fieldName)
return 0.0, false
}
func jsonMaybeFloat(v interface{}, typeName string, fieldName string) (*float64, bool) {
tmp, ok := v.(float64)
if ok {
return &tmp, true
}
logError("Invalid JSON data type", typeName, fieldName)
return nil, false
}
func jsonFloatArray(v interface{}, typeName string, fieldName string) ([]float64, bool) {
vals, ok := v.([]interface{})
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil, false
}
fvals := make([]float64, len(vals))
for i := range vals {
tmp, ok := vals[i].(float64)
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil, false
}
fvals[i] = tmp
}
return fvals, true
}
var (
versionStripRe = regexp.MustCompile(`(^v|\+.*$)`)
versionSplitRe = regexp.MustCompile(`[-.]`)
versionNumRe = regexp.MustCompile(`^[0-9]+$`)
)
func paddedVersionString(input string) string {
// Remove build info and leading `v` if any
// Split version into parts (both core version numbers and pre-release tags)
// "v1.2.3-rc.1+build123" -> ["1","2","3","rc","1"]
stripped := versionStripRe.ReplaceAllLiteralString(input, "")
parts := versionSplitRe.Split(stripped, -1)
// If it's SemVer without a pre-release, add `~` to the end
// ["1","0","0"] -> ["1","0","0","~"]
// "~" is the largest ASCII character, so this will make "1.0.0"
// greater than "1.0.0-beta" for example
if len(parts) == 3 {
parts = append(parts, "~")
}
// Left pad each numeric part with spaces so string comparisons will
// work ("9">"10", but " 9"<"10")
for i := range parts {
if versionNumRe.MatchString(parts[i]) {
parts[i] = strings.Repeat(" ", 5-len(parts[i])) + parts[i]
}
}
// Then, join back together into a single string
return strings.Join(parts, "-")
}