Skip to content

Commit

Permalink
Fix go-gitea#16252 - Equivalent to go-gitea#16268
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Jun 27, 2021
1 parent 4b7258e commit 339a74b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 deletions.
12 changes: 12 additions & 0 deletions models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"

"xorm.io/xorm"
"xorm.io/xorm/convert"
Expand Down Expand Up @@ -126,6 +127,17 @@ func Cell2Int64(val xorm.Cell) int64 {
return (*val).(int64)
}

// 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
}

// BeforeSet is invoked from XORM before setting the value of a field of this object.
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
if colName == "type" {
Expand Down
15 changes: 5 additions & 10 deletions models/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type UnitConfig struct{}

// FromDB fills up a UnitConfig from serialized format.
func (cfg *UnitConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

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

// FromDB fills up a ExternalWikiConfig from serialized format.
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

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

// FromDB fills up a ExternalTrackerConfig from serialized format.
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

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

// FromDB fills up a IssuesConfig from serialized format.
func (cfg *IssuesConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

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

// FromDB fills up a PullRequestsConfig from serialized format.
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

// ToDB exports a PullRequestsConfig to a serialized format.
Expand Down
4 changes: 1 addition & 3 deletions services/auth/source/ldap/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ type wrappedSource struct {

// FromDB fills up a LDAPConfig from serialized format.
func (source *Source) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary

wrapped := &wrappedSource{
Source: source,
}

err := json.Unmarshal(bs, &wrapped)
err := models.JsonUnmarshalIgnoreErroneousBOM(bs, &wrapped)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions services/auth/source/oauth2/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type Source struct {

// FromDB fills up an OAuth2Config from serialized format.
func (source *Source) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, source)
return models.JsonUnmarshalIgnoreErroneousBOM(bs, source)
}

// ToDB exports an SMTPConfig to a serialized format.
Expand Down
3 changes: 1 addition & 2 deletions services/auth/source/pam/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type Source struct {

// FromDB fills up a PAMConfig from serialized format.
func (source *Source) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &source)
return models.JsonUnmarshalIgnoreErroneousBOM(bs, &source)
}

// ToDB exports a PAMConfig to a serialized format.
Expand Down
3 changes: 1 addition & 2 deletions services/auth/source/smtp/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type Source struct {

// FromDB fills up an SMTPConfig from serialized format.
func (source *Source) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, source)
return models.JsonUnmarshalIgnoreErroneousBOM(bs, source)
}

// ToDB exports an SMTPConfig to a serialized format.
Expand Down
3 changes: 1 addition & 2 deletions services/auth/source/sspi/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ type Source struct {

// FromDB fills up an SSPIConfig from serialized format.
func (cfg *Source) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, cfg)
return models.JsonUnmarshalIgnoreErroneousBOM(bs, cfg)
}

// ToDB exports an SSPIConfig to a serialized format.
Expand Down

0 comments on commit 339a74b

Please sign in to comment.