Skip to content

Commit e50309a

Browse files
committed
Add basic edit ldap auth test & actually fix go-gitea#16252 (go-gitea#16465)
Backport go-gitea#16465 One of the reasons why go-gitea#16447 was needed and why go-gitea#16268 was needed in the first place was because it appears that editing ldap configuration doesn't get tested. This PR therefore adds a basic test that will run the edit pipeline. In doing so it's now clear that go-gitea#16447 and go-gitea#16268 aren't actually solving go-gitea#16252. It turns out that what actually happens is that is that the bytes are actually double encoded. This PR now changes the json unmarshal wrapper to handle this double encode. Fix go-gitea#16252 Signed-off-by: Andrew Thornton <[email protected]>
1 parent e6c2225 commit e50309a

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed

integrations/auth_ldap_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,60 @@ func TestLDAPUserSignin(t *testing.T) {
144144
assert.Equal(t, u.Email, htmlDoc.Find(`label[for="email"]`).Siblings().First().Text())
145145
}
146146

147+
func TestLDAPAuthChange(t *testing.T) {
148+
defer prepareTestEnv(t)()
149+
addAuthSourceLDAP(t, "")
150+
151+
session := loginUser(t, "user1")
152+
req := NewRequest(t, "GET", "/admin/auths")
153+
resp := session.MakeRequest(t, req, http.StatusOK)
154+
doc := NewHTMLParser(t, resp.Body)
155+
href, exists := doc.Find("table.table td a").Attr("href")
156+
if !exists {
157+
assert.True(t, exists, "No authentication source found")
158+
return
159+
}
160+
161+
req = NewRequest(t, "GET", href)
162+
resp = session.MakeRequest(t, req, http.StatusOK)
163+
doc = NewHTMLParser(t, resp.Body)
164+
csrf := doc.GetCSRF()
165+
host, _ := doc.Find(`input[name="host"]`).Attr("value")
166+
assert.Equal(t, host, getLDAPServerHost())
167+
binddn, _ := doc.Find(`input[name="bind_dn"]`).Attr("value")
168+
assert.Equal(t, binddn, "uid=gitea,ou=service,dc=planetexpress,dc=com")
169+
170+
req = NewRequestWithValues(t, "POST", href, map[string]string{
171+
"_csrf": csrf,
172+
"type": "2",
173+
"name": "ldap",
174+
"host": getLDAPServerHost(),
175+
"port": "389",
176+
"bind_dn": "uid=gitea,ou=service,dc=planetexpress,dc=com",
177+
"bind_password": "password",
178+
"user_base": "ou=people,dc=planetexpress,dc=com",
179+
"filter": "(&(objectClass=inetOrgPerson)(memberOf=cn=git,ou=people,dc=planetexpress,dc=com)(uid=%s))",
180+
"admin_filter": "(memberOf=cn=admin_staff,ou=people,dc=planetexpress,dc=com)",
181+
"restricted_filter": "(uid=leela)",
182+
"attribute_username": "uid",
183+
"attribute_name": "givenName",
184+
"attribute_surname": "sn",
185+
"attribute_mail": "mail",
186+
"attribute_ssh_public_key": "",
187+
"is_sync_enabled": "on",
188+
"is_active": "on",
189+
})
190+
session.MakeRequest(t, req, http.StatusFound)
191+
192+
req = NewRequest(t, "GET", href)
193+
resp = session.MakeRequest(t, req, http.StatusOK)
194+
doc = NewHTMLParser(t, resp.Body)
195+
host, _ = doc.Find(`input[name="host"]`).Attr("value")
196+
assert.Equal(t, host, getLDAPServerHost())
197+
binddn, _ = doc.Find(`input[name="bind_dn"]`).Attr("value")
198+
assert.Equal(t, binddn, "uid=gitea,ou=service,dc=planetexpress,dc=com")
199+
}
200+
147201
func TestLDAPUserSync(t *testing.T) {
148202
if skipLDAPTests() {
149203
t.Skip()

models/login_source.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77

88
import (
99
"crypto/tls"
10+
"encoding/binary"
1011
"errors"
1112
"fmt"
1213
"net/smtp"
@@ -69,11 +70,30 @@ var (
6970
_ convert.Conversion = &SSPIConfig{}
7071
)
7172

72-
// jsonUnmarshalIgnoreErroneousBOM - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
73-
// possible that a Blob may gain an unwanted prefix of 0xff 0xfe.
74-
func jsonUnmarshalIgnoreErroneousBOM(bs []byte, v interface{}) error {
73+
// jsonUnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
74+
// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
75+
func jsonUnmarshalHandleDoubleEncode(bs []byte, v interface{}) error {
7576
json := jsoniter.ConfigCompatibleWithStandardLibrary
7677
err := json.Unmarshal(bs, v)
78+
if err != nil {
79+
ok := true
80+
rs := []byte{}
81+
temp := make([]byte, 2)
82+
for _, rn := range string(bs) {
83+
if rn > 0xffff {
84+
ok = false
85+
break
86+
}
87+
binary.LittleEndian.PutUint16(temp, uint16(rn))
88+
rs = append(rs, temp...)
89+
}
90+
if ok {
91+
if rs[0] == 0xff && rs[1] == 0xfe {
92+
rs = rs[2:]
93+
}
94+
err = json.Unmarshal(rs, v)
95+
}
96+
}
7797
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
7898
err = json.Unmarshal(bs[2:], v)
7999
}
@@ -87,7 +107,7 @@ type LDAPConfig struct {
87107

88108
// FromDB fills up a LDAPConfig from serialized format.
89109
func (cfg *LDAPConfig) FromDB(bs []byte) error {
90-
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
110+
return jsonUnmarshalHandleDoubleEncode(bs, &cfg)
91111
}
92112

93113
// ToDB exports a LDAPConfig to a serialized format.
@@ -114,7 +134,7 @@ type SMTPConfig struct {
114134

115135
// FromDB fills up an SMTPConfig from serialized format.
116136
func (cfg *SMTPConfig) FromDB(bs []byte) error {
117-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
137+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
118138
}
119139

120140
// ToDB exports an SMTPConfig to a serialized format.
@@ -131,7 +151,7 @@ type PAMConfig struct {
131151

132152
// FromDB fills up a PAMConfig from serialized format.
133153
func (cfg *PAMConfig) FromDB(bs []byte) error {
134-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
154+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
135155
}
136156

137157
// ToDB exports a PAMConfig to a serialized format.
@@ -152,7 +172,7 @@ type OAuth2Config struct {
152172

153173
// FromDB fills up an OAuth2Config from serialized format.
154174
func (cfg *OAuth2Config) FromDB(bs []byte) error {
155-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
175+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
156176
}
157177

158178
// ToDB exports an SMTPConfig to a serialized format.
@@ -172,7 +192,7 @@ type SSPIConfig struct {
172192

173193
// FromDB fills up an SSPIConfig from serialized format.
174194
func (cfg *SSPIConfig) FromDB(bs []byte) error {
175-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
195+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
176196
}
177197

178198
// ToDB exports an SSPIConfig to a serialized format.

models/repo_unit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type UnitConfig struct{}
2828

2929
// FromDB fills up a UnitConfig from serialized format.
3030
func (cfg *UnitConfig) FromDB(bs []byte) error {
31-
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
31+
return jsonUnmarshalHandleDoubleEncode(bs, &cfg)
3232
}
3333

3434
// ToDB exports a UnitConfig to a serialized format.
@@ -44,7 +44,7 @@ type ExternalWikiConfig struct {
4444

4545
// FromDB fills up a ExternalWikiConfig from serialized format.
4646
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
47-
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
47+
return jsonUnmarshalHandleDoubleEncode(bs, &cfg)
4848
}
4949

5050
// ToDB exports a ExternalWikiConfig to a serialized format.
@@ -62,7 +62,7 @@ type ExternalTrackerConfig struct {
6262

6363
// FromDB fills up a ExternalTrackerConfig from serialized format.
6464
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
65-
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
65+
return jsonUnmarshalHandleDoubleEncode(bs, &cfg)
6666
}
6767

6868
// ToDB exports a ExternalTrackerConfig to a serialized format.
@@ -80,7 +80,7 @@ type IssuesConfig struct {
8080

8181
// FromDB fills up a IssuesConfig from serialized format.
8282
func (cfg *IssuesConfig) FromDB(bs []byte) error {
83-
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
83+
return jsonUnmarshalHandleDoubleEncode(bs, &cfg)
8484
}
8585

8686
// ToDB exports a IssuesConfig to a serialized format.
@@ -102,7 +102,7 @@ type PullRequestsConfig struct {
102102

103103
// FromDB fills up a PullRequestsConfig from serialized format.
104104
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
105-
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
105+
return jsonUnmarshalHandleDoubleEncode(bs, &cfg)
106106
}
107107

108108
// ToDB exports a PullRequestsConfig to a serialized format.

0 commit comments

Comments
 (0)