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>
This commit is contained in:
zeripath 2021-06-27 22:33:25 +01:00 committed by GitHub
parent 87782636e6
commit e11f042a95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 20 deletions

View file

@ -69,6 +69,17 @@ var (
_ convert.Conversion = &SSPIConfig{} _ convert.Conversion = &SSPIConfig{}
) )
// jsonUnmarshalIgnoreErroneousBOM - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
// possible that a Blob may gain an unwanted prefix of 0xff 0xfe.
func jsonUnmarshalIgnoreErroneousBOM(bs []byte, v interface{}) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(bs, &v)
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
err = json.Unmarshal(bs[2:], &v)
}
return err
}
// LDAPConfig holds configuration for LDAP login source. // LDAPConfig holds configuration for LDAP login source.
type LDAPConfig struct { type LDAPConfig struct {
*ldap.Source *ldap.Source
@ -76,8 +87,7 @@ type LDAPConfig struct {
// FromDB fills up a LDAPConfig from serialized format. // FromDB fills up a LDAPConfig from serialized format.
func (cfg *LDAPConfig) FromDB(bs []byte) error { func (cfg *LDAPConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return json.Unmarshal(bs, &cfg)
} }
// ToDB exports a LDAPConfig to a serialized format. // ToDB exports a LDAPConfig to a serialized format.
@ -104,8 +114,7 @@ type SMTPConfig struct {
// FromDB fills up an SMTPConfig from serialized format. // FromDB fills up an SMTPConfig from serialized format.
func (cfg *SMTPConfig) FromDB(bs []byte) error { func (cfg *SMTPConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
return json.Unmarshal(bs, cfg)
} }
// ToDB exports an SMTPConfig to a serialized format. // ToDB exports an SMTPConfig to a serialized format.
@ -122,8 +131,7 @@ type PAMConfig struct {
// FromDB fills up a PAMConfig from serialized format. // FromDB fills up a PAMConfig from serialized format.
func (cfg *PAMConfig) FromDB(bs []byte) error { func (cfg *PAMConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
return json.Unmarshal(bs, &cfg)
} }
// ToDB exports a PAMConfig to a serialized format. // ToDB exports a PAMConfig to a serialized format.
@ -144,8 +152,7 @@ type OAuth2Config struct {
// FromDB fills up an OAuth2Config from serialized format. // FromDB fills up an OAuth2Config from serialized format.
func (cfg *OAuth2Config) FromDB(bs []byte) error { func (cfg *OAuth2Config) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
return json.Unmarshal(bs, cfg)
} }
// ToDB exports an SMTPConfig to a serialized format. // ToDB exports an SMTPConfig to a serialized format.
@ -165,8 +172,7 @@ type SSPIConfig struct {
// FromDB fills up an SSPIConfig from serialized format. // FromDB fills up an SSPIConfig from serialized format.
func (cfg *SSPIConfig) FromDB(bs []byte) error { func (cfg *SSPIConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, cfg)
return json.Unmarshal(bs, cfg)
} }
// ToDB exports an SSPIConfig to a serialized format. // ToDB exports an SSPIConfig to a serialized format.

View file

@ -28,8 +28,7 @@ type UnitConfig struct{}
// FromDB fills up a UnitConfig from serialized format. // FromDB fills up a UnitConfig from serialized format.
func (cfg *UnitConfig) FromDB(bs []byte) error { func (cfg *UnitConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return json.Unmarshal(bs, &cfg)
} }
// ToDB exports a UnitConfig to a serialized format. // ToDB exports a UnitConfig to a serialized format.
@ -45,8 +44,7 @@ type ExternalWikiConfig struct {
// FromDB fills up a ExternalWikiConfig from serialized format. // FromDB fills up a ExternalWikiConfig from serialized format.
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error { func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return json.Unmarshal(bs, &cfg)
} }
// ToDB exports a ExternalWikiConfig to a serialized format. // ToDB exports a ExternalWikiConfig to a serialized format.
@ -64,8 +62,7 @@ type ExternalTrackerConfig struct {
// FromDB fills up a ExternalTrackerConfig from serialized format. // FromDB fills up a ExternalTrackerConfig from serialized format.
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error { func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return json.Unmarshal(bs, &cfg)
} }
// ToDB exports a ExternalTrackerConfig to a serialized format. // ToDB exports a ExternalTrackerConfig to a serialized format.
@ -83,8 +80,7 @@ type IssuesConfig struct {
// FromDB fills up a IssuesConfig from serialized format. // FromDB fills up a IssuesConfig from serialized format.
func (cfg *IssuesConfig) FromDB(bs []byte) error { func (cfg *IssuesConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return json.Unmarshal(bs, &cfg)
} }
// ToDB exports a IssuesConfig to a serialized format. // ToDB exports a IssuesConfig to a serialized format.
@ -106,8 +102,7 @@ type PullRequestsConfig struct {
// FromDB fills up a PullRequestsConfig from serialized format. // FromDB fills up a PullRequestsConfig from serialized format.
func (cfg *PullRequestsConfig) FromDB(bs []byte) error { func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary return jsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return json.Unmarshal(bs, &cfg)
} }
// ToDB exports a PullRequestsConfig to a serialized format. // ToDB exports a PullRequestsConfig to a serialized format.