Skip to content

Commit 6a1590b

Browse files
committed
Prevent nil pointer when loading any JSON configuration
1 parent dd16059 commit 6a1590b

File tree

17 files changed

+63
-10
lines changed

17 files changed

+63
-10
lines changed

admin/jwt.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package main
33
import (
44
"crypto/rsa"
55
"crypto/tls"
6+
"fmt"
67

78
"github.com/golang-jwt/jwt/v4"
89
"github.com/jmpsec/osctrl/settings"
910
"github.com/jmpsec/osctrl/types"
10-
"github.com/spf13/viper"
1111
"github.com/rs/zerolog/log"
12+
"github.com/spf13/viper"
1213
)
1314

1415
// Function to load the configuration file
@@ -22,6 +23,9 @@ func loadJWTConfiguration(file string) (types.JSONConfigurationJWT, error) {
2223
}
2324
// JWT values
2425
jwtRaw := viper.Sub(settings.AuthJWT)
26+
if jwtRaw == nil {
27+
return cfg, fmt.Errorf("JSON key %s not found in file %s", settings.AuthJWT, file)
28+
}
2529
if err := jwtRaw.Unmarshal(&cfg); err != nil {
2630
return cfg, err
2731
}

admin/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func loadConfiguration(file, service string) (types.JSONConfigurationAdmin, erro
206206
// Admin values
207207
adminRaw := viper.Sub(service)
208208
if adminRaw == nil {
209-
return cfg, fmt.Errorf("JSON key %s not found in %s", service, file)
209+
return cfg, fmt.Errorf("JSON key %s not found in file %s", service, file)
210210
}
211211
if err := adminRaw.Unmarshal(&cfg); err != nil {
212212
return cfg, err

admin/oauth.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
"github.com/jmpsec/osctrl/settings"
57
"github.com/rs/zerolog/log"
68
"github.com/spf13/viper"
@@ -26,6 +28,9 @@ func loadOAuth(file string) (JSONConfigurationOAuth, error) {
2628
}
2729
// OAuth values
2830
oauthRaw := viper.Sub(settings.AuthOAuth)
31+
if oauthRaw == nil {
32+
return cfg, fmt.Errorf("JSON key %s not found in %s", settings.AuthOAuth, file)
33+
}
2934
if err := oauthRaw.Unmarshal(&cfg); err != nil {
3035
return cfg, err
3136
}

admin/oidc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package main
22

33
import (
4+
"fmt"
45

56
"github.com/jmpsec/osctrl/settings"
6-
"github.com/spf13/viper"
77
"github.com/rs/zerolog/log"
8+
"github.com/spf13/viper"
89
)
910

1011
// JSONConfigurationOIDC to keep all OIDC details for auth
@@ -30,6 +31,9 @@ func loadOIDC(file string) (JSONConfigurationOIDC, error) {
3031
}
3132
// OAuth values
3233
oauthRaw := viper.Sub(settings.AuthOIDC)
34+
if oauthRaw == nil {
35+
return cfg, fmt.Errorf("JSON key %s not found in %s", settings.AuthOIDC, file)
36+
}
3337
if err := oauthRaw.Unmarshal(&cfg); err != nil {
3438
return cfg, err
3539
}

admin/saml.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func loadSAML(file string) (JSONConfigurationSAML, error) {
4545
}
4646
// SAML values
4747
samlRaw := viper.Sub(settings.AuthSAML)
48+
if samlRaw == nil {
49+
return cfg, fmt.Errorf("JSON key %s not found in %s", settings.AuthSAML, file)
50+
}
4851
if err := samlRaw.Unmarshal(&cfg); err != nil {
4952
return cfg, err
5053
}

api/jwt.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
"github.com/jmpsec/osctrl/settings"
57
"github.com/jmpsec/osctrl/types"
68
"github.com/rs/zerolog/log"
@@ -17,8 +19,11 @@ func loadJWTConfiguration(file string) (types.JSONConfigurationJWT, error) {
1719
return cfg, err
1820
}
1921
// JWT values
20-
headersRaw := viper.Sub(settings.AuthJWT)
21-
if err := headersRaw.Unmarshal(&cfg); err != nil {
22+
jwtRaw := viper.Sub(settings.AuthJWT)
23+
if jwtRaw == nil {
24+
return cfg, fmt.Errorf("JSON key %s not found in %s", settings.AuthJWT, file)
25+
}
26+
if err := jwtRaw.Unmarshal(&cfg); err != nil {
2227
return cfg, err
2328
}
2429
// No errors!

backend/backend.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func LoadConfiguration(file, key string) (JSONConfigurationDB, error) {
4848
}
4949
// Backend values
5050
dbRaw := viper.Sub(key)
51+
if dbRaw == nil {
52+
return config, fmt.Errorf("JSON key %s not found in %s", key, file)
53+
}
5154
if err := dbRaw.Unmarshal(&config); err != nil {
5255
return config, err
5356
}

cache/cache.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ func LoadConfiguration(file, key string) (JSONConfigurationRedis, error) {
4646
return config, err
4747
}
4848
// Backend values
49-
dbRaw := viper.Sub(key)
50-
if err := dbRaw.Unmarshal(&config); err != nil {
49+
redisRaw := viper.Sub(key)
50+
if redisRaw == nil {
51+
return config, fmt.Errorf("JSON key %s not found in %s", key, file)
52+
}
53+
if err := redisRaw.Unmarshal(&config); err != nil {
5154
return config, err
5255
}
5356
// No errors!

carves/s3.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func LoadS3(file string) (types.S3Configuration, error) {
8585
return _s3Cfg, err
8686
}
8787
cfgRaw := viper.Sub(settings.LoggingS3)
88+
if cfgRaw == nil {
89+
return _s3Cfg, fmt.Errorf("JSON key %s not found in %s", settings.LoggingS3, file)
90+
}
8891
if err := cfgRaw.Unmarshal(&_s3Cfg); err != nil {
8992
return _s3Cfg, err
9093
}

cli/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func loadAPIConfiguration(file string) (JSONConfigurationAPI, error) {
6868
// API values
6969
apiRaw := viper.Sub(projectName)
7070
if apiRaw == nil {
71-
return config, fmt.Errorf("could not find key %s", projectName)
71+
return config, fmt.Errorf("JSON key %s not found in %s", projectName, file)
7272
}
7373
if err := apiRaw.Unmarshal(&config); err != nil {
7474
return config, err

0 commit comments

Comments
 (0)