Skip to content

Commit e6ac143

Browse files
committed
Increase the size of the webauthn_credential credential_id field
Unfortunately credentialIDs in u2f are 255 bytes long which with base32 encoding becomes 408 bytes. The default size of a xorm string field is only a VARCHAR(255) This problem is not apparent on SQLite because strings get mapped to TEXT there. Fix go-gitea#18727 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 4e57bd1 commit e6ac143

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

models/migrations/v207.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func addWebAuthnCred(x *xorm.Engine) error {
2222
Name string
2323
LowerName string `xorm:"unique(s)"`
2424
UserID int64 `xorm:"INDEX unique(s)"`
25-
CredentialID string `xorm:"INDEX"`
25+
CredentialID string `xorm:"INDEX VARCHAR(410)"` // CredentalID in U2F is at most 255bytes / 5 * 8 = 408 - add a few extra characters for safety
2626
PublicKey []byte
2727
AttestationType string
2828
AAGUID []byte

models/migrations/v208.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func useBase32HexForCredIDInWebAuthnCredential(x *xorm.Engine) error {
1515
// Create webauthnCredential table
1616
type webauthnCredential struct {
1717
ID int64 `xorm:"pk autoincr"`
18-
CredentialID string `xorm:"INDEX"`
18+
CredentialID string `xorm:"INDEX VARCHAR(410)"`
1919
}
2020
if err := x.Sync2(&webauthnCredential{}); err != nil {
2121
return err

models/migrations/v209.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
"xorm.io/xorm/schemas"
10+
)
11+
12+
func increaseCredentialIDTo410(x *xorm.Engine) error {
13+
// Create webauthnCredential table
14+
type webauthnCredential struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
CredentialID string `xorm:"INDEX VARCHAR(410)"`
17+
}
18+
if err := x.Sync2(&webauthnCredential{}); err != nil {
19+
return err
20+
}
21+
22+
switch x.Dialect().URI().DBType {
23+
case schemas.MYSQL:
24+
_, err := x.Exec("ALTER TABLE webauthn_credential MODIFY COLUMN content VARCHAR(410)")
25+
if err != nil {
26+
return err
27+
}
28+
case schemas.ORACLE:
29+
_, err := x.Exec("ALTER TABLE webauthn_credential MODIFY content VARCHAR(410)")
30+
if err != nil {
31+
return err
32+
}
33+
case schemas.MSSQL:
34+
_, err := x.Exec("ALTER TABLE webauthn_credential ALTER COLUMN content VARCHAR(410)")
35+
if err != nil {
36+
return err
37+
}
38+
case schemas.POSTGRES:
39+
_, err := x.Exec("ALTER TABLE webauthn_credential ALTER COLUMN content TYPE VARCHAR(410)")
40+
if err != nil {
41+
return err
42+
}
43+
default:
44+
// SQLite doesn't support ALTER COLUMN, and it seem to already makes String _TEXT_ by default so no migration needed
45+
}
46+
47+
return nil
48+
}

0 commit comments

Comments
 (0)