mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-11 23:32:09 +01:00
4de909747b
Some checks are pending
/ release (push) Waiting to run
testing / test-remote-cacher (map[image:redis:7.2 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/valkey/valkey:7.2.5-alpine3.19 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
go-require lint is ignored for now Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4535 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com> Co-committed-by: TheFox0x7 <thefox0x7@gmail.com>
115 lines
4 KiB
Go
115 lines
4 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package migrations
|
|
|
|
import (
|
|
"net"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMigrateWhiteBlocklist(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
|
|
nonAdminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})
|
|
|
|
setting.Migrations.AllowedDomains = "github.com"
|
|
setting.Migrations.AllowLocalNetworks = false
|
|
require.NoError(t, Init())
|
|
|
|
err := IsMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git", nonAdminUser)
|
|
require.Error(t, err)
|
|
|
|
err = IsMigrateURLAllowed("https://github.com/go-gitea/gitea.git", nonAdminUser)
|
|
require.NoError(t, err)
|
|
|
|
err = IsMigrateURLAllowed("https://gITHUb.com/go-gitea/gitea.git", nonAdminUser)
|
|
require.NoError(t, err)
|
|
|
|
setting.Migrations.AllowedDomains = ""
|
|
setting.Migrations.BlockedDomains = "github.com"
|
|
require.NoError(t, Init())
|
|
|
|
err = IsMigrateURLAllowed("https://gitlab.com/gitlab/gitlab.git", nonAdminUser)
|
|
require.NoError(t, err)
|
|
|
|
err = IsMigrateURLAllowed("https://github.com/go-gitea/gitea.git", nonAdminUser)
|
|
require.Error(t, err)
|
|
|
|
err = IsMigrateURLAllowed("https://10.0.0.1/go-gitea/gitea.git", nonAdminUser)
|
|
require.Error(t, err)
|
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
|
require.NoError(t, Init())
|
|
err = IsMigrateURLAllowed("https://10.0.0.1/go-gitea/gitea.git", nonAdminUser)
|
|
require.NoError(t, err)
|
|
|
|
old := setting.ImportLocalPaths
|
|
setting.ImportLocalPaths = false
|
|
|
|
err = IsMigrateURLAllowed("/home/foo/bar/goo", adminUser)
|
|
require.Error(t, err)
|
|
|
|
setting.ImportLocalPaths = true
|
|
abs, err := filepath.Abs(".")
|
|
require.NoError(t, err)
|
|
|
|
err = IsMigrateURLAllowed(abs, adminUser)
|
|
require.NoError(t, err)
|
|
|
|
err = IsMigrateURLAllowed(abs, nonAdminUser)
|
|
require.Error(t, err)
|
|
|
|
nonAdminUser.AllowImportLocal = true
|
|
err = IsMigrateURLAllowed(abs, nonAdminUser)
|
|
require.NoError(t, err)
|
|
|
|
setting.ImportLocalPaths = old
|
|
}
|
|
|
|
func TestAllowBlockList(t *testing.T) {
|
|
init := func(allow, block string, local bool) {
|
|
setting.Migrations.AllowedDomains = allow
|
|
setting.Migrations.BlockedDomains = block
|
|
setting.Migrations.AllowLocalNetworks = local
|
|
require.NoError(t, Init())
|
|
}
|
|
|
|
// default, allow all external, block none, no local networks
|
|
init("", "", false)
|
|
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")}))
|
|
|
|
// allow all including local networks (it could lead to SSRF in production)
|
|
init("", "", true)
|
|
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")}))
|
|
|
|
// 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)
|
|
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")}))
|
|
|
|
// allow wildcard (it could lead to SSRF in production)
|
|
init("*", "", false)
|
|
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")}))
|
|
|
|
// local network can still be blocked
|
|
init("*", "127.0.0.*", false)
|
|
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")}))
|
|
|
|
// reset
|
|
init("", "", false)
|
|
}
|