Skip to content

Commit 24a6f80

Browse files
authored
Fix GOSEC (#3357)
* Fix GOSEC
1 parent ac81d50 commit 24a6f80

File tree

8 files changed

+25
-21
lines changed

8 files changed

+25
-21
lines changed

artifactory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6815,7 +6815,7 @@ func setupTestFilesForSearchPatterns(t *testing.T) {
68156815
tmpFile, err := os.CreateTemp("", "test-file-*.txt")
68166816
assert.NoError(t, err)
68176817
defer func(name string) {
6818-
_ = os.Remove(name)
6818+
_ = os.Remove(name) //#nosec G703 -- test code, path from temp file
68196819
}(tmpFile.Name())
68206820
_, err = tmpFile.WriteString("test content")
68216821
if err != nil {

docker_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func setupInsecureBuildxBuilder(t *testing.T, builderName string) func() {
138138
http = true
139139
insecure = true
140140
`, registryHost)
141-
require.NoError(t, os.WriteFile(configPath, []byte(configContent), 0644))
141+
require.NoError(t, os.WriteFile(configPath, []byte(configContent), 0644)) //#nosec G703 -- test code, path is constructed from temp dir
142142

143143
// Remove builder if it exists (stop first, then remove)
144144
_ = exec.Command("docker", "buildx", "stop", builderName).Run()
@@ -427,7 +427,7 @@ RUN echo "Built for nested path test"
427427
CMD ["echo", "Hello from nested path"]`, baseImage)
428428

429429
dockerfilePath := filepath.Join(workspace, "Dockerfile")
430-
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644))
430+
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644)) //#nosec G703 -- test code, path built from test workspace
431431

432432
// Cleanup old build
433433
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, buildName, artHttpDetails)
@@ -515,7 +515,7 @@ RUN echo "This is the nested base image"
515515
CMD ["echo", "base"]`, alpineBase)
516516

517517
baseDockerfilePath := filepath.Join(workspace, "Dockerfile.base")
518-
assert.NoError(t, os.WriteFile(baseDockerfilePath, []byte(baseDockerfile), 0644))
518+
assert.NoError(t, os.WriteFile(baseDockerfilePath, []byte(baseDockerfile), 0644)) //#nosec G703 -- test code, path built from test workspace
519519

520520
// Push base image to nested path
521521
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, baseImageBuildName, artHttpDetails)
@@ -538,7 +538,7 @@ RUN echo "This is the child image using nested base"
538538
CMD ["echo", "child"]`, baseImageTag)
539539

540540
childDockerfilePath := filepath.Join(workspace, "Dockerfile.child")
541-
assert.NoError(t, os.WriteFile(childDockerfilePath, []byte(childDockerfile), 0644))
541+
assert.NoError(t, os.WriteFile(childDockerfilePath, []byte(childDockerfile), 0644)) //#nosec G703 -- test code, path built from test workspace
542542

543543
// Build child image
544544
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, childBuildName, artHttpDetails)
@@ -1092,7 +1092,7 @@ RUN echo "Hello from test"
10921092
CMD ["sh"]`, baseImage)
10931093

10941094
dockerfilePath := filepath.Join(workspace, "Dockerfile")
1095-
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644))
1095+
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644)) //#nosec G703 -- test code, path built from test workspace
10961096

10971097
// clean build before test
10981098
runJfrogCli(t, "rt", "bc", buildName, buildNumber)
@@ -1141,11 +1141,11 @@ RUN echo "Hello from test"
11411141
CMD ["sh"]`, baseImage)
11421142

11431143
dockerfilePath := filepath.Join(workspace, "Dockerfile")
1144-
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644))
1144+
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644)) //#nosec G703 -- test code, path built from test workspace
11451145

11461146
// Create test file
11471147
testFilePath := filepath.Join(workspace, "test.txt")
1148-
assert.NoError(t, os.WriteFile(testFilePath, []byte("Hello from Docker build test"), 0644))
1148+
assert.NoError(t, os.WriteFile(testFilePath, []byte("Hello from Docker build test"), 0644)) //#nosec G703 -- test code
11491149

11501150
// clean build before test
11511151
runJfrogCli(t, "rt", "bc", buildName, buildNumber)
@@ -1199,7 +1199,7 @@ FROM %s
11991199
CMD ["hello"]`, golangImage, alpineImage)
12001200

12011201
dockerfilePath := filepath.Join(workspace, "Dockerfile")
1202-
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644))
1202+
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644)) //#nosec G703 -- test code, path built from test workspace
12031203

12041204
// clean build before test
12051205
runJfrogCli(t, "rt", "bc", buildName, buildNumber)
@@ -1251,7 +1251,7 @@ RUN echo "Built with buildx"
12511251
CMD ["echo", "Hello from buildx"]`, baseImage)
12521252

12531253
dockerfilePath := filepath.Join(workspace, "Dockerfile")
1254-
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644))
1254+
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644)) //#nosec G703 -- test code, path built from test workspace
12551255

12561256
// Check if buildx is available
12571257
cmd := exec.Command("docker", "buildx", "version")
@@ -1310,7 +1310,7 @@ RUN echo "Testing virtual repo"
13101310
CMD ["sh"]`, baseImage)
13111311

13121312
dockerfilePath := filepath.Join(workspace, "Dockerfile")
1313-
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644))
1313+
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644)) //#nosec G703 -- test code, path built from test workspace
13141314

13151315
// clean build before test
13161316
runJfrogCli(t, "rt", "bc", buildName, buildNumber)
@@ -1455,7 +1455,7 @@ func TestDockerBuildPublishWithCIVcsProps(t *testing.T) {
14551455
CMD ["echo", "Hello from CI VCS test"]`, baseImage)
14561456

