mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-02 21:29:34 +00:00
b06002f449
1. simplify "StorageRepo" code 2. simplify git.OpenRepository code 3. by the way, clean up some unused files in git test fixtures.
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRepoIsEmpty(t *testing.T) {
|
|
emptyRepo2Path := filepath.Join(testReposDir, "repo2_empty")
|
|
repo, err := OpenRepositoryLocal(emptyRepo2Path)
|
|
assert.NoError(t, err)
|
|
defer repo.Close()
|
|
isEmpty, err := repo.IsEmpty(t.Context())
|
|
assert.NoError(t, err)
|
|
assert.True(t, isEmpty)
|
|
}
|
|
|
|
// TestCloneRefusesRedirects ensures Clone never follows HTTP redirects, so a remote
|
|
// cannot redirect to an otherwise-blocked address (SSRF, e.g. during migration).
|
|
func TestCloneRefusesRedirects(t *testing.T) {
|
|
var targetHit atomic.Bool
|
|
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
targetHit.Store(true)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer target.Close()
|
|
|
|
redirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, target.URL+r.URL.Path, http.StatusFound)
|
|
}))
|
|
defer redirect.Close()
|
|
|
|
err := Clone(t.Context(), redirect.URL, filepath.Join(t.TempDir(), "dst"), CloneRepoOptions{})
|
|
assert.Error(t, err)
|
|
assert.False(t, targetHit.Load(), "git must not follow the redirect to the target")
|
|
}
|