2020-11-29 00:37:58 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-11-29 00:37:58 +00:00
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2022-07-13 01:07:16 +00:00
|
|
|
"net"
|
2021-03-15 21:52:11 +00:00
|
|
|
"path/filepath"
|
2020-11-29 00:37:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-11-29 00:37:58 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2024-07-30 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-29 00:37:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigrateWhiteBlocklist(t *testing.T) {
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-03-15 21:52:11 +00:00
|
|
|
|
2022-08-16 02:22:25 +00:00
|
|
|
adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
|
|
|
|
nonAdminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})
|
2021-03-15 21:52:11 +00:00
|
|
|
|
2021-11-20 09:34:05 +00:00
|
|
|
setting.Migrations.AllowedDomains = "github.com"
|
|
|
|
setting.Migrations.AllowLocalNetworks = false
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, Init())
|
2020-11-29 00:37:58 +00:00
|
|
|
|
2021-03-15 21:52:11 +00:00
|
|
|
err := IsMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git", nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.Error(t, err)
|
2020-11-29 00:37:58 +00:00
|
|
|
|
2021-03-15 21:52:11 +00:00
|
|
|
err = IsMigrateURLAllowed("https://github.com/go-gitea/gitea.git", nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-29 00:37:58 +00:00
|
|
|
|
2021-03-18 13:58:47 +00:00
|
|
|
err = IsMigrateURLAllowed("https://gITHUb.com/go-gitea/gitea.git", nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-18 13:58:47 +00:00
|
|
|
|
2021-11-20 09:34:05 +00:00
|
|
|
setting.Migrations.AllowedDomains = ""
|
|
|
|
setting.Migrations.BlockedDomains = "github.com"
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, Init())
|
2020-11-29 00:37:58 +00:00
|
|
|
|
2021-03-15 21:52:11 +00:00
|
|
|
err = IsMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git", nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-29 00:37:58 +00:00
|
|
|
|
2021-03-15 21:52:11 +00:00
|
|
|
err = IsMigrateURLAllowed("https://github.com/go-gitea/gitea.git", nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.Error(t, err)
|
2021-03-15 21:52:11 +00:00
|
|
|
|
|
|
|
err = IsMigrateURLAllowed("https://10.0.0.1/go-gitea/gitea.git", nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.Error(t, err)
|
2021-03-08 13:10:17 +00:00
|
|
|
|
2021-03-15 21:52:11 +00:00
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, Init())
|
2021-03-15 21:52:11 +00:00
|
|
|
err = IsMigrateURLAllowed("https://10.0.0.1/go-gitea/gitea.git", nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-15 21:52:11 +00:00
|
|
|
|
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)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.Error(t, err)
|
2021-03-08 13:10:17 +00:00
|
|
|
|
|
|
|
setting.ImportLocalPaths = true
|
2021-03-15 21:52:11 +00:00
|
|
|
abs, err := filepath.Abs(".")
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-15 21:52:11 +00:00
|
|
|
|
|
|
|
err = IsMigrateURLAllowed(abs, adminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-15 21:52:11 +00:00
|
|
|
|
|
|
|
err = IsMigrateURLAllowed(abs, nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.Error(t, err)
|
2021-03-15 21:52:11 +00:00
|
|
|
|
|
|
|
nonAdminUser.AllowImportLocal = true
|
|
|
|
err = IsMigrateURLAllowed(abs, nonAdminUser)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-08 13:10:17 +00:00
|
|
|
|
|
|
|
setting.ImportLocalPaths = old
|
2020-11-29 00:37:58 +00:00
|
|
|
}
|
2022-07-13 01:07:16 +00:00
|
|
|
|
|
|
|
func TestAllowBlockList(t *testing.T) {
|
|
|
|
init := func(allow, block string, local bool) {
|
|
|
|
setting.Migrations.AllowedDomains = allow
|
|
|
|
setting.Migrations.BlockedDomains = block
|
|
|
|
setting.Migrations.AllowLocalNetworks = local
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, Init())
|
2022-07-13 01:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// default, allow all external, block none, no local networks
|
|
|
|
init("", "", false)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
|
|
|
|
require.Error(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
|
2022-07-13 01:07:16 +00:00
|
|
|
|
|
|
|
// allow all including local networks (it could lead to SSRF in production)
|
|
|
|
init("", "", true)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
|
|
|
|
require.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
|
2022-07-13 01:07:16 +00:00
|
|
|
|
|
|
|
// allow wildcard, block some subdomains. if the domain name is allowed, then the local network check is skipped
|
|
|
|
init("*.domain.com", "blocked.domain.com", false)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, checkByAllowBlockList("sub.domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
|
|
|
|
require.NoError(t, checkByAllowBlockList("sub.domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
|
|
|
|
require.Error(t, checkByAllowBlockList("blocked.domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
|
|
|
|
require.Error(t, checkByAllowBlockList("sub.other.com", []net.IP{net.ParseIP("1.2.3.4")}))
|
2022-07-13 01:07:16 +00:00
|
|
|
|
|
|
|
// allow wildcard (it could lead to SSRF in production)
|
|
|
|
init("*", "", false)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
|
|
|
|
require.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
|
2022-07-13 01:07:16 +00:00
|
|
|
|
|
|
|
// local network can still be blocked
|
|
|
|
init("*", "127.0.0.*", false)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
|
|
|
|
require.Error(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
|
2022-07-13 01:07:16 +00:00
|
|
|
|
|
|
|
// reset
|
|
|
|
init("", "", false)
|
|
|
|
}
|