mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Use hostmatcher to replace matchlist, improve security (#17605)
				
					
				
			Use hostmacher to replace matchlist. And we introduce a better DialContext to do a full host/IP check, otherwise the attackers can still bypass the allow/block list by a 302 redirection.
This commit is contained in:
		@@ -6,7 +6,6 @@ package migrations
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
@@ -18,8 +17,6 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	base "code.gitea.io/gitea/modules/migration"
 | 
			
		||||
	"code.gitea.io/gitea/modules/proxy"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/structs"
 | 
			
		||||
 | 
			
		||||
	"github.com/xanzy/go-gitlab"
 | 
			
		||||
@@ -77,16 +74,11 @@ type GitlabDownloader struct {
 | 
			
		||||
//   Use either a username/password, personal token entered into the username field, or anonymous/public access
 | 
			
		||||
//   Note: Public access only allows very basic access
 | 
			
		||||
func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, password, token string) (*GitlabDownloader, error) {
 | 
			
		||||
	gitlabClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(&http.Client{
 | 
			
		||||
		Transport: &http.Transport{
 | 
			
		||||
			TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Migrations.SkipTLSVerify},
 | 
			
		||||
			Proxy:           proxy.Proxy(),
 | 
			
		||||
		},
 | 
			
		||||
	}))
 | 
			
		||||
	gitlabClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(NewMigrationHTTPClient()))
 | 
			
		||||
	// Only use basic auth if token is blank and password is NOT
 | 
			
		||||
	// Basic auth will fail with empty strings, but empty token will allow anonymous public API usage
 | 
			
		||||
	if token == "" && password != "" {
 | 
			
		||||
		gitlabClient, err = gitlab.NewBasicAuthClient(username, password, gitlab.WithBaseURL(baseURL))
 | 
			
		||||
		gitlabClient, err = gitlab.NewBasicAuthClient(username, password, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(NewMigrationHTTPClient()))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -300,12 +292,7 @@ func (g *GitlabDownloader) convertGitlabRelease(rel *gitlab.Release) *base.Relea
 | 
			
		||||
		PublisherName:   rel.Author.Username,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	httpClient := &http.Client{
 | 
			
		||||
		Transport: &http.Transport{
 | 
			
		||||
			TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Migrations.SkipTLSVerify},
 | 
			
		||||
			Proxy:           proxy.Proxy(),
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	httpClient := NewMigrationHTTPClient()
 | 
			
		||||
 | 
			
		||||
	for k, asset := range rel.Assets.Links {
 | 
			
		||||
		r.Assets = append(r.Assets, &base.ReleaseAsset{
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user