Skip to content

Commit e6218c8

Browse files
Eshani Parulekarccojocar
authored andcommitted
test(g304): add samples for var perm and var flag with cleaned path\n\n- Ensure G304 does not fire when only non-path args (flag/perm) are variables\n- Both samples use filepath.Clean on the path arg\n- Rules suite remains green (42 passed)
1 parent 79f835d commit e6218c8

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

testutils/g304_samples.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,47 @@ func main() {
301301
package main
302302
303303
var THEWD string
304+
`}, 0, gosec.NewConfig()},
305+
{[]string{`
306+
package main
307+
308+
import (
309+
"os"
310+
"path/filepath"
311+
)
312+
313+
func open(fn string, perm os.FileMode) {
314+
fh, err := os.OpenFile(filepath.Clean(fn), os.O_RDONLY, perm)
315+
if err != nil {
316+
panic(err)
317+
}
318+
defer fh.Close()
319+
}
320+
321+
func main() {
322+
fn := "filename"
323+
open(fn, 0o600)
324+
}
325+
`}, 0, gosec.NewConfig()},
326+
{[]string{`
327+
package main
328+
329+
import (
330+
"os"
331+
"path/filepath"
332+
)
333+
334+
func open(fn string, flag int) {
335+
fh, err := os.OpenFile(filepath.Clean(fn), flag, 0o600)
336+
if err != nil {
337+
panic(err)
338+
}
339+
defer fh.Close()
340+
}
341+
342+
func main() {
343+
fn := "filename"
344+
open(fn, os.O_RDONLY)
345+
}
304346
`}, 0, gosec.NewConfig()},
305347
}

0 commit comments

Comments
 (0)