Skip to content

Commit 5b84a1e

Browse files
Merge pull request #993 from Checkmarx/fix/Github-Cloud-Url-Pattern
Fix the GitHub Cloud URL pattern to account for additional supported repository URLs.
2 parents 6025a3f + 04bf81a commit 5b84a1e

File tree

2 files changed

+244
-28
lines changed

2 files changed

+244
-28
lines changed

internal/commands/scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ func getSCSEnginesSelected(scsEngines string) (isScorecardSelected, isSecretDete
978978

979979
func isURLSupportedByScorecard(scsRepoURL string) bool {
980980
// only for https; currently our scorecard solution doesn't support GitHub Enterprise Server hosts
981-
githubURLPattern := regexp.MustCompile(`^(?:https?://)?github\.com/.+`)
981+
githubURLPattern := regexp.MustCompile(`(?:https?://)?(?:^|[^.])github\.com/.+`)
982982
isGithubURL := githubURLPattern.MatchString(scsRepoURL)
983983
if scsRepoURL != "" && !isGithubURL {
984984
fmt.Println(ScsScorecardUnsupportedHostWarningMsg)

internal/commands/scan_test.go

Lines changed: 243 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,37 @@ import (
2929
)
3030

3131
const (
32-
unknownFlag = "unknown flag: --chibutero"
33-
blankSpace = " "
34-
errorMissingBranch = "Failed creating a scan: Please provide a branch"
35-
dummyGitlabRepo = "https://gitlab.com/dummy-org/gitlab-dummy"
36-
dummyRepo = "https://github.com/dummyuser/dummy_project.git"
37-
dummyShortenedGithubRepo = "github.com/dummyuser/dummy_project.git"
38-
dummyToken = "dummyToken"
39-
dummySSHRepo = "git@github.com:dummyRepo/dummyProject.git"
40-
errorSourceBadFormat = "Failed creating a scan: Input in bad format: Sources input has bad format: "
41-
scaPathError = "ScaResolver error: exec: \"resolver\": executable file not found in "
42-
fileSourceFlag = "--file"
43-
fileSourceValueEmpty = "data/empty.Dockerfile"
44-
fileSourceValue = "data/Dockerfile"
45-
fileSourceIncorrectValue = "data/source.zip"
46-
fileSourceIncorrectValueError = "data/source.zip. Provided file is not supported by kics"
47-
fileSourceError = "flag needs an argument: --file"
48-
engineFlag = "--engine"
49-
engineValue = "docker"
50-
invalidEngineValue = "invalidengine"
51-
engineError = "flag needs an argument: --engine"
52-
additionalParamsFlag = "--additional-params"
53-
additionalParamsValue = "-v"
54-
additionalParamsError = "flag needs an argument: --additional-params"
55-
scanCommand = "scan"
56-
kicsRealtimeCommand = "kics-realtime"
57-
InvalidEngineMessage = "Please verify if engine is installed"
58-
SCSScoreCardError = "SCS scan failed to start: Scorecard scan is missing required flags, please include in the ast-cli arguments: " +
32+
unknownFlag = "unknown flag: --chibutero"
33+
blankSpace = " "
34+
errorMissingBranch = "Failed creating a scan: Please provide a branch"
35+
dummyGitlabRepo = "https://gitlab.com/dummy-org/gitlab-dummy"
36+
dummyRepo = "https://github.com/dummyuser/dummy_project.git"
37+
dummyRepoWithToken = "https://token@github.com/dummyuser/dummy_project"
38+
dummyRepoWithTokenAndUsername = "https://username:token@github.com/dummyuser/dummy_project"
39+
dummyShortenedRepoWithToken = "token@github.com/dummyuser/dummy_project"
40+
dummyShortenedRepoWithTokenAndUsername = "username:token@github.com/dummyuser/dummy_project"
41+
dummyShortenedGithubRepo = "github.com/dummyuser/dummy_project.git"
42+
dummyToken = "dummyToken"
43+
dummySSHRepo = "git@github.com:dummyRepo/dummyProject.git"
44+
errorSourceBadFormat = "Failed creating a scan: Input in bad format: Sources input has bad format: "
45+
scaPathError = "ScaResolver error: exec: \"resolver\": executable file not found in "
46+
fileSourceFlag = "--file"
47+
fileSourceValueEmpty = "data/empty.Dockerfile"
48+
fileSourceValue = "data/Dockerfile"
49+
fileSourceIncorrectValue = "data/source.zip"
50+
fileSourceIncorrectValueError = "data/source.zip. Provided file is not supported by kics"
51+
fileSourceError = "flag needs an argument: --file"
52+
engineFlag = "--engine"
53+
engineValue = "docker"
54+
invalidEngineValue = "invalidengine"
55+
engineError = "flag needs an argument: --engine"
56+
additionalParamsFlag = "--additional-params"
57+
additionalParamsValue = "-v"
58+
additionalParamsError = "flag needs an argument: --additional-params"
59+
scanCommand = "scan"
60+
kicsRealtimeCommand = "kics-realtime"
61+
InvalidEngineMessage = "Please verify if engine is installed"
62+
SCSScoreCardError = "SCS scan failed to start: Scorecard scan is missing required flags, please include in the ast-cli arguments: " +
5963
"--scs-repo-url your_repo_url --scs-repo-token your_repo_token"
6064
outputFileName = "test_output.log"
6165
noUpdatesForExistingProject = "No tags to update. Skipping project update."
@@ -1090,6 +1094,218 @@ func TestCreateScan_WithSCSSecretDetectionAndScorecardShortenedGithubRepo_scsMap
10901094
}
10911095
}
10921096

1097+
func TestCreateScan_WithSCSSecretDetectionAndScorecardShortenedGithubRepoWithTokenInURL_scsMapHasBoth(t *testing.T) {
1098+
// Create a pipe for capturing stdout
1099+
r, w, _ := os.Pipe()
1100+
oldStdout := os.Stdout
1101+
defer func() { os.Stdout = oldStdout }()
1102+
os.Stdout = w // Redirecting stdout to the pipe
1103+
1104+
var resubmitConfig []wrappers.Config
1105+
cmdCommand := &cobra.Command{
1106+
Use: "scan",
1107+
Short: "Scan a project",
1108+
Long: `Scan a project`,
1109+
}
1110+
cmdCommand.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "SCS Engine flag")
1111+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "GitHub token to be used with SCS engines")
1112+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "GitHub url to be used with SCS engines")
1113+
_ = cmdCommand.Execute()
1114+
_ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection,scorecard")
1115+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken)
1116+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyShortenedRepoWithToken)
1117+
1118+
result, _ := addSCSScan(cmdCommand, resubmitConfig, true)
1119+
1120+
// Close the writer to signal that we are done capturing the output
1121+
w.Close()
1122+
1123+
// Read from the pipe (stdout)
1124+
var buf bytes.Buffer
1125+
_, err := io.Copy(&buf, r) // Copy the captured output to a buffer
1126+
if err != nil {
1127+
t.Fatalf("Failed to capture output: %v", err)
1128+
}
1129+
1130+
output := buf.String()
1131+
if strings.Contains(output, ScsScorecardUnsupportedHostWarningMsg) {
1132+
t.Errorf("Expected output to not contain %q, but got %q", ScsScorecardUnsupportedHostWarningMsg, output)
1133+
}
1134+
1135+
scsConfig := wrappers.SCSConfig{
1136+
Twoms: "true",
1137+
Scorecard: "true",
1138+
RepoURL: dummyShortenedRepoWithToken,
1139+
RepoToken: dummyToken,
1140+
}
1141+
scsMapConfig := make(map[string]interface{})
1142+
scsMapConfig[resultsMapType] = commonParams.MicroEnginesType
1143+
scsMapConfig[resultsMapValue] = &scsConfig
1144+
1145+
if !reflect.DeepEqual(result, scsMapConfig) {
1146+
t.Errorf("Expected %+v, but got %+v", scsMapConfig, result)
1147+
}
1148+
}
1149+
1150+
func TestCreateScan_WithSCSSecretDetectionAndScorecardGithubRepoWithTokenInURL_scsMapHasBoth(t *testing.T) {
1151+
// Create a pipe for capturing stdout
1152+
r, w, _ := os.Pipe()
1153+
oldStdout := os.Stdout
1154+
defer func() { os.Stdout = oldStdout }()
1155+
os.Stdout = w // Redirecting stdout to the pipe
1156+
1157+
var resubmitConfig []wrappers.Config
1158+
cmdCommand := &cobra.Command{
1159+
Use: "scan",
1160+
Short: "Scan a project",
1161+
Long: `Scan a project`,
1162+
}
1163+
cmdCommand.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "SCS Engine flag")
1164+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "GitHub token to be used with SCS engines")
1165+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "GitHub url to be used with SCS engines")
1166+
_ = cmdCommand.Execute()
1167+
_ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection,scorecard")
1168+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken)
1169+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyRepoWithToken)
1170+
1171+
result, _ := addSCSScan(cmdCommand, resubmitConfig, true)
1172+
1173+
// Close the writer to signal that we are done capturing the output
1174+
w.Close()
1175+
1176+
// Read from the pipe (stdout)
1177+
var buf bytes.Buffer
1178+
_, err := io.Copy(&buf, r) // Copy the captured output to a buffer
1179+
if err != nil {
1180+
t.Fatalf("Failed to capture output: %v", err)
1181+
}
1182+
1183+
output := buf.String()
1184+
if strings.Contains(output, ScsScorecardUnsupportedHostWarningMsg) {
1185+
t.Errorf("Expected output to not contain %q, but got %q", ScsScorecardUnsupportedHostWarningMsg, output)
1186+
}
1187+
1188+
scsConfig := wrappers.SCSConfig{
1189+
Twoms: "true",
1190+
Scorecard: "true",
1191+
RepoURL: dummyRepoWithToken,
1192+
RepoToken: dummyToken,
1193+
}
1194+
scsMapConfig := make(map[string]interface{})
1195+
scsMapConfig[resultsMapType] = commonParams.MicroEnginesType
1196+
scsMapConfig[resultsMapValue] = &scsConfig
1197+
1198+
if !reflect.DeepEqual(result, scsMapConfig) {
1199+
t.Errorf("Expected %+v, but got %+v", scsMapConfig, result)
1200+
}
1201+
}
1202+
1203+
func TestCreateScan_WithSCSSecretDetectionAndScorecardGithubRepoWithTokenAndUsernameInURL_scsMapHasBoth(t *testing.T) {
1204+
// Create a pipe for capturing stdout
1205+
r, w, _ := os.Pipe()
1206+
oldStdout := os.Stdout
1207+
defer func() { os.Stdout = oldStdout }()
1208+
os.Stdout = w // Redirecting stdout to the pipe
1209+
1210+
var resubmitConfig []wrappers.Config
1211+
cmdCommand := &cobra.Command{
1212+
Use: "scan",
1213+
Short: "Scan a project",
1214+
Long: `Scan a project`,
1215+
}
1216+
cmdCommand.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "SCS Engine flag")
1217+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "GitHub token to be used with SCS engines")
1218+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "GitHub url to be used with SCS engines")
1219+
_ = cmdCommand.Execute()
1220+
_ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection,scorecard")
1221+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken)
1222+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyRepoWithTokenAndUsername)
1223+
1224+
result, _ := addSCSScan(cmdCommand, resubmitConfig, true)
1225+
1226+
// Close the writer to signal that we are done capturing the output
1227+
w.Close()
1228+
1229+
// Read from the pipe (stdout)
1230+
var buf bytes.Buffer
1231+
_, err := io.Copy(&buf, r) // Copy the captured output to a buffer
1232+
if err != nil {
1233+
t.Fatalf("Failed to capture output: %v", err)
1234+
}
1235+
1236+
output := buf.String()
1237+
if strings.Contains(output, ScsScorecardUnsupportedHostWarningMsg) {
1238+
t.Errorf("Expected output to not contain %q, but got %q", ScsScorecardUnsupportedHostWarningMsg, output)
1239+
}
1240+
1241+
scsConfig := wrappers.SCSConfig{
1242+
Twoms: "true",
1243+
Scorecard: "true",
1244+
RepoURL: dummyRepoWithTokenAndUsername,
1245+
RepoToken: dummyToken,
1246+
}
1247+
scsMapConfig := make(map[string]interface{})
1248+
scsMapConfig[resultsMapType] = commonParams.MicroEnginesType
1249+
scsMapConfig[resultsMapValue] = &scsConfig
1250+
1251+
if !reflect.DeepEqual(result, scsMapConfig) {
1252+
t.Errorf("Expected %+v, but got %+v", scsMapConfig, result)
1253+
}
1254+
}
1255+
1256+
func TestCreateScan_WithSCSSecretDetectionAndScorecardShortenedGithubRepoWithTokenAndUsernameInURL_scsMapHasBoth(t *testing.T) {
1257+
// Create a pipe for capturing stdout
1258+
r, w, _ := os.Pipe()
1259+
oldStdout := os.Stdout
1260+
defer func() { os.Stdout = oldStdout }()
1261+
os.Stdout = w // Redirecting stdout to the pipe
1262+
1263+
var resubmitConfig []wrappers.Config
1264+
cmdCommand := &cobra.Command{
1265+
Use: "scan",
1266+
Short: "Scan a project",
1267+
Long: `Scan a project`,
1268+
}
1269+
cmdCommand.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "SCS Engine flag")
1270+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "GitHub token to be used with SCS engines")
1271+
cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "GitHub url to be used with SCS engines")
1272+
_ = cmdCommand.Execute()
1273+
_ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection,scorecard")
1274+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken)
1275+
_ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyShortenedRepoWithTokenAndUsername)
1276+
1277+
result, _ := addSCSScan(cmdCommand, resubmitConfig, true)
1278+
1279+
// Close the writer to signal that we are done capturing the output
1280+
w.Close()
1281+
1282+
// Read from the pipe (stdout)
1283+
var buf bytes.Buffer
1284+
_, err := io.Copy(&buf, r) // Copy the captured output to a buffer
1285+
if err != nil {
1286+
t.Fatalf("Failed to capture output: %v", err)
1287+
}
1288+
1289+
output := buf.String()
1290+
if strings.Contains(output, ScsScorecardUnsupportedHostWarningMsg) {
1291+
t.Errorf("Expected output to not contain %q, but got %q", ScsScorecardUnsupportedHostWarningMsg, output)
1292+
}
1293+
1294+
scsConfig := wrappers.SCSConfig{
1295+
Twoms: "true",
1296+
Scorecard: "true",
1297+
RepoURL: dummyShortenedRepoWithTokenAndUsername,
1298+
RepoToken: dummyToken,
1299+
}
1300+
scsMapConfig := make(map[string]interface{})
1301+
scsMapConfig[resultsMapType] = commonParams.MicroEnginesType
1302+
scsMapConfig[resultsMapValue] = &scsConfig
1303+
1304+
if !reflect.DeepEqual(result, scsMapConfig) {
1305+
t.Errorf("Expected %+v, but got %+v", scsMapConfig, result)
1306+
}
1307+
}
1308+
10931309
func TestCreateScan_WithSCSSecretDetectionAndScorecardGitLabRepo_scsMapHasSecretDetection(t *testing.T) {
10941310
// Create a pipe for capturing stdout
10951311
r, w, _ := os.Pipe()

0 commit comments

Comments
 (0)