2020-11-29 01:37:58 +01:00
// 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-15 21:52:11 +00:00
"path/filepath"
2020-11-29 01:37:58 +01:00
"testing"
2021-03-15 21:52:11 +00:00
"code.gitea.io/gitea/models"
2021-09-19 19:49:59 +08:00
"code.gitea.io/gitea/models/db"
2020-11-29 01:37:58 +01:00
"code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
)
func TestMigrateWhiteBlocklist ( t * testing . T ) {
2021-09-19 19:49:59 +08:00
assert . NoError ( t , db . PrepareTestDatabase ( ) )
2021-03-15 21:52:11 +00:00
2021-09-19 19:49:59 +08:00
adminUser := db . AssertExistsAndLoadBean ( t , & models . User { Name : "user1" } ) . ( * models . User )
nonAdminUser := db . AssertExistsAndLoadBean ( t , & models . User { Name : "user2" } ) . ( * models . User )
2021-03-15 21:52:11 +00:00
2020-11-29 01:37:58 +01:00
setting . Migrations . AllowedDomains = [ ] string { "github.com" }
assert . NoError ( t , Init ( ) )
2021-03-15 21:52:11 +00:00
err := IsMigrateURLAllowed ( "https://gitlab.com/gitlab/gitlab.git" , nonAdminUser )
2020-11-29 01:37:58 +01:00
assert . Error ( t , err )
2021-03-15 21:52:11 +00:00
err = IsMigrateURLAllowed ( "https://github.com/go-gitea/gitea.git" , nonAdminUser )
2020-11-29 01:37:58 +01:00
assert . NoError ( t , err )
2021-03-18 14:58:47 +01:00
err = IsMigrateURLAllowed ( "https://gITHUb.com/go-gitea/gitea.git" , nonAdminUser )
assert . NoError ( t , err )
2020-11-29 01:37:58 +01:00
setting . Migrations . AllowedDomains = [ ] string { }
setting . Migrations . BlockedDomains = [ ] string { "github.com" }
assert . NoError ( t , Init ( ) )
2021-03-15 21:52:11 +00:00
err = IsMigrateURLAllowed ( "https://gitlab.com/gitlab/gitlab.git" , nonAdminUser )
2020-11-29 01:37:58 +01:00
assert . NoError ( t , err )
2021-03-15 21:52:11 +00:00
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 01:37:58 +01:00
assert . Error ( t , err )
2021-03-08 13:10:17 +00:00
2021-03-15 21:52:11 +00:00
setting . Migrations . AllowLocalNetworks = true
err = IsMigrateURLAllowed ( "https://10.0.0.1/go-gitea/gitea.git" , nonAdminUser )
assert . NoError ( t , err )
2021-03-08 13:10:17 +00:00
old := setting . ImportLocalPaths
setting . ImportLocalPaths = false
2021-03-15 21:52:11 +00:00
err = IsMigrateURLAllowed ( "/home/foo/bar/goo" , adminUser )
2021-03-08 13:10:17 +00:00
assert . Error ( t , err )
setting . ImportLocalPaths = true
2021-03-15 21:52:11 +00:00
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 13:10:17 +00:00
assert . NoError ( t , err )
setting . ImportLocalPaths = old
2020-11-29 01:37:58 +01:00
}