Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2635778

Browse files
zeripath6543
andauthoredJul 20, 2021
Add basic edit ldap auth test & actually fix #16252 (#16465)
One of the reasons why #16447 was needed and why #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 #16447 and #16268 aren't actually solving #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 #16252 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
1 parent 08ef45b commit 2635778

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed
 

‎integrations/auth_ldap_test.go

+54
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

+28-8
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"
@@ -70,11 +71,30 @@ var (
7071
_ convert.Conversion = &SSPIConfig{}
7172
)
7273

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

89109
// FromDB fills up a LDAPConfig from serialized format.
90110
func (cfg *LDAPConfig) FromDB(bs []byte) error {
91-
err := jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
111+
err := jsonUnmarshalHandleDoubleEncode(bs, &cfg)
92112
if err != nil {
93113
return err
94114
}
@@ -129,7 +149,7 @@ type SMTPConfig struct {
129149

130150
// FromDB fills up an SMTPConfig from serialized format.
131151
func (cfg *SMTPConfig) FromDB(bs []byte) error {
132-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
152+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
133153
}
134154

135155
// ToDB exports an SMTPConfig to a serialized format.
@@ -146,7 +166,7 @@ type PAMConfig struct {
146166

147167
// FromDB fills up a PAMConfig from serialized format.
148168
func (cfg *PAMConfig) FromDB(bs []byte) error {
149-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
169+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
150170
}
151171

152172
// ToDB exports a PAMConfig to a serialized format.
@@ -167,7 +187,7 @@ type OAuth2Config struct {
167187

168188
// FromDB fills up an OAuth2Config from serialized format.
169189
func (cfg *OAuth2Config) FromDB(bs []byte) error {
170-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
190+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
171191
}
172192

173193
// ToDB exports an SMTPConfig to a serialized format.
@@ -187,7 +207,7 @@ type SSPIConfig struct {
187207

188208
// FromDB fills up an SSPIConfig from serialized format.
189209
func (cfg *SSPIConfig) FromDB(bs []byte) error {
190-
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
210+
return jsonUnmarshalHandleDoubleEncode(bs, cfg)
191211
}
192212

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

‎models/repo_unit.go

+5-5
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.
@@ -104,7 +104,7 @@ type PullRequestsConfig struct {
104104

105105
// FromDB fills up a PullRequestsConfig from serialized format.
106106
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
107-
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
107+
return jsonUnmarshalHandleDoubleEncode(bs, &cfg)
108108
}
109109

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

0 commit comments

Comments
 (0)
Please sign in to comment.