Skip to content

Commit 4cde47f

Browse files
committed
fix(secret): correctly skip secret-scanner config file from scanning
1 parent ea7e9ad commit 4cde47f

2 files changed

Lines changed: 73 additions & 23 deletions

File tree

pkg/fanal/analyzer/secret/secret.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,15 @@ func (a *SecretAnalyzer) Required(filePath string, fi os.FileInfo) bool {
164164
return false
165165
}
166166

167-
// Skip the config file for secret scanning
168-
if filepath.Base(a.configPath) == filePath {
169-
return false
167+
// Skip the secret-scanner config file itself.
168+
// filePath is scan-relative and slash-normalized by the walker; configPath comes from the
169+
// --secret-config flag verbatim and may contain native separators, so normalize both.
170+
if a.configPath != "" {
171+
cleanFile := filepath.ToSlash(filepath.Clean(filePath))
172+
cleanConfig := filepath.ToSlash(filepath.Clean(a.configPath))
173+
if cleanConfig == cleanFile {
174+
return false
175+
}
170176
}
171177

172178
// Check if the file extension should be skipped

pkg/fanal/analyzer/secret/secret_test.go

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,35 +208,73 @@ func TestSecretAnalyzer(t *testing.T) {
208208
}
209209

210210
func TestSecretRequire(t *testing.T) {
211+
const defaultConfig = "testdata/skip-tests-config.yaml"
212+
211213
tests := []struct {
212-
name string
213-
filePath string
214-
want bool
214+
name string
215+
configPath string
216+
filePath string
217+
want bool
215218
}{
216219
{
217-
name: "pass regular file",
218-
filePath: "testdata/secret.txt",
219-
want: true,
220+
name: "pass regular file",
221+
configPath: defaultConfig,
222+
filePath: "testdata/secret.txt",
223+
want: true,
224+
},
225+
{
226+
name: "skip small file",
227+
configPath: defaultConfig,
228+
filePath: "testdata/emptyfile",
229+
want: false,
230+
},
231+
{
232+
name: "skip folder",
233+
configPath: defaultConfig,
234+
filePath: "testdata/node_modules/secret.txt",
235+
want: false,
220236
},
221237
{
222-
name: "skip small file",
223-
filePath: "testdata/emptyfile",
224-
want: false,
238+
name: "skip file",
239+
configPath: defaultConfig,
240+
filePath: "testdata/package-lock.json",
241+
want: false,
225242
},
226243
{
227-
name: "skip folder",
228-
filePath: "testdata/node_modules/secret.txt",
229-
want: false,
244+
name: "skip extension",
245+
configPath: defaultConfig,
246+
filePath: "testdata/secret.doc",
247+
want: false,
230248
},
231249
{
232-
name: "skip file",
233-
filePath: "testdata/package-lock.json",
234-
want: false,
250+
name: "skip config file when configPath is a relative path matching filePath",
251+
configPath: "testdata/skip-tests-config.yaml",
252+
filePath: "testdata/skip-tests-config.yaml",
253+
want: false,
235254
},
236255
{
237-
name: "skip extension",
238-
filePath: "testdata/secret.doc",
239-
want: false,
256+
name: "skip config file when configPath is a bare filename matching filePath",
257+
configPath: "skip-tests-config.yaml",
258+
filePath: "skip-tests-config.yaml",
259+
want: false,
260+
},
261+
{
262+
name: "do not skip unrelated file sharing a path suffix with configPath",
263+
configPath: "foo/bar/myconfig.yaml",
264+
filePath: "bar/myconfig.yaml",
265+
want: true,
266+
},
267+
{
268+
name: "do not skip file at scan root when configPath is in a subfolder",
269+
configPath: "configs/myconfig.yaml",
270+
filePath: "myconfig.yaml",
271+
want: true,
272+
},
273+
{
274+
name: "do not skip file when configPath is empty",
275+
configPath: "",
276+
filePath: "src/myfile.yaml",
277+
want: true,
240278
},
241279
}
242280

@@ -245,12 +283,18 @@ func TestSecretRequire(t *testing.T) {
245283
a := secret.SecretAnalyzer{}
246284
err := a.Init(analyzer.AnalyzerOptions{
247285
SecretScannerOption: analyzer.SecretScannerOption{
248-
ConfigPath: "testdata/skip-tests-config.yaml",
286+
ConfigPath: tt.configPath,
249287
},
250288
})
251289
require.NoError(t, err)
252290

253-
fi, err := os.Stat(tt.filePath)
291+
// Stat a real file so fi.Size() passes the small-file check; the path
292+
// argument passed to Required can be a synthetic scan-relative path.
293+
statPath := tt.filePath
294+
if _, err := os.Stat(statPath); err != nil {
295+
statPath = defaultConfig
296+
}
297+
fi, err := os.Stat(statPath)
254298
require.NoError(t, err)
255299

256300
got := a.Required(tt.filePath, fi)

0 commit comments

Comments
 (0)