-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add black list and white list support for migrating repositories #8040
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||||||
// Use of this source code is governed by a MIT-style | ||||||
// license that can be found in the LICENSE file. | ||||||
|
||||||
package matchlist | ||||||
|
||||||
import ( | ||||||
"github.com/gobwas/glob" | ||||||
) | ||||||
|
||||||
// Matchlist represents a black or white list | ||||||
type Matchlist struct { | ||||||
rules []string | ||||||
ruleGlobs []glob.Glob | ||||||
} | ||||||
|
||||||
// NewMatchlist creates a new black or white list | ||||||
func NewMatchlist(rules ...string) (*Matchlist, error) { | ||||||
list := Matchlist{ | ||||||
rules: rules, | ||||||
ruleGlobs: make([]glob.Glob, 0, len(rules)), | ||||||
} | ||||||
|
||||||
for _, rule := range list.rules { | ||||||
rg, err := glob.Compile(rule) | ||||||
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
I think that separators should work better if specified, so the tokenization is aware of the different parts of the glob. 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. Some case normalization should be used. For instance, if I black list: "google.com", someone could work around that by using "GOOGLE.COM". |
||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
list.ruleGlobs = append(list.ruleGlobs, rg) | ||||||
} | ||||||
|
||||||
return &list, nil | ||||||
} | ||||||
|
||||||
// Match will matches | ||||||
func (b *Matchlist) Match(u string) bool { | ||||||
for _, r := range b.ruleGlobs { | ||||||
if r.Match(u) { | ||||||
return true | ||||||
} | ||||||
} | ||||||
return false | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,15 @@ | |
package migrations | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/matchlist" | ||
"code.gitea.io/gitea/modules/migrations/base" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// MigrateOptions is equal to base.MigrateOptions | ||
|
@@ -23,8 +29,34 @@ func RegisterDownloaderFactory(factory base.DownloaderFactory) { | |
factories = append(factories, factory) | ||
} | ||
|
||
func isMigrateURLAllowed(remoteURL string) (bool, error) { | ||
u, err := url.Parse(remoteURL) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https") { | ||
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. Why check the protocol? This will let |
||
if len(setting.Migration.WhitelistedDomains) > 0 { | ||
if !whitelist.Match(u.Host) { | ||
return false, fmt.Errorf("Migrate from %v is not allowed", u.Host) | ||
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. Perhaps a translatable error should be better? |
||
} | ||
} else { | ||
if blacklist.Match(u.Host) { | ||
return false, fmt.Errorf("Migrate from %v is not allowed", u.Host) | ||
} | ||
} | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// MigrateRepository migrate repository according MigrateOptions | ||
func MigrateRepository(doer *models.User, ownerName string, opts base.MigrateOptions) (*models.Repository, error) { | ||
allowed, err := isMigrateURLAllowed(opts.RemoteURL) | ||
if !allowed { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
downloader base.Downloader | ||
uploader = NewGiteaLocalUploader(doer, ownerName, opts.Name) | ||
|
@@ -250,3 +282,23 @@ func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts | |
|
||
return nil | ||
} | ||
|
||
var ( | ||
whitelist *matchlist.Matchlist | ||
blacklist *matchlist.Matchlist | ||
) | ||
|
||
// Init migrations service | ||
func Init() error { | ||
var err error | ||
whitelist, err = matchlist.NewMatchlist(setting.Migration.WhitelistedDomains...) | ||
if err != nil { | ||
return fmt.Errorf("Init migration whitelist domains failed: %v", err) | ||
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. This error will show up when the instance starts, right? I mean, the admin needs to see the errors as soon as possible. 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. Yes. It will display when gitea start. |
||
} | ||
|
||
blacklist, err = matchlist.NewMatchlist(setting.Migration.BlacklistedDomains...) | ||
if err != nil { | ||
return fmt.Errorf("Init migration blacklist domains failed: %v", err) | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMigrateWhiteBlacklist(t *testing.T) { | ||
setting.Migration.WhitelistedDomains = []string{"github.com"} | ||
assert.NoError(t, Init()) | ||
|
||
allowed, err := isMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git") | ||
assert.False(t, allowed) | ||
assert.Error(t, err) | ||
|
||
allowed, err = isMigrateURLAllowed("https://github.com/go-gitea/gitea.git") | ||
assert.True(t, allowed) | ||
assert.NoError(t, err) | ||
|
||
setting.Migration.WhitelistedDomains = []string{} | ||
setting.Migration.BlacklistedDomains = []string{"github.com"} | ||
assert.NoError(t, Init()) | ||
|
||
allowed, err = isMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git") | ||
assert.True(t, allowed) | ||
assert.NoError(t, err) | ||
|
||
allowed, err = isMigrateURLAllowed("https://github.com/go-gitea/gitea.git") | ||
assert.False(t, allowed) | ||
assert.Error(t, err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package setting | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Migration represents migrations' settings | ||
var Migration = struct { | ||
WhitelistedDomains []string | ||
BlacklistedDomains []string | ||
}{ | ||
WhitelistedDomains: []string{}, | ||
BlacklistedDomains: []string{}, | ||
} | ||
|
||
// InitMigrationConfig represents load migration configurations | ||
func InitMigrationConfig() error { | ||
if err := Cfg.Section("migration").MapTo(&Migration); err != nil { | ||
return fmt.Errorf("Failed to map Migration settings: %v", err) | ||
} | ||
return nil | ||
} |
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.
Missing EOL