Skip to content

Commit 676a7e6

Browse files
committed
initial func to test paths
1 parent 832366b commit 676a7e6

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

scans_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ func TestXrayBinaryScanWithBypassArchiveLimits(t *testing.T) {
117117

118118
// Docker scan tests
119119

120+
func TestValidatePathFunc(t *testing.T) {
121+
// ValidatePathFunc should return true for a valid path
122+
validations.TestValidatePathsFunc(t)
123+
}
124+
120125
func TestDockerScanWithProgressBar(t *testing.T) {
121126
callback := commonTests.MockProgressInitialization()
122127
defer callback()

utils/validations/test_validate_sca.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,187 @@ func ValidateScanResponses(t *testing.T, exactMatch bool, expected, actual []ser
9797
}
9898
}
9999

100+
func TestValidatePathsFunc(t *testing.T) {
101+
//sampleJson := `{
102+
// "targets": [
103+
//{
104+
// "target": "/path/to/target",
105+
// "name": "some-target-name",
106+
// "technology": "some-technology",
107+
// "sca_scans": {
108+
// "xray_scan": [
109+
//{
110+
sampleJson := `{
111+
"scan_id": "1a97d1a4-4d30-430c-46e9-d0c998065d08",
112+
"vulnerabilities": [
113+
{
114+
"cves": [
115+
{
116+
"cve": "CVE-2024-51744",
117+
"cvss_v3_score": "3.1",
118+
"cvss_v3_vector": "CVSS:3.1/AV:N/AC:H/PR:N/UI:R/S:U/C:L/I:N/A:N",
119+
"cwe": [
120+
"CWE-347",
121+
"CWE-755"
122+
],
123+
"cwe_details": {
124+
"CWE-347": {
125+
"name": "Improper Verification of Cryptographic Signature",
126+
"description": "The product does not verify, or incorrectly verifies, the cryptographic signature for data."
127+
},
128+
"CWE-755": {
129+
"name": "Improper Handling of Exceptional Conditions",
130+
"description": "The product does not handle or incorrectly handles an exceptional condition."
131+
}
132+
}
133+
}
134+
],
135+
"summary": "golang-jwt is a Go implementation of JSON Web Tokens. Unclear documentation of the error behavior in \"ParseWithClaims\" can lead to situation where users are potentially not checking errors in the way they should be. Especially, if a token is both expired and invalid, the errors returned by \"ParseWithClaims\" return both error codes. If users only check for the \"jwt.ErrTokenExpired\" using \"error.Is\", they will ignore the embedded \"jwt.ErrTokenSignatureInvalid\" and thus potentially accept invalid tokens. A fix has been back-ported with the error handling logic from the \"v5\" branch to the \"v4\" branch. In this logic, the \"ParseWithClaims\" function will immediately return in \"dangerous\" situations (e.g., an invalid signature), limiting the combined errors only to situations where the signature is valid, but further validation failed (e.g., if the signature is valid, but is expired AND has the wrong audience). This fix is part of the 4.5.1 release. We are aware that this changes the behaviour of an established function and is not 100 % backwards compatible, so updating to 4.5.1 might break your code. In case you cannot update to 4.5.0, please make sure that you are properly checking for all errors (\"dangerous\" ones first), so that you are not running in the case detailed above.",
136+
"severity": "Low",
137+
"components": {
138+
"go://github.com/golang-jwt/jwt/v4:4.4.2": {
139+
"fixed_versions": [
140+
"[4.5.1]"
141+
],
142+
"impact_paths": [
143+
[
144+
{
145+
"component_id": "docker://localhost:8046/cli-docker-virtual-1734626392/bitnami/minio:2022"
146+
},
147+
{
148+
"component_id": "generic://sha256:2252918a526208e7f0b752c923f267b14f1c8beece250ee6bc72c9c2ef7bb1c6/sha256__2252918a526208e7f0b752c923f267b14f1c8beece250ee6bc72c9c2ef7bb1c6.tar",
149+
"full_path": "sha256__2252918a526208e7f0b752c923f267b14f1c8beece250ee6bc72c9c2ef7bb1c6.tar"
150+
}
151+
]
152+
]
153+
}
154+
}
155+
}
156+
]
157+
}`
158+
// ]
159+
// }
160+
// }
161+
// ]
162+
//}`
163+
actualJson := services.ScanResponse{}
164+
err := json.Unmarshal([]byte(sampleJson), &actualJson)
165+
if err != nil {
166+
assert.NoError(t, err)
167+
}
168+
stringsToCheck := []string{"vulnerabilities[].components[*].impact_paths[][].full_path"}
169+
ValidatePaths(t, actualJson, stringsToCheck)
170+
//ValidateContainedStrings(t, sampleJson, []string{"impact_paths", "full_path"})
171+
}
172+
173+
func ValidatePaths(t *testing.T, output interface{}, paths []string) {
174+
for _, path := range paths {
175+
elements := strings.Split(path, ".")
176+
assert.True(t, validatePath(output, elements), "path does not exist: %s", path)
177+
}
178+
}
179+
180+
func validatePath(data interface{}, path []string) bool {
181+
if len(path) == 0 {
182+
return true
183+
}
184+
185+
key := path[0]
186+
sliceKey := strings.Replace(key, "[]", "", -1)
187+
mapKey := strings.Replace(key, "[*]", "", -1)
188+
189+
if key == "[]" {
190+
// Top-level is an array
191+
if reflect.TypeOf(data).Kind() == reflect.Slice {
192+
slice := reflect.ValueOf(data)
193+
for i := 0; i < slice.Len(); i++ {
194+
if validatePath(slice.Index(i).Interface(), path[1:]) {
195+
return true
196+
}
197+
}
198+
}
199+
} else if strings.Contains(key, "[]") {
200+
// Handle array notation in the middle
201+
if reflect.TypeOf(data).Kind() == reflect.Map {
202+
dataMap := reflect.ValueOf(data)
203+
if val := dataMap.MapIndex(reflect.ValueOf(sliceKey)); val.IsValid() {
204+
if val.Kind() == reflect.Slice {
205+
slice := val
206+
for i := 0; i < slice.Len(); i++ {
207+
if validatePath(slice.Index(i).Interface(), path[1:]) {
208+
return true
209+
}
210+
}
211+
}
212+
}
213+
} else if reflect.TypeOf(data).Kind() == reflect.Struct {
214+
structField, valid := getFieldByTag(data, sliceKey)
215+
if valid && structField.Kind() == reflect.Slice {
216+
slice := structField
217+
for i := 0; i < slice.Len(); i++ {
218+
if validatePath(slice.Index(i).Interface(), path[1:]) {
219+
return true
220+
}
221+
}
222+
}
223+
}
224+
} else if strings.Contains(key, "[*]") {
225+
// Handle any map key
226+
if reflect.TypeOf(data).Kind() == reflect.Map {
227+
dataMap := reflect.ValueOf(data)
228+
if val := dataMap.MapIndex(reflect.ValueOf(mapKey)); val.IsValid() {
229+
if val.Kind() == reflect.Map {
230+
for _, item := range val.Interface().(map[string]interface{}) {
231+
if validatePath(item, path[1:]) {
232+
return true
233+
}
234+
}
235+
}
236+
}
237+
} else if reflect.TypeOf(data).Kind() == reflect.Struct {
238+
structField, valid := getFieldByTag(data, mapKey)
239+
if valid && structField.Kind() == reflect.Map {
240+
for _, item := range reflect.ValueOf(structField.Interface()).MapKeys() {
241+
if validatePath(item, path[1:]) {
242+
return true
243+
}
244+
}
245+
}
246+
}
247+
} else {
248+
// Handle standard keys
249+
if reflect.TypeOf(data).Kind() == reflect.Map {
250+
dataMap := reflect.ValueOf(data)
251+
if val := dataMap.MapIndex(reflect.ValueOf(key)); val.IsValid() {
252+
return validatePath(val.Interface(), path[1:])
253+
}
254+
} else if reflect.TypeOf(data).Kind() == reflect.Struct {
255+
structField, valid := getFieldByTag(data, key)
256+
if valid {
257+
return validatePath(structField.Interface(), path[1:])
258+
}
259+
}
260+
}
261+
return false
262+
}
263+
264+
func getFieldByTag(data interface{}, key string) (reflect.Value, bool) {
265+
structValue := reflect.ValueOf(data)
266+
structType := structValue.Type()
267+
268+
for i := 0; i < structValue.NumField(); i++ {
269+
field := structType.Field(i)
270+
tag := field.Tag.Get("json")
271+
272+
tagParts := strings.Split(tag, ",")
273+
if tagParts[0] == key {
274+
return structValue.Field(i), true
275+
}
276+
}
277+
278+
return reflect.Value{}, false
279+
}
280+
100281
func getScanResponseByScanId(scanId string, content []services.ScanResponse) *services.ScanResponse {
101282
for _, result := range content {
102283
if result.ScanId == scanId {

0 commit comments

Comments
 (0)