14571457
dockerfilePath := filepath.Join(workspace, "Dockerfile")
1458-
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644))
1458+
assert.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfileContent), 0644)) //#nosec G703 -- test code, path built from test workspace
14591459

14601460
// Clean build before test
14611461
runJfrogCli(t, "rt", "bc", buildName, buildNumber)

general/ai/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func sendRestAPI(apiType ApiType, content interface{}) (response string, err err
164164
req.Header.Set(askRateLimitHeader, "true")
165165
}
166166
log.Debug(fmt.Sprintf("Sending HTTP %s request to: %s", req.Method, req.URL))
167-
resp, err := client.GetClient().Do(req)
167+
resp, err := client.GetClient().Do(req) //#nosec G704 -- URL is constructed from a hardcoded AI service endpoint
168168
if err != nil {
169169
err = errorutils.CheckErrorf("CLI-AI server is not available. Please check your network or try again later.")
170170
return

general/summary/cli.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ func saveFile(content, filePath string) (err error) {
131131
if content == "" {
132132
return nil
133133
}
134-
file, err := os.Create(filePath)
134+
// #nosec G703 -- filePath is constructed from SummaryOutputDirPathEnv set by CLI, not arbitrary user input and filePath is already cleaned.
135+
file, err := os.Create(filepath.Clean(filePath))
135136
if err != nil {
136137
return err
137138
}
@@ -145,11 +146,12 @@ func saveFile(content, filePath string) (err error) {
145146
}
146147

147148
func getSectionMarkdownContent(section MarkdownSection) (string, error) {
148-
sectionFilepath := filepath.Join(os.Getenv(coreutils.SummaryOutputDirPathEnv), commandsummary.OutputDirName, string(section), markdownFileName)
149+
sectionFilepath := filepath.Clean(filepath.Join(os.Getenv(coreutils.SummaryOutputDirPathEnv), commandsummary.OutputDirName, string(section), markdownFileName))
150+
// #nosec G703 -- sectionFilepath is constructed from SummaryOutputDirPathEnv set by CLI, not arbitrary user input and sectionFilepath is already cleaned.
149151
if _, err := os.Stat(sectionFilepath); os.IsNotExist(err) {
150152
return "", nil
151153
}
152-
154+
// #nosec G703 -- sectionFilepath is constructed from SummaryOutputDirPathEnv set by CLI, not arbitrary user input
153155
contentBytes, err := os.ReadFile(sectionFilepath)
154156
if err != nil {
155157
return "", fmt.Errorf("error reading markdown file for section %s: %w", section, err)
@@ -279,7 +281,8 @@ func processScan(index commandsummary.Index, filePath string, scannedName string
279281

280282
// shouldGenerateUploadSummary checks if upload summary should be generated.
281283
func shouldGenerateUploadSummary() (bool, error) {
282-
buildInfoPath := filepath.Join(os.Getenv(coreutils.SummaryOutputDirPathEnv), commandsummary.OutputDirName, string(BuildInfo))
284+
buildInfoPath := filepath.Clean(filepath.Join(os.Getenv(coreutils.SummaryOutputDirPathEnv), commandsummary.OutputDirName, string(BuildInfo)))
285+
// #nosec G703 -- buildInfoPath is constructed from SummaryOutputDirPathEnv set by CLI, not arbitrary user input, and buildInfoPath is already cleaned.
283286
if _, err := os.Stat(buildInfoPath); os.IsNotExist(err) {
284287
return true, nil
285288
}

lifecycle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1713,5 +1713,5 @@ type KeyPairPayload struct {
17131713
Alias string `json:"alias,omitempty"`
17141714
Passphrase string `json:"passphrase,omitempty"`
17151715
PublicKey string `json:"publicKey,omitempty"`
1716-
PrivateKey string `json:"privateKey,omitempty"`
1716+
PrivateKey string `json:"privateKey,omitempty"` //#nosec G117 -- test struct, not a real secret
17171717
}

nuget_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func runInitNewConfig(t *testing.T, testSuite testInitNewConfigDescriptor, baseR
251251
return
252252
}
253253

254-
content, err := os.ReadFile(configFile.Name())
254+
content, err := os.ReadFile(configFile.Name()) //#nosec G703 -- test code, path from temp file
255255
if err != nil {
256256
assert.NoError(t, err)
257257
return

utils/cliutils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func getLatestCliVersionFromGithubAPI() (githubVersionInfo githubResponse, err e
668668
func doHttpRequest(client *http.Client, req *http.Request) (resp *http.Response, body []byte, err error) {
669669
const maxResponseSize = 10 * 1024 * 1024 // 10MB limit
670670
req.Close = true
671-
resp, err = client.Do(req)
671+
resp, err = client.Do(req) //#nosec G704 -- URL is constructed internally from validated version API endpoint
672672
if errorutils.CheckError(err) != nil {
673673
return
674674
}

utils/cliutils/utils_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ type redirectingTransport struct {
365365
func (t *redirectingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
366366
if req.URL.String() == t.targetURL {
367367
// Create a new request to the redirect URL
368-
redirectReq, err := http.NewRequest(req.Method, t.redirectURL, req.Body)
368+
// #nosec G704 -- redirectURL is a controlled test value, not user input
369+
redirectReq, err := http.NewRequest(req.Method, t.redirectURL, req.Body) //nolint:gosec // G704 - URL is a test-controlled constant
369370
if err != nil {
370371
return nil, err
371372
}

0 commit comments

Comments
 (0)