Skip to content

Commit 339a74b

Browse files
committed
Fix go-gitea#16252 - Equivalent to go-gitea#16268
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 4b7258e commit 339a74b

File tree

7 files changed

+22
-21
lines changed

7 files changed

+22
-21
lines changed

models/login_source.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/timeutil"
14+
jsoniter "github.com/json-iterator/go"
1415

1516
"xorm.io/xorm"
1617
"xorm.io/xorm/convert"
@@ -126,6 +127,17 @@ func Cell2Int64(val xorm.Cell) int64 {
126127
return (*val).(int64)
127128
}
128129

130+
// JsonUnmarshalIgnoreErroneousBOM - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
131+
// possible that a Blob may gain an unwanted prefix of 0xff 0xfe.
132+
func JsonUnmarshalIgnoreErroneousBOM(bs []byte, v interface{}) error {
133+
json := jsoniter.ConfigCompatibleWithStandardLibrary
134+
err := json.Unmarshal(bs, &v)
135+
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
136+
err = json.Unmarshal(bs[2:], &v)
137+
}
138+
return err
139+
}
140+
129141
// BeforeSet is invoked from XORM before setting the value of a field of this object.
130142
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
131143
if colName == "type" {

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.

services/auth/source/ldap/source.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ type wrappedSource struct {
6565

6666
// FromDB fills up a LDAPConfig from serialized format.
6767
func (source *Source) FromDB(bs []byte) error {
68-
json := jsoniter.ConfigCompatibleWithStandardLibrary
69-
7068
wrapped := &wrappedSource{
7169
Source: source,
7270
}
7371

74-
err := json.Unmarshal(bs, &wrapped)
72+
err := models.JsonUnmarshalIgnoreErroneousBOM(bs, &wrapped)
7573
if err != nil {
7674
return err
7775
}

services/auth/source/oauth2/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ type Source struct {
3232

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

3938
// ToDB exports an SMTPConfig to a serialized format.

services/auth/source/pam/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ type Source struct {
2828

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

3534
// ToDB exports a PAMConfig to a serialized format.

services/auth/source/smtp/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ type Source struct {
3232

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

3938
// ToDB exports an SMTPConfig to a serialized format.

services/auth/source/sspi/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ type Source struct {
2727

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

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

0 commit comments

Comments
 (0)