Skip to content

Commit a5f7e4c

Browse files
committed
Encrypt LDAP bind password in db with SECRET_KEY
The LDAP source bind password are currently stored in plaintext in the db This PR simply encrypts them with the setting.SECRET_KEY. Fix go-gitea#15460 Signed-off-by: Andrew Thornton <[email protected]>
1 parent c9cc669 commit a5f7e4c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

models/login_source.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/auth/oauth2"
1919
"code.gitea.io/gitea/modules/auth/pam"
2020
"code.gitea.io/gitea/modules/log"
21+
"code.gitea.io/gitea/modules/secret"
2122
"code.gitea.io/gitea/modules/setting"
2223
"code.gitea.io/gitea/modules/timeutil"
2324
"code.gitea.io/gitea/modules/util"
@@ -76,11 +77,25 @@ type LDAPConfig struct {
7677
// FromDB fills up a LDAPConfig from serialized format.
7778
func (cfg *LDAPConfig) FromDB(bs []byte) error {
7879
json := jsoniter.ConfigCompatibleWithStandardLibrary
79-
return json.Unmarshal(bs, &cfg)
80+
err := json.Unmarshal(bs, &cfg)
81+
if err != nil {
82+
return err
83+
}
84+
if cfg.BindPasswordEncrypt != "" {
85+
cfg.BindPassword, err = secret.DecryptSecret(setting.SecretKey, cfg.BindPasswordEncrypt)
86+
cfg.BindPasswordEncrypt = ""
87+
}
88+
return err
8089
}
8190

8291
// ToDB exports a LDAPConfig to a serialized format.
8392
func (cfg *LDAPConfig) ToDB() ([]byte, error) {
93+
var err error
94+
cfg.BindPasswordEncrypt, err = secret.EncryptSecret(setting.SecretKey, cfg.BindPassword)
95+
if err != nil {
96+
return nil, err
97+
}
98+
cfg.BindPassword = ""
8499
json := jsoniter.ConfigCompatibleWithStandardLibrary
85100
return json.Marshal(cfg)
86101
}

modules/auth/ldap/ldap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Source struct {
3535
SecurityProtocol SecurityProtocol
3636
SkipVerify bool
3737
BindDN string // DN to bind with
38+
BindPasswordEncrypt string // Encrypted Bind BN password
3839
BindPassword string // Bind DN password
3940
UserBase string // Base search path for users
4041
UserDN string // Template for the DN of the user for simple auth

0 commit comments

Comments
 (0)