Skip to content

Commit e7b3ae9

Browse files
MVrachevccojocar
authored andcommitted
Clarify and add new unit tests for rule G107 (#376)
The existing unit tests for G107 didn't have any comments why a certain code is problematic. Other than that we need more unit tests for rule G107 for the different scenarios. Signed-off-by: Martin Vrachev <[email protected]>
1 parent f90efff commit e7b3ae9

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

testutils/source.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func main() {
317317

318318
// SampleCodeG107 - SSRF via http requests with variable url
319319
SampleCodeG107 = []CodeSample{{[]string{`
320+
// Input from the std in is considered insecure
320321
package main
321322
import (
322323
"net/http"
@@ -342,6 +343,53 @@ func main() {
342343
}
343344
fmt.Printf("%s", body)
344345
}`}, 1, gosec.NewConfig()}, {[]string{`
346+
// A variable value can easily be changed no matter
347+
// if it's a global or a local one
348+
package main
349+
350+
import (
351+
"fmt"
352+
"io/ioutil"
353+
"net/http"
354+
)
355+
356+
var url string = "https://www.google.com"
357+
358+
func main() {
359+
360+
resp, err := http.Get(url)
361+
if err != nil {
362+
panic(err)
363+
}
364+
defer resp.Body.Close()
365+
body, err := ioutil.ReadAll(resp.Body)
366+
if err != nil {
367+
panic(err)
368+
}
369+
fmt.Printf("%s", body)
370+
}`}, 1, gosec.NewConfig()}, {[]string{`
371+
// Environmental variables are not considered as secure source
372+
package main
373+
import (
374+
"net/http"
375+
"io/ioutil"
376+
"fmt"
377+
"os"
378+
)
379+
func main() {
380+
url := os.Getenv("tainted_url")
381+
resp, err := http.Get(url)
382+
if err != nil {
383+
panic(err)
384+
}
385+
defer resp.Body.Close()
386+
body, err := ioutil.ReadAll(resp.Body)
387+
if err != nil {
388+
panic(err)
389+
}
390+
fmt.Printf("%s", body)
391+
}`}, 1, gosec.NewConfig()}, {[]string{`
392+
// Constant variables or harcoded strings are secure
345393
package main
346394
347395
import (

0 commit comments

Comments
 (0)