-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Fix/pr 2900 #2998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix/pr 2900 #2998
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
go-playground validator v10 max tag strings rune count utf8💡 Result:
In go-playground/validator/v10, the
maxtag (andmin/len/gt/gte/lt/lteon strings) measures string length in Unicode code points (runes), not bytes:maxis implemented ashasMaxOf→isLte[1].reflect.String,isLtecomparesint64(utf8.RuneCountInString(field.String())) <= p[1].min(and by extensionmax) for strings as “number of characters” [2].Notes:
utf8.RuneCountInStringsemantics.Sources: [1] baked-in implementation (
hasMaxOf/isLteusesutf8.RuneCountInString) https://docs-go.hexacode.org/src/github.com/go-playground/validator/v10/baked_in.go, [2] pkg.go.dev docs forminon strings (“characters”) https://pkg.go.dev/github.com/go-playground/validator/v10🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 84
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 184
🏁 Script executed:
# Check imports in controller/oauth.go to see current JSON handling patterns head -50 controller/oauth.goRepository: QuantumNous/new-api
Length of output: 1316
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1138
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 894
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 816
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 2899
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1035
Use
utf8.RuneCountInString()instead oflen()to count username length consistently with the validator constraint.The
User.Usernamefield has avalidate:"max=20"constraint that counts UTF-8 runes (not bytes), but this code checkslen(oauthUser.Username) <= model.UserNameMaxLengthwhich counts bytes. A 15-character CJK username (45 bytes but only 15 runes) would pass validation in a normal registration flow but get silently rejected here, falling back to an auto-generated username.Additionally, when
CheckUserExistOrDeletedreturns an error, it is currently silently swallowed without logging. This makes transient database failures invisible.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents
🧩 Analysis chain
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 233
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 571
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 638
🌐 Web query:
go-playground/validator v10 max constraint string rune count implementation💡 Result:
In
go-playground/validator/v10(e.g., v10.30.1), themaxtag for strings is implemented ashasMaxOf, which simply delegates toisLte(“less-than-or-equal”).isLtemeasures string length usingutf8.RuneCountInString(field.String()), i.e. rune/code-point count, not byte length (and not grapheme-cluster/user-perceived character count).maxandltetherefore behave the same for strings. [1]The package docs also describe
maxfor strings as “string length is at most that number of characters.” [2]Sources
[1]
baked_in.go(hasMaxOf→isLte;isLteusesutf8.RuneCountInStringfor strings) (raw.githubusercontent.com)[2] Package documentation for
max(pkg.go.dev)Citations:
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 3986
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 168
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1253
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 198
Use
utf8.RuneCountInString()instead oflen()to match validator v10'smaxconstraint, and log errors fromCheckUserExistOrDeleted().The
Usernamefield hasvalidate:"max=20"which measures rune count viautf8.RuneCountInString(). However, the guard at line 244 useslen(), which counts bytes. For multi-byte usernames (e.g., 15 CJK characters = 45 bytes), this incorrectly rejects valid usernames that would pass validation.Additionally, errors from
CheckUserExistOrDeleted()are silently discarded, making DB failures invisible. Other parts of the codebase (e.g.,controller/user.go) properly log these errors withcommon.SysLog().Proposed fix
🤖 Prompt for AI Agents