Skip to content
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

Add Allow-/Block-List for Migrate & Mirrors #13610

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7a083b9
add black list and white list support for migrating repositories
lunny Aug 30, 2019
3f2b34b
fix fmt
lunny Aug 30, 2019
ad5a226
fix lint
lunny Aug 30, 2019
7280964
fix vendor
lunny Aug 30, 2019
cdff51c
fix modules.txt
lunny Sep 7, 2019
ab362e5
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 17, 2020
ec4dbc8
clean diff
6543 Nov 17, 2020
770dab6
specify log message
6543 Nov 17, 2020
05e45bf
use blocklist/allowlist
6543 Nov 17, 2020
a078736
allways use lowercase to match url
6543 Nov 17, 2020
dec70f1
Apply allow/block
6543 Nov 17, 2020
d2c1619
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 18, 2020
840fc85
Settings: use existing "migrations" section
6543 Nov 21, 2020
af32a09
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 21, 2020
9f5e0de
convert domains lower case
6543 Nov 23, 2020
3ede511
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 23, 2020
37f45f4
dont store unused value
6543 Nov 23, 2020
e0934b8
Block private addresses for migration by default
6543 Nov 23, 2020
43982b4
fix lint
6543 Nov 23, 2020
9cd404a
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 23, 2020
6725fd5
use proposed-upstream func to detect private IP addr
6543 Nov 24, 2020
6b8ecc4
a nit
6543 Nov 24, 2020
6ef7267
add own error for blocked migration, add tests, imprufe api
6543 Nov 24, 2020
c66cf83
fix test
6543 Nov 24, 2020
3b57ffc
fix-if-localhost-is-ipv4
6543 Nov 24, 2020
027f6f1
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 24, 2020
ab53576
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 26, 2020
8372dd1
rename error & error message
6543 Nov 26, 2020
b9dda50
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 26, 2020
aa8ec6f
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 28, 2020
bb5ce58
rename setting options
6543 Nov 28, 2020
9815e9d
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 28, 2020
e13cd15
Apply suggestions from code review
zeripath Nov 28, 2020
6602023
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 28, 2020
870ca5b
Merge branch 'master' into allow-block_list_migrate-mirror_8040
lunny Nov 28, 2020
e091333
Merge branch 'master' into allow-block_list_migrate-mirror_8040
zeripath Nov 28, 2020
7850e5e
Merge branch 'master' into allow-block_list_migrate-mirror_8040
6543 Nov 28, 2020
3c67197
Merge branch 'master' into allow-block_list_migrate-mirror_8040
techknowlogick Nov 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1212,3 +1212,11 @@ STORAGE_TYPE = local
;MINIO_LOCATION = us-east-1
; Minio enabled ssl only available when STORAGE_TYPE is `minio`
;MINIO_USE_SSL = false

[migration]
; Whitelist for migrating, default is blank. Blank means everything will be allowed.
6543 marked this conversation as resolved.
Show resolved Hide resolved
; Multiple domains could be separated by commas.
ALLOWLISTED_DOMAINS =
; Blacklist for migrating, default is blank. Multiple domains could be separated by commas.
6543 marked this conversation as resolved.
Show resolved Hide resolved
; When WHITELISTED_DOMAINS is not blank, this option will be ignored.
6543 marked this conversation as resolved.
Show resolved Hide resolved
BLOCKLISTED_DOMAINS =
5 changes: 5 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,11 @@ MINIO_USE_SSL = false

And used by `[attachment]`, `[lfs]` and etc. as `STORAGE_TYPE`.

## Migraions (`migration`)

- `ALLOWLISTED_DOMAINS`: ****: Domains whitelist for migrating repositories, default is blank. It means everything will be allowed. Multiple domains could be separated by commas.
6543 marked this conversation as resolved.
Show resolved Hide resolved
- `BLOCKLISTED_DOMAINS`: ****: Domains blacklist for migrating repositories, default is blank. Multiple domains could be separated by commas. When `ALLOWLISTED_DOMAINS` is not blank, this option will be ignored.
6543 marked this conversation as resolved.
Show resolved Hide resolved

## Other (`other`)

