2020-11-29 06:07:58 +05:30
|
|
|
// 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 (
|
2021-03-16 03:22:11 +05:30
|
|
|
"path/filepath"
|
2020-11-29 06:07:58 +05:30
|
|
|
"testing"
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2020-11-29 06:07:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigrateWhiteBlocklist(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-03-16 03:22:11 +05:30
|
|
|
|
2021-11-16 14:23:21 +05:30
|
|
|
adminUser := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User)
|
|
|
|
nonAdminUser := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User)
|
2021-03-16 03:22:11 +05:30
|
|
|
|
2021-11-20 15:04:05 +05:30
|
|
|
setting.Migrations.AllowedDomains = "github.com"
|
|
|
|
setting.Migrations.AllowLocalNetworks = false
|
2020-11-29 06:07:58 +05:30
|
|
|
assert.NoError(t, Init())
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
err := IsMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git", nonAdminUser)
|
2020-11-29 06:07:58 +05:30
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
err = IsMigrateURLAllowed("https://github.com/go-gitea/gitea.git", nonAdminUser)
|
2020-11-29 06:07:58 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-18 19:28:47 +05:30
|
|
|
err = IsMigrateURLAllowed("https://gITHUb.com/go-gitea/gitea.git", nonAdminUser)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-11-20 15:04:05 +05:30
|
|
|
setting.Migrations.AllowedDomains = ""
|
|
|
|
setting.Migrations.BlockedDomains = "github.com"
|
2020-11-29 06:07:58 +05:30
|
|
|
assert.NoError(t, Init())
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
err = IsMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git", nonAdminUser)
|
2020-11-29 06:07:58 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
err = IsMigrateURLAllowed("https://github.com/go-gitea/gitea.git", nonAdminUser)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = IsMigrateURLAllowed("https://10.0.0.1/go-gitea/gitea.git", nonAdminUser)
|
2020-11-29 06:07:58 +05:30
|
|
|
assert.Error(t, err)
|
2021-03-08 18:40:17 +05:30
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
2021-11-20 15:04:05 +05:30
|
|
|
assert.NoError(t, Init())
|
2021-03-16 03:22:11 +05:30
|
|
|
err = IsMigrateURLAllowed("https://10.0.0.1/go-gitea/gitea.git", nonAdminUser)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-08 18:40:17 +05:30
|
|
|
old := setting.ImportLocalPaths
|
|
|
|
setting.ImportLocalPaths = false
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
err = IsMigrateURLAllowed("/home/foo/bar/goo", adminUser)
|
2021-03-08 18:40:17 +05:30
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
setting.ImportLocalPaths = true
|
2021-03-16 03:22:11 +05:30
|
|
|
abs, err := filepath.Abs(".")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = IsMigrateURLAllowed(abs, adminUser)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = IsMigrateURLAllowed(abs, nonAdminUser)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
nonAdminUser.AllowImportLocal = true
|
|
|
|
err = IsMigrateURLAllowed(abs, nonAdminUser)
|
2021-03-08 18:40:17 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
setting.ImportLocalPaths = old
|
2020-11-29 06:07:58 +05:30
|
|
|
}
|