Skip to content

Commit 2cdf668

Browse files
Add e2e tests
1 parent dd1dad8 commit 2cdf668

File tree

9 files changed

+223
-4
lines changed

9 files changed

+223
-4
lines changed

engine/engine.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package engine
33
import (
44
"crypto/sha1"
55
"fmt"
6-
"github.com/checkmarx/2ms/engine/linecontent"
7-
"github.com/checkmarx/2ms/engine/score"
86
"os"
97
"regexp"
108
"strings"
119
"sync"
1210
"text/tabwriter"
1311

12+
"github.com/checkmarx/2ms/engine/linecontent"
13+
"github.com/checkmarx/2ms/engine/score"
14+
1415
"github.com/checkmarx/2ms/engine/rules"
1516
"github.com/checkmarx/2ms/engine/validation"
1617
"github.com/checkmarx/2ms/lib/secrets"
@@ -85,6 +86,8 @@ func (e *Engine) Detect(item plugins.ISourceItem, secretsChannel chan *secrets.S
8586
Raw: *item.GetContent(),
8687
FilePath: item.GetSource(),
8788
}
89+
90+
fragment.Raw += "\n"
8891
for _, value := range e.detector.Detect(fragment) {
8992
itemId := getFindingId(item, value)
9093
var startLine, endLine int

tests/e2e.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path"
12+
"path/filepath"
1213
"runtime"
1314

1415
"github.com/checkmarx/2ms/lib/reporting"
@@ -20,7 +21,11 @@ type cli struct {
2021
}
2122

2223
func createCLI(outputDir string) (cli, error) {
23-
executable := path.Join(outputDir, "2ms")
24+
executableName := "2ms"
25+
if runtime.GOOS == "windows" {
26+
executableName += ".exe"
27+
}
28+
executable := filepath.Join(outputDir, executableName)
2429
lib, err := build.Import("github.com/checkmarx/2ms", "", build.FindOnly)
2530
if err != nil {
2631
return cli{}, fmt.Errorf("failed to import 2ms: %s", err)

tests/e2e_test.go

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package tests
22

3-
import "testing"
3+
import (
4+
"encoding/json"
5+
"os"
6+
"testing"
7+
8+
"github.com/checkmarx/2ms/lib/reporting"
9+
"github.com/google/go-cmp/cmp"
10+
)
411

512
func TestIntegration(t *testing.T) {
613
if testing.Short() {
@@ -79,3 +86,74 @@ func TestIntegration(t *testing.T) {
7986
}
8087
})
8188
}
89+
90+
func TestSecretsEdgeCases(t *testing.T) {
91+
if testing.Short() {
92+
t.Skip("skipping edge cases test")
93+
}
94+
95+
tests := []struct {
96+
Name string
97+
ScanTarget string
98+
TargetPath string
99+
ExpectedReportPath string
100+
}{
101+
{
102+
Name: "secret at end without newline (filesystem)",
103+
ScanTarget: "filesystem",
104+
TargetPath: "testData/input/secret_at_end.txt",
105+
ExpectedReportPath: "testData/expectedReport/report1.json",
106+
},
107+
{
108+
Name: "secret at end with multiLine (filesystem)",
109+
ScanTarget: "filesystem",
110+
TargetPath: "testData/input/multi_line_secret.txt",
111+
ExpectedReportPath: "testData/expectedReport/report2.json",
112+
},
113+
{
114+
Name: "secret at end with backspace in newline (filesystem)",
115+
ScanTarget: "filesystem",
116+
TargetPath: "testData/input/secret_at_end_with_newline.txt",
117+
ExpectedReportPath: "testData/expectedReport/report3.json",
118+
},
119+
}
120+
121+
for _, tc := range tests {
122+
t.Run(tc.Name, func(t *testing.T) {
123+
executable, err := createCLI(t.TempDir())
124+
if err != nil {
125+
t.Fatalf("failed to build CLI: %s", err)
126+
}
127+
128+
args := []string{tc.ScanTarget}
129+
if tc.ScanTarget == "filesystem" {
130+
args = append(args, "--path", tc.TargetPath)
131+
} else {
132+
args = append(args, tc.TargetPath)
133+
}
134+
args = append(args, "--ignore-on-exit", "results")
135+
136+
if err := executable.run(args[0], args[1:]...); err != nil {
137+
t.Fatalf("error running scan with args: %v, got: %v", args, err)
138+
}
139+
140+
actualReport, err := executable.getReport()
141+
if err != nil {
142+
t.Fatalf("failed to get report: %s", err)
143+
}
144+
145+
expectedBytes, err := os.ReadFile(tc.ExpectedReportPath)
146+
if err != nil {
147+
t.Fatalf("failed to read expected report: %s", err)
148+
}
149+
var expectedReport reporting.Report
150+
if err := json.Unmarshal(expectedBytes, &expectedReport); err != nil {
151+
t.Fatalf("failed to unmarshal expected report: %s", err)
152+
}
153+
154+
if !cmp.Equal(expectedReport, actualReport) {
155+
t.Errorf("Scan report does not match expected report:\n%s", cmp.Diff(expectedReport, actualReport))
156+
}
157+
})
158+
}
159+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"totalItemsScanned": 1,
3+
"totalSecretsFound": 2,
4+
"results": {
5+
"6a3e642795e27b989c54ac0c91147fe8e9a405b4": [
6+
{
7+
"id": "6a3e642795e27b989c54ac0c91147fe8e9a405b4",
8+
"source": "testData/input/secret_at_end.txt",
9+
"ruleId": "generic-api-key",
10+
"startLine": 2,
11+
"endLine": 2,
12+
"lineContent": "\n\t\t`\"client_secret\" : \"6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde\",`",
13+
"startColumn": 6,
14+
"endColumn": 88,
15+
"value": "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",
16+
"ruleDescription": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.",
17+
"cvssScore": 8.2
18+
}
19+
],
20+
"84bc054139c2363b37538209055a2d9c23026fab": [
21+
{
22+
"id": "84bc054139c2363b37538209055a2d9c23026fab",
23+
"source": "testData/input/secret_at_end.txt",
24+
"ruleId": "generic-api-key",
25+
"startLine": 1,
26+
"endLine": 1,
27+
"lineContent": "`\"client_id\" : \"0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506\"`,\r",
28+
"startColumn": 3,
29+
"endColumn": 81,
30+
"value": "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506",
31+
"ruleDescription": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.",
32+
"cvssScore": 8.2
33+
}
34+
]
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"totalItemsScanned": 1,
3+
"totalSecretsFound": 3,
4+
"results": {
5+
"1ef1c6a736cb0725175ac969776c3fe0b4602389": [
6+
{
7+
"id": "1ef1c6a736cb0725175ac969776c3fe0b4602389",
8+
"source": "testData/input/multi_line_secret.txt",
9+
"ruleId": "private-key",
10+
"startLine": 3,
11+
"endLine": 4,
12+
"lineContent": "\n -----BEGIN RSA PRIVATE KEY----- MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu KUpRKfFLfRYC9AIKjbJTWit+Cq\r\n vjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp79mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs /5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00 -----END RSA PRIVATE KEY-----\r",
13+
"startColumn": 10,
14+
"endColumn": 377,
15+
"value": "-----BEGIN RSA PRIVATE KEY----- MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu KUpRKfFLfRYC9AIKjbJTWit+Cq\r\n vjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp79mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs /5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00 -----END RSA PRIVATE KEY-----",
16+
"ruleDescription": "Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.",
17+
"cvssScore": 8.2
18+
}
19+
],
20+
"58e5a02e5571db6dc1f9c0fdba8d86e254225bf1": [
21+
{
22+
"id": "58e5a02e5571db6dc1f9c0fdba8d86e254225bf1",
23+
"source": "testData/input/multi_line_secret.txt",
24+
"ruleId": "generic-api-key",
25+
"startLine": 1,
26+
"endLine": 1,
27+
"lineContent": "`\"client_id\" : \"0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506\"`,\r",
28+
"startColumn": 3,
29+
"endColumn": 81,
30+
"value": "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506",
31+
"ruleDescription": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.",
32+
"cvssScore": 8.2
33+
}
34+
],
35+
"ed47a9a9052d119d91763ce84d689370fdbccf1f": [
36+
{
37+
"id": "ed47a9a9052d119d91763ce84d689370fdbccf1f",
38+
"source": "testData/input/multi_line_secret.txt",
39+
"ruleId": "generic-api-key",
40+
"startLine": 2,
41+
"endLine": 2,
42+
"lineContent": "\n\t\t`\"client_secret\" : \"6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde\",`\r",
43+
"startColumn": 6,
44+
"endColumn": 88,
45+
"value": "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",
46+
"ruleDescription": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.",
47+
"cvssScore": 8.2
48+
}
49+
]
50+
}
51+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"totalItemsScanned": 1,
3+
"totalSecretsFound": 2,
4+
"results": {
5+
"6af9b6df67e2971f45e6e27d4e068c2a515d2961": [
6+
{
7+
"id": "6af9b6df67e2971f45e6e27d4e068c2a515d2961",
8+
"source": "testData/input/secret_at_end_with_newline.txt",
9+
"ruleId": "generic-api-key",
10+
"startLine": 2,
11+
"endLine": 2,
12+
"lineContent": "\n\t\t`\"client_secret\" : \"6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde\",`\r",
13+
"startColumn": 6,
14+
"endColumn": 88,
15+
"value": "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",
16+
"ruleDescription": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.",
17+
"cvssScore": 8.2
18+
}
19+
],
20+
"f4b4bf79a4000811227225e3c556ea3862cfcb1a": [
21+
{
22+
"id": "f4b4bf79a4000811227225e3c556ea3862cfcb1a",
23+
"source": "testData/input/secret_at_end_with_newline.txt",
24+
"ruleId": "generic-api-key",
25+
"startLine": 1,
26+
"endLine": 1,
27+
"lineContent": "`\"client_id\" : \"0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506\"`,\r",
28+
"startColumn": 3,
29+
"endColumn": 81,
30+
"value": "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506",
31+
"ruleDescription": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.",
32+
"cvssScore": 8.2
33+
}
34+
]
35+
}
36+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
`"client_id" : "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506"`,
2+
`"client_secret" : "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",`
3+
-----BEGIN RSA PRIVATE KEY----- MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu KUpRKfFLfRYC9AIKjbJTWit+Cq
4+
vjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp79mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs /5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00 -----END RSA PRIVATE KEY-----
5+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`"client_id" : "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506"`,
2+
`"client_secret" : "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`"client_id" : "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506"`,
2+
`"client_secret" : "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",`
3+

0 commit comments

Comments
 (0)