Skip to content

Commit 46be3df

Browse files
authored
OASIS-8551 fix(attribute-validation): Adds validation on user attributes to only allow primitive types. (#339)
* fixing attribute validation. * headers updated.
1 parent b7ff265 commit 46be3df

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/event/factory.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019-2020, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020,2022 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -243,7 +243,7 @@ func getEventAttributes(projectConfig config.ProjectConfig, attributes map[strin
243243
var eventAttributes = []VisitorAttribute{}
244244

245245
for key, value := range attributes {
246-
if value == nil {
246+
if !isValidAttribute(value) {
247247
continue
248248
}
249249
visitorAttribute := VisitorAttribute{}
@@ -291,3 +291,23 @@ func getTagValue(eventTags map[string]interface{}) (float64, error) {
291291

292292
return 0, errors.New("no event tag found for value")
293293
}
294+
295+
// check if attribute value is valid
296+
func isValidAttribute(value interface{}) bool {
297+
if value == nil {
298+
return false
299+
}
300+
if _, ok := value.(string); ok {
301+
return true
302+
}
303+
if _, ok := value.(float64); ok {
304+
return true
305+
}
306+
if _, ok := value.(int); ok {
307+
return true
308+
}
309+
if _, ok := value.(bool); ok {
310+
return true
311+
}
312+
return false
313+
}

pkg/event/factory_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019,2022 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -234,3 +234,15 @@ func TestCreateImpressionUserEvent(t *testing.T) {
234234
}
235235
}
236236
}
237+
238+
func TestIsValidAttribute(t *testing.T) {
239+
assert.False(t, isValidAttribute(nil))
240+
assert.False(t, isValidAttribute(map[string]interface{}{}))
241+
assert.False(t, isValidAttribute([]string{}))
242+
assert.False(t, isValidAttribute([]interface{}{}))
243+
assert.False(t, isValidAttribute(make(chan int)))
244+
assert.True(t, isValidAttribute("123"))
245+
assert.True(t, isValidAttribute(1.11))
246+
assert.True(t, isValidAttribute(1))
247+
assert.True(t, isValidAttribute(true))
248+
}

0 commit comments

Comments
 (0)