@@ -38,10 +38,10 @@ var (
38
38
X509_ISSUER_KEY = "x509Issuer"
39
39
X509_SERIAL_KEY = "x509Serial"
40
40
41
- validCertSelectorKeys = map [ string ] struct {} {
42
- "x509Subject" : {} ,
43
- "x509Issuer" : {} ,
44
- "x509Serial" : {} ,
41
+ validCertSelectorKeys = [] string {
42
+ X509_SUBJECT_KEY ,
43
+ X509_ISSUER_KEY ,
44
+ X509_SERIAL_KEY ,
45
45
}
46
46
)
47
47
@@ -87,18 +87,24 @@ func getStringMap(s string) (map[string]string, error) {
87
87
}
88
88
key := strings .TrimSpace (strings .Join (keyTokens [1 :], "=" ))
89
89
90
+ isValidKey := false
91
+ for _ , validKey := range validCertSelectorKeys {
92
+ if validKey == key {
93
+ isValidKey = true
94
+ break
95
+ }
96
+ }
97
+ if ! isValidKey {
98
+ return nil , errors .New ("cert selector contained invalid key" )
99
+ }
100
+
90
101
valueTokens := strings .Split (tokens [1 ], "=" )
91
102
if valueTokens [0 ] != "Value" {
92
103
return nil , errors .New ("invalid cert selector map value" )
93
104
}
94
105
value := strings .TrimSpace (strings .Join (valueTokens [1 :], "=" ))
95
106
m [key ] = value
96
107
}
97
- for key := range m {
98
- if _ , ok := m [key ]; ! ok {
99
- return nil , errors .New ("invalid cert selector map key" )
100
- }
101
- }
102
108
103
109
return m , nil
104
110
}
@@ -112,6 +118,16 @@ func getMapFromJsonEntries(jsonStr string) (map[string]string, error) {
112
118
return nil , errors .New ("unable to parse JSON map entries" )
113
119
}
114
120
for _ , mapEntry := range mapEntries {
121
+ isValidKey := false
122
+ for _ , validKey := range validCertSelectorKeys {
123
+ if validKey == mapEntry .Key {
124
+ isValidKey = true
125
+ break
126
+ }
127
+ }
128
+ if ! isValidKey {
129
+ return nil , errors .New ("cert selector contained invalid key" )
130
+ }
115
131
m [mapEntry .Key ] = mapEntry .Value
116
132
}
117
133
return m , nil
@@ -146,16 +162,9 @@ func PopulateCertIdentifierFromJsonStr(jsonStr string) (helper.CertIdentifier, e
146
162
147
163
// Populates a CertIdentifier object using a cert selector string
148
164
func PopulateCertIdentifierFromCertSelectorStr (certSelectorStr string ) (helper.CertIdentifier , error ) {
149
- var certIdentifier helper.CertIdentifier
150
-
151
- err := json .Unmarshal ([]byte (certSelector ), & certIdentifier )
152
- certSelectorMap , err := getStringMap (certSelector )
165
+ certSelectorMap , err := getStringMap (certSelectorStr )
153
166
if err != nil {
154
- certSelectorMap , err = getMapFromJsonEntries (certSelector )
155
- if err != nil {
156
- msg := "unable to parse cert selector"
157
- return helper.CertIdentifier {}, errors .New (msg )
158
- }
167
+ return helper.CertIdentifier {}, err
159
168
}
160
169
161
170
return createCertSelectorFromMap (certSelectorMap ), nil
@@ -173,11 +182,14 @@ func PopulateCertIdentifier(certSelector string) (helper.CertIdentifier, error)
173
182
return helper.CertIdentifier {}, errors .New ("unable to read cert selector file" )
174
183
}
175
184
certIdentifier , err = PopulateCertIdentifierFromJsonStr (string (certSelectorFile [:]))
185
+ if err != nil {
186
+ return helper.CertIdentifier {}, errors .New ("unable to parse JSON cert selector" )
187
+ }
176
188
} else {
177
189
certIdentifier , err = PopulateCertIdentifierFromCertSelectorStr (certSelector )
178
- }
179
- if err != nil {
180
- return helper. CertIdentifier {}, errors . New ( "unable to read cert selector" )
190
+ if err != nil {
191
+ return helper. CertIdentifier {}, errors . New ( "unable to parse cert selector string" )
192
+ }
181
193
}
182
194
}
183
195
0 commit comments