-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
Unsanitize user and org names in DB #4762
Open
pat-s
wants to merge
24
commits into
main
Choose a base branch
from
fix/sanitize-usernames-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ecc7541
fix: add user migration
pat-s dce8939
chore: update years
pat-s 1aa34c1
chore: update migration to unsanitize
pat-s 19ac5be
chore: remove all toLower calls
pat-s 291b1be
adjust tests to new migration
pat-s 6a71341
chore: spell
pat-s c0264f8
Update server/store/datastore/migration/024_unsanitize_org_and_user_n…
pat-s fd289fa
Update server/store/datastore/migration/024_unsanitize_org_and_user_n…
pat-s d05055b
Update server/store/datastore/migration/024_unsanitize_org_and_user_n…
pat-s 0df49fa
Update server/store/datastore/migration/024_unsanitize_org_and_user_n…
pat-s 1c3f31e
chore: remove org test
pat-s 66da958
chore: update test
pat-s f2df9a2
chore: format
pat-s 3f297ad
chore: adjust more tests
pat-s 16f2469
Update server/store/datastore/org.go
pat-s a2d5946
chore: review
pat-s 8e0fc7a
import
pat-s 1771ce8
Merge branch 'main' into fix/sanitize-usernames-db
xoxys e638c62
clean header
pat-s 69c7d75
add tests checking case-sensitive DB duplicates
pat-s d354425
Merge branch 'main' into fix/sanitize-usernames-db
xoxys 73d4f19
restore toLower
pat-s aa726dc
improve user org handling
anbraten 31f31c4
fix test
anbraten 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,6 +202,7 @@ | |
"typecheck", | ||
"Typeflag", | ||
"unplugin", | ||
"unsanitize", | ||
"Upsert", | ||
"urfave", | ||
"usecase", | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -110,7 +110,7 @@ func HandleAuth(c *gin.Context) { | |||||
|
||||||
_forge, err := server.Config.Services.Manager.ForgeByID(forgeID) | ||||||
if err != nil { | ||||||
log.Error().Err(err).Msgf("Cannot get forge by id %d", forgeID) | ||||||
log.Error().Err(err).Msgf("cannot get forge by id %d", forgeID) | ||||||
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error") | ||||||
return | ||||||
} | ||||||
|
@@ -183,29 +183,34 @@ func HandleAuth(c *gin.Context) { | |||||
// create or set the user's organization if it isn't linked yet | ||||||
if user.OrgID == 0 { | ||||||
// check if an org with the same name exists already and assign it to the user if it does | ||||||
if org, err := _store.OrgFindByName(user.Login); err == nil && org != nil { | ||||||
// TODO: find the org by name and forgeID directly | ||||||
org, err := _store.OrgFindByName(user.Login) | ||||||
if err != nil && !errors.Is(err, types.RecordNotExist) { | ||||||
log.Error().Err(err).Msgf("cannot get org %s", user.Login) | ||||||
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error") | ||||||
return | ||||||
} | ||||||
|
||||||
// if an org with the same name exists and both are from the same forge => assign org to the user | ||||||
if err == nil && org != nil && org.ForgeID == forgeID { | ||||||
org.IsUser = true | ||||||
user.OrgID = org.ID | ||||||
|
||||||
if err := _store.OrgUpdate(org); err != nil { | ||||||
log.Error().Err(err).Msgf("on user creation, could not mark org as user") | ||||||
log.Error().Err(err).Msgf("on login, could not assign org to user") | ||||||
} | ||||||
} | ||||||
if err != nil && !errors.Is(err, types.RecordNotExist) { | ||||||
log.Error().Err(err).Msgf("cannot get org %s", user.Login) | ||||||
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error") | ||||||
return | ||||||
} | ||||||
|
||||||
if user.OrgID == 0 { | ||||||
// if no org with the same name exists => create a new org | ||||||
if user.OrgID == 0 || errors.Is(err, types.RecordNotExist) { | ||||||
org := &model.Org{ | ||||||
Name: user.Login, | ||||||
IsUser: true, | ||||||
Private: false, | ||||||
ForgeID: user.ForgeID, | ||||||
} | ||||||
if err := _store.OrgCreate(org); err != nil { | ||||||
log.Error().Err(err).Msgf("on user creation, could not create org for user") | ||||||
log.Error().Err(err).Msgf("on login, could not create org for user") | ||||||
} | ||||||
user.OrgID = org.ID | ||||||
} | ||||||
|
@@ -217,10 +222,18 @@ func HandleAuth(c *gin.Context) { | |||||
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error") | ||||||
return | ||||||
} | ||||||
|
||||||
// this should never happen, but if it does we should make admins aware of it | ||||||
if org != nil && org.ForgeID != forgeID { | ||||||
log.Error().Err(err).Msgf("user org is not from the same forge %s", user.Login) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error") | ||||||
return | ||||||
} | ||||||
|
||||||
if org != nil && org.Name != user.Login { | ||||||
org.Name = user.Login | ||||||
if err := _store.OrgUpdate(org); err != nil { | ||||||
log.Error().Err(err).Msgf("on user creation, could not mark org as user") | ||||||
log.Error().Err(err).Msgf("on login, could not mark org as user") | ||||||
} | ||||||
} | ||||||
} | ||||||
|
This file contains 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 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 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
66 changes: 66 additions & 0 deletions
66
server/store/datastore/migration/024_unsanitize_org_and_user_names.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2025 Woodpecker Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package migration | ||
|
||
import ( | ||
"fmt" | ||
|
||
"src.techknowlogick.com/xormigrate" | ||
"xorm.io/builder" | ||
"xorm.io/xorm" | ||
) | ||
|
||
var unSanitizeOrgAndUserNames = xormigrate.Migration{ | ||
ID: "unsanitize-org-and-user-names", | ||
MigrateSession: func(sess *xorm.Session) (err error) { | ||
type user struct { | ||
ID int64 `xorm:"pk autoincr 'id'"` | ||
Login string `xorm:"TEXT 'login'"` | ||
ForgeID int64 `xorm:"forge_id"` | ||
} | ||
|
||
type org struct { | ||
ID int64 `xorm:"pk autoincr 'id'"` | ||
Name string `xorm:"TEXT 'name'"` | ||
ForgeID int64 `xorm:"forge_id"` | ||
} | ||
|
||
if err := sess.Sync(new(user), new(org)); err != nil { | ||
return fmt.Errorf("sync new models failed: %w", err) | ||
} | ||
|
||
// get all users | ||
var users []*user | ||
anbraten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err := sess.Find(&users); err != nil { | ||
return fmt.Errorf("find all repos failed: %w", err) | ||
} | ||
|
||
for _, user := range users { | ||
userOrg := &org{} | ||
_, err := sess.Where("name = ? AND forge_id = ?", user.Login, user.ForgeID).Get(userOrg) | ||
if err != nil { | ||
return fmt.Errorf("getting org failed: %w", err) | ||
} | ||
|
||
if user.Login != userOrg.Name { | ||
anbraten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
userOrg.Name = user.Login | ||
if _, err := sess.Where(builder.Eq{"id": userOrg.ID}).Cols("Name").Update(userOrg); err != nil { | ||
return fmt.Errorf("updating org name failed: %w", err) | ||
} | ||
} | ||
} | ||
return nil | ||
}, | ||
} |
This file contains 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 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 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 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 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.
I've noticed this bug as the error was not handled by the
if
below as it was inline in the previous if.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.
Good catch