Skip to content

Commit d6c6190

Browse files
committed
Handle misencoding of login_source cfg in mssql
Unfortunately due a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) updating loginsources on MSSQL causes them to become corrupted. (go-gitea#16252) Whilst waiting for the referenced PR to be merged and to handle the corrupted loginsources correctly we need to add a wrapper to the `FromDB()` methods to look for and ignore the misplaced BOMs that have been added. Fix go-gitea#16252 Signed-off-by: Andrew Thornton <[email protected]>
1 parent f533b5d commit d6c6190

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

models/login_source.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,25 @@ var (
7070
_ convert.Conversion = &SSPIConfig{}
7171
)
7272

73+
// jsonUnmarshalIgnoreErroneousBOM - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) >
74+
// possible that a Blob may gain an unwanted prefix of 0xff 0xfe.
75+
func jsonUnmarshalIgnoreErroneousBOM(bs []byte, v interface{}) error {
76+
json := jsoniter.ConfigCompatibleWithStandardLibrary
77+
err := json.Unmarshal(bs, &v)
78+
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
79+
err = json.Unmarshal(bs[2:], &v)
80+
}
81+
return err
82+
}
83+
7384
// LDAPConfig holds configuration for LDAP login source.
7485
type LDAPConfig struct {
7586
*ldap.Source
7687
}
7788

7889
// FromDB fills up a LDAPConfig from serialized format.
7990
func (cfg *LDAPConfig) FromDB(bs []byte) error {
80-
json := jsoniter.ConfigCompatibleWithStandardLibrary
81-
err := json.Unmarshal(bs, &cfg)
91+
err := jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
8292
if err != nil {
8393
return err
8494
}
@@ -119,8 +129,7 @@ type SMTPConfig struct {
119129

120130
// FromDB fills up an SMTPConfig from serialized format.
121131
func (cfg *SMTPConfig) FromDB(bs []byte) error {
122-
json := jsoniter.ConfigCompatibleWithStandardLibrary
123-
return json.Unmarshal(bs, cfg)
132+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
124133
}
125134

126135
// ToDB exports an SMTPConfig to a serialized format.
@@ -137,8 +146,7 @@ type PAMConfig struct {
137146

138147
// FromDB fills up a PAMConfig from serialized format.
139148
func (cfg *PAMConfig) FromDB(bs []byte) error {
140-
json := jsoniter.ConfigCompatibleWithStandardLibrary
141-
return json.Unmarshal(bs, &cfg)
149+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
142150
}
143151

144152
// ToDB exports a PAMConfig to a serialized format.
@@ -159,8 +167,7 @@ type OAuth2Config struct {
159167

160168
// FromDB fills up an OAuth2Config from serialized format.
161169
func (cfg *OAuth2Config) FromDB(bs []byte) error {
162-
json := jsoniter.ConfigCompatibleWithStandardLibrary
163-
return json.Unmarshal(bs, cfg)
170+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
164171
}
165172

166173
// ToDB exports an SMTPConfig to a serialized format.
@@ -180,8 +187,7 @@ type SSPIConfig struct {
180187

181188
// FromDB fills up an SSPIConfig from serialized format.
182189
func (cfg *SSPIConfig) FromDB(bs []byte) error {
183-
json := jsoniter.ConfigCompatibleWithStandardLibrary
184-
return json.Unmarshal(bs, cfg)
190+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
185191
}
186192

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

models/repo_unit.go

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

2929
// FromDB fills up a UnitConfig from serialized format.
3030
func (cfg *UnitConfig) FromDB(bs []byte) error {
31-
json := jsoniter.ConfigCompatibleWithStandardLibrary
32-
return json.Unmarshal(bs, &cfg)
31+
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
3332
}
3433

3534
// ToDB exports a UnitConfig to a serialized format.
@@ -45,8 +44,7 @@ type ExternalWikiConfig struct {
4544

4645
// FromDB fills up a ExternalWikiConfig from serialized format.
4746
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
48-
json := jsoniter.ConfigCompatibleWithStandardLibrary
49-
return json.Unmarshal(bs, &cfg)
47+
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
5048
}
5149

5250
// ToDB exports a ExternalWikiConfig to a serialized format.
@@ -64,8 +62,7 @@ type ExternalTrackerConfig struct {
6462

6563
// FromDB fills up a ExternalTrackerConfig from serialized format.
6664
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
67-
json := jsoniter.ConfigCompatibleWithStandardLibrary
68-
return json.Unmarshal(bs, &cfg)
65+
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
6966
}
7067

7168
// ToDB exports a ExternalTrackerConfig to a serialized format.
@@ -83,8 +80,7 @@ type IssuesConfig struct {
8380

8481
// FromDB fills up a IssuesConfig from serialized format.
8582
func (cfg *IssuesConfig) FromDB(bs []byte) error {
86-
json := jsoniter.ConfigCompatibleWithStandardLibrary
87-
return json.Unmarshal(bs, &cfg)
83+
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
8884
}
8985

9086
// ToDB exports a IssuesConfig to a serialized format.
@@ -107,8 +103,7 @@ type PullRequestsConfig struct {
107103

108104
// FromDB fills up a PullRequestsConfig from serialized format.
109105
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
110-
json := jsoniter.ConfigCompatibleWithStandardLibrary
111-
return json.Unmarshal(bs, &cfg)
106+
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
112107
}
113108

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

0 commit comments

Comments
 (0)