Skip to content

Commit e11f042

Browse files
authored
Handle misencoding of login_source cfg in mssql (#16268) (#16275)
Backport #16268 Unfortunately due a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) updating loginsources on MSSQL causes them to become corrupted. (#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 #16252 Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 8778263 commit e11f042

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

models/login_source.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,25 @@ var (
6969
_ convert.Conversion = &SSPIConfig{}
7070
)
7171

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 {
75+
json := jsoniter.ConfigCompatibleWithStandardLibrary
76+
err := json.Unmarshal(bs, &v)
77+
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
78+
err = json.Unmarshal(bs[2:], &v)
79+
}
80+
return err
81+
}
82+
7283
// LDAPConfig holds configuration for LDAP login source.
7384
type LDAPConfig struct {
7485
*ldap.Source
7586
}
7687

7788
// FromDB fills up a LDAPConfig from serialized format.
7889
func (cfg *LDAPConfig) FromDB(bs []byte) error {
79-
json := jsoniter.ConfigCompatibleWithStandardLibrary
80-
return json.Unmarshal(bs, &cfg)
90+
return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
8191
}
8292

8393
// ToDB exports a LDAPConfig to a serialized format.
@@ -104,8 +114,7 @@ type SMTPConfig struct {
104114

105115
// FromDB fills up an SMTPConfig from serialized format.
106116
func (cfg *SMTPConfig) FromDB(bs []byte) error {
107-
json := jsoniter.ConfigCompatibleWithStandardLibrary
108-
return json.Unmarshal(bs, cfg)
117+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
109118
}
110119

111120
// ToDB exports an SMTPConfig to a serialized format.
@@ -122,8 +131,7 @@ type PAMConfig struct {
122131

123132
// FromDB fills up a PAMConfig from serialized format.
124133
func (cfg *PAMConfig) FromDB(bs []byte) error {
125-
json := jsoniter.ConfigCompatibleWithStandardLibrary
126-
return json.Unmarshal(bs, &cfg)
134+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
127135
}
128136

129137
// ToDB exports a PAMConfig to a serialized format.
@@ -144,8 +152,7 @@ type OAuth2Config struct {
144152

145153
// FromDB fills up an OAuth2Config from serialized format.
146154
func (cfg *OAuth2Config) FromDB(bs []byte) error {
147-
json := jsoniter.ConfigCompatibleWithStandardLibrary
148-
return json.Unmarshal(bs, cfg)
155+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
149156
}
150157

151158
// ToDB exports an SMTPConfig to a serialized format.
@@ -165,8 +172,7 @@ type SSPIConfig struct {
165172

166173
// FromDB fills up an SSPIConfig from serialized format.
167174
func (cfg *SSPIConfig) FromDB(bs []byte) error {
168-
json := jsoniter.ConfigCompatibleWithStandardLibrary
169-
return json.Unmarshal(bs, cfg)
175+
return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
170176
}
171177

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

models/repo_unit.go

+5-10
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.
@@ -106,8 +102,7 @@ type PullRequestsConfig struct {
106102

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

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

0 commit comments

Comments
 (0)