Skip to content

Commit 65a6fa7

Browse files
committed
Go Environment variable (parsing) models and tests
1 parent 6103749 commit 65a6fa7

File tree

13 files changed

+206
-1
lines changed

13 files changed

+206
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/go-all
4+
extensible: sourceModel
5+
data:
6+
- ["github.com/hashicorp/go-envparse", "", False, "Parse", "", "", "ReturnValue", "environment", "manual"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/go-all
4+
extensible: sourceModel
5+
data:
6+
- ["github.com/joho/godotenv", "", False, "Parse", "", "", "ReturnValue", "environment", "manual"]
7+
- ["github.com/joho/godotenv", "", False, "Read", "", "", "ReturnValue", "environment", "manual"]
8+
- ["github.com/joho/godotenv", "", False, "Unmarshal", "", "", "ReturnValue", "environment", "manual"]
9+
- ["github.com/joho/godotenv", "", False, "UnmarshalBytes", "", "", "ReturnValue", "environment", "manual"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/go-all
4+
extensible: sourceModel
5+
data:
6+
- ["github.com/kelseyhightower/envconfig", "", False, "CheckDisallowed", "", "", "Argument[1]", "environment", "manual"]
7+
- ["github.com/kelseyhightower/envconfig", "", False, "MustProcess", "", "", "Argument[1]", "environment", "manual"]
8+
- ["github.com/kelseyhightower/envconfig", "", False, "Process", "", "", "Argument[1]", "environment", "manual"]
9+
- ["github.com/kelseyhightower/envconfig", "", False, "Usage", "", "", "Argument[1]", "environment", "manual"]
10+
- ["github.com/kelseyhightower/envconfig", "", False, "Usagef", "", "", "Argument[1]", "environment", "manual"]
11+
- ["github.com/kelseyhightower/envconfig", "", False, "Usaget", "", "", "Argument[1]", "environment", "manual"]

go/ql/lib/ext/os.model.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ extensions:
4646
pack: codeql/go-all
4747
extensible: sourceModel
4848
data:
49+
- ["os", "", False, "Environ", "", "", "ReturnValue", "environment", "manual"]
50+
- ["os", "", False, "Getenv", "", "", "ReturnValue", "environment", "manual"]
51+
- ["os", "", False, "LookupEnv", "", "", "ReturnValue[0]", "environment", "manual"]
4952
- ["os", "", False, "Open", "", "", "ReturnValue[0]", "file", "manual"]
5053
- ["os", "", False, "OpenFile", "", "", "ReturnValue[0]", "file", "manual"]
51-
- ["os", "", False, "ReadFile", "", "", "ReturnValue[0]", "file", "manual"]
54+
- ["os", "", False, "ReadFile", "", "", "ReturnValue[0]", "file", "manual"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module test
2+
3+
go 1.22.5
4+
5+
require (
6+
github.com/hashicorp/go-envparse v0.1.0
7+
github.com/joho/godotenv v1.5.1
8+
github.com/kelseyhightower/envconfig v1.4.0
9+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| test.go:12:10:12:26 | call to Getenv |
2+
| test.go:14:2:14:33 | ... := ...[0] |
3+
| test.go:19:20:19:31 | call to Environ |
4+
| test.go:34:29:34:32 | &... |
5+
| test.go:41:2:41:40 | ... := ...[0] |
6+
| test.go:48:2:48:52 | ... := ...[0] |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/threat-models
4+
extensible: threatModelConfiguration
5+
data:
6+
- ["environment", true, 0]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/go-envparse"
6+
"github.com/joho/godotenv"
7+
"github.com/kelseyhightower/envconfig"
8+
"os"
9+
)
10+
11+
func osEnvironmentVariables() {
12+
home := os.Getenv("HOME")
13+
14+
port, ok := os.LookupEnv("PORT")
15+
if !ok {
16+
port = "3000"
17+
}
18+
19+
for _, e := range os.Environ() {
20+
_ = e
21+
}
22+
23+
fmt.Printf("HOME: %s\n", home)
24+
fmt.Printf("PORT: %s\n", port)
25+
}
26+
27+
type ServerConfig struct {
28+
Port int `envconfig:"PORT"`
29+
Host string `envconfig:"HOST"`
30+
}
31+
32+
func envconfigEnvironmentVariables() {
33+
var cfg ServerConfig
34+
envconfig.Process("myapp", &cfg)
35+
}
36+
37+
func godotenvEnvironmentVariables() {
38+
var err error
39+
var username, greeting string
40+
41+
users, err := godotenv.Read("user.env")
42+
if err != nil {
43+
return
44+
}
45+
46+
username := users["USERNAME"]
47+
48+
greetings, err := godotenv.Unmarshal("HELLO=hello")
49+
if err != nil {
50+
return
51+
}
52+
53+
greeting := greetings["HELLO"]
54+
55+
fmt.Printf("%s, %s!\n", greeting, username)
56+
}
57+
58+
func envparseEnvironmentVariables() {
59+
f := os.Open("file.txt")
60+
envVars, ok := envparse.Parse(f)
61+
62+
if !ok {
63+
return
64+
}
65+
66+
fmt.Printf("HOME: %s\n", envVars["HOME"])
67+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import go
2+
3+
from DataFlow::Node source
4+
where source instanceof ThreatModelFlowSource
5+
select source

go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/environment/vendor/github.com/hashicorp/go-envparse/stub.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)