- `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer.
Expand Down
5 changes: 5 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ MINIO_USE_SSL = false

然后你在 `[attachment]`, `[lfs]` 等中可以把这个名字用作 `STORAGE_TYPE` 的值。

## Migraions (`migration`)

- `ALLOWLISTED_DOMAINS`: ****: 迁移仓库的域名白名单,默认为空,表示允许从任意域名迁移仓库,多个域名用逗号分隔。
- `BLOCKLISTED_DOMAINS`: ****: 迁移仓库的域名黑名单,默认为空,多个域名用逗号分隔。如果 `ALLOWLISTED_DOMAINS` 不为空,此选项将会被忽略。

## Other (`other`)

- `SHOW_FOOTER_BRANDING`: 为真则在页面底部显示Gitea的字样。
Expand Down
48 changes: 48 additions & 0 deletions modules/matchlist/matchlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 (
"strings"

"github.com/gobwas/glob"
)

// Matchlist represents a black or white list
6543 marked this conversation as resolved.
Show resolved Hide resolved
type Matchlist struct {
rules []string
ruleGlobs []glob.Glob
}

// NewMatchlist creates a new black or white list
6543 marked this conversation as resolved.
Show resolved Hide resolved
func NewMatchlist(rules ...string) (*Matchlist, error) {
for i := range rules {
rules[i] = strings.ToLower(rules[i])
}
list := Matchlist{
rules: rules,
ruleGlobs: make([]glob.Glob, 0, len(rules)),
}

for _, rule := range list.rules {
rg, err := glob.Compile(rule)
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
}
50 changes: 49 additions & 1 deletion modules/migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ package migrations
import (
"context"
"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"
)
Expand All @@ -27,12 +30,37 @@ func RegisterDownloaderFactory(factory base.DownloaderFactory) {
factories = append(factories, factory)
}

func isMigrateURLAllowed(remoteURL string) (bool, error) {
u, err := url.Parse(strings.ToLower(remoteURL))
if err != nil {
return false, err
}

if strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https") {
if len(setting.Migration.AllowlistedDomains) > 0 {
if !allowlist.Match(u.Host) {
return false, fmt.Errorf("Migrate from %v is not allowed", u.Host)
}
} else {
if blocklist.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(ctx context.Context, doer *models.User, ownerName string, opts base.MigrateOptions) (*models.Repository, error) {
allowed, err := isMigrateURLAllowed(opts.CloneAddr)
if !allowed {
return nil, err
}

var (
downloader base.Downloader
uploader = NewGiteaLocalUploader(ctx, doer, ownerName, opts.RepoName)
err error
)

for _, factory := range factories {
Expand Down Expand Up @@ -308,3 +336,23 @@ func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts

return nil
}

var (
allowlist *matchlist.Matchlist
blocklist *matchlist.Matchlist
)

// Init migrations service
func Init() error {
var err error
allowlist, err = matchlist.NewMatchlist(setting.Migration.AllowlistedDomains...)
if err != nil {
return fmt.Errorf("init migration allowlist domains failed: %v", err)
}

blocklist, err = matchlist.NewMatchlist(setting.Migration.BlocklistedDomains...)
if err != nil {
return fmt.Errorf("init migration blocklist domains failed: %v", err)
}
return nil
}
38 changes: 38 additions & 0 deletions modules/migrations/migrate_test.go
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 TestMigrateWhiteBlocklist(t *testing.T) {
setting.Migration.AllowlistedDomains = []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.AllowlistedDomains = []string{}
setting.Migration.BlocklistedDomains = []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)
}
26 changes: 26 additions & 0 deletions modules/setting/migrate.go
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 {
AllowlistedDomains []string
BlocklistedDomains []string
}{
AllowlistedDomains: []string{},
BlocklistedDomains: []string{},
}

// InitMigrationConfig represents load migration configurations
func InitMigrationConfig() error {
6543 marked this conversation as resolved.
Show resolved Hide resolved
if err := Cfg.Section("migration").MapTo(&Migration); err != nil {
return fmt.Errorf("Failed to map Migration settings: %v", err)
}
return nil
}
4 changes: 4 additions & 0 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/external"
repo_migrations "code.gitea.io/gitea/modules/migrations"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/options"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -201,6 +202,9 @@ func GlobalInit(ctx context.Context) {
if err := task.Init(); err != nil {
log.Fatal("Failed to initialize task scheduler: %v", err)
}
if err := repo_migrations.Init(); err != nil {
log.Fatal("Failed to initialize repository migrations: %v", err)
}
eventsource.GetManager().Init()

if setting.EnableSQLite3 {
Expand Down