Skip to content

Commit be60ba3

Browse files
fix(analyzer): add failed utf conversions to unwanted files (#7997)
* add bad utf conversions to unwanted in analyzer * add bad utf conversions to unwanted in analyzer * update e2e tests and uts to cover valid ansible samples * update base images --------- Co-authored-by: cx-miguel-silva <100352574+cx-miguel-silva@users.noreply.github.com>
1 parent f42f2ed commit be60ba3

8 files changed

Lines changed: 172 additions & 1 deletion

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ USER root
5050
ENV PATH $PATH:/app/bin
5151

5252
# Command to run the executable
53-
ENTRYPOINT ["/app/bin/kics"]
53+
ENTRYPOINT ["/app/bin/kics"]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"document": [
3+
{
4+
"file": "file",
5+
"id": "0",
6+
"playbooks": [
7+
{
8+
"community.aws.efs": {
9+
"encrypt": "no",
10+
"name": "myTestEFS",
11+
"state": "present",
12+
"tags": {
13+
"Name": "myTestNameTag",
14+
"purpose": "file-storage"
15+
},
16+
"targets": [
17+
{
18+
"security_groups": [
19+
"sg-1a2b3c4d"
20+
],
21+
"subnet_id": "subnet-748c5d03"
22+
}
23+
]
24+
},
25+
"name": "foo"
26+
},
27+
{
28+
"community.aws.efs": {
29+
"encrypt": false,
30+
"name": "myTestEFS",
31+
"state": "present",
32+
"tags": {
33+
"Name": "myTestNameTag",
34+
"purpose": "file-storage"
35+
},
36+
"targets": [
37+
{
38+
"security_groups": [
39+
"sg-1a2b3c4d"
40+
],
41+
"subnet_id": "subnet-748c5d03"
42+
}
43+
]
44+
},
45+
"name": "foo2"
46+
},
47+
{
48+
"community.aws.efs": {
49+
"name": "myTestEFS",
50+
"state": "present",
51+
"tags": {
52+
"Name": "myTestNameTag",
53+
"purpose": "file-storage"
54+
},
55+
"targets": [
56+
{
57+
"security_groups": [
58+
"sg-1a2b3c4d"
59+
],
60+
"subnet_id": "subnet-748c5d03"
61+
}
62+
]
63+
},
64+
"name": "foo3"
65+
}
66+
]
67+
}
68+
]
69+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testcases
2+
3+
// E2E-CLI-105 - KICS should ignore files with bad UTF-8 conversion and not include them in the scanned files
4+
func init() { //nolint
5+
testSample := TestCase{
6+
Name: "should ignore files with bad UTF-8 conversion and not include them in the scanned files [E2E-CLI-105]",
7+
Args: args{
8+
Args: []cmdArgs{
9+
[]string{
10+
"scan",
11+
"-p", "/path/test/fixtures/mix_utf8_and_non_utf/",
12+
"--payload-path", "/path/e2e/output/E2E_CLI_105_PAYLOAD",
13+
},
14+
},
15+
ExpectedPayload: []string{
16+
"E2E_CLI_105_PAYLOAD.json",
17+
},
18+
},
19+
WantStatus: []int{50},
20+
}
21+
22+
Tests = append(Tests, testSample)
23+
}

pkg/analyzer/analyzer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ func (a *analyzerInfo) checkContent(
537537
content, err := utils.ReadFileToUTF8(a.filePath)
538538
if err != nil {
539539
log.Warn().Msgf("failed to analyze file: %s", err)
540+
unwanted <- a.filePath
540541
return
541542
}
542543

pkg/analyzer/analyzer_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,23 @@ func TestAnalyzer_Analyze(t *testing.T) {
557557
excludeGitIgnore: false,
558558
MaxFileSize: -1,
559559
},
560+
{
561+
name: "analyze_test_non_utf8_is_set_as_unwanted",
562+
paths: []string{
563+
filepath.FromSlash("../../test/fixtures/mix_utf8_and_non_utf"),
564+
},
565+
wantTypes: []string{"ansible"},
566+
wantExclude: []string{
567+
filepath.FromSlash("../../test/fixtures/mix_utf8_and_non_utf/non_utf.json"),
568+
},
569+
typesFromFlag: []string{""},
570+
excludeTypesFromFlag: []string{""},
571+
wantLOC: 57,
572+
wantErr: false,
573+
gitIgnoreFileName: "",
574+
excludeGitIgnore: false,
575+
MaxFileSize: -1,
576+
},
560577
}
561578

562579
for _, tt := range tests {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
server {
2+
listen 80;
3+
server_name example.com;
4+
5+
location / {
6+
root /var/www/html;
7+
index index.html index.htm;
8+
}
9+
10+
location /api {
11+
proxy_pass http://backend:8080;
12+
proxy_set_header Host $host;
13+
proxy_set_header X-Real-IP $remote_addr;
14+
}
15+
16+
# Security headers
17+
add_header X-Frame-Options "SAMEORIGIN" always;
18+
add_header X-Content-Type-Options "nosniff" always;
19+
add_header X-XSS-Protection "1; mode=block" always;
20+
21+
error_page 404 /404.html;
22+
error_page 500 502 503 504 /50x.html;
23+
}
24+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Invalid UTF-8 ÿþ test",
3+
"data": "€‚"
4+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
- name: foo
3+
community.aws.efs:
4+
state: present
5+
name: myTestEFS
6+
encrypt: no
7+
tags:
8+
Name: myTestNameTag
9+
purpose: file-storage
10+
targets:
11+
- subnet_id: subnet-748c5d03
12+
security_groups: ["sg-1a2b3c4d"]
13+
- name: foo2
14+
community.aws.efs:
15+
state: present
16+
name: myTestEFS
17+
encrypt: false
18+
tags:
19+
Name: myTestNameTag
20+
purpose: file-storage
21+
targets:
22+
- subnet_id: subnet-748c5d03
23+
security_groups: ["sg-1a2b3c4d"]
24+
- name: foo3
25+
community.aws.efs:
26+
state: present
27+
name: myTestEFS
28+
tags:
29+
Name: myTestNameTag
30+
purpose: file-storage
31+
targets:
32+
- subnet_id: subnet-748c5d03
33+
security_groups: ["sg-1a2b3c4d"]

0 commit comments

Comments
 (0)