2020-12-02 10:09:37 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-09 08:08:25 -04:00
|
|
|
"io"
|
2020-12-02 10:09:37 -05:00
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-12-06 10:14:39 -05:00
|
|
|
"regexp"
|
2020-12-02 10:09:37 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gitlab.com/gitlab-org/labkit/log"
|
2021-12-06 10:14:39 -05:00
|
|
|
|
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/testhelper"
|
2020-12-02 10:09:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeniedLfsDownload(t *testing.T) {
|
|
|
|
contentFilename := "b68143e6463773b1b6c6fd009a76c32aeec041faff32ba2ed42fd7f708a17f80"
|
|
|
|
url := fmt.Sprintf("gitlab-lfs/objects/%s", contentFilename)
|
|
|
|
|
|
|
|
prepareDownloadDir(t)
|
|
|
|
deniedXSendfileDownload(t, contentFilename, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllowedLfsDownload(t *testing.T) {
|
|
|
|
contentFilename := "b68143e6463773b1b6c6fd009a76c32aeec041faff32ba2ed42fd7f708a17f80"
|
|
|
|
url := fmt.Sprintf("gitlab-lfs/objects/%s", contentFilename)
|
|
|
|
|
|
|
|
prepareDownloadDir(t)
|
|
|
|
allowedXSendfileDownload(t, contentFilename, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func allowedXSendfileDownload(t *testing.T, contentFilename string, filePath string) {
|
|
|
|
contentPath := path.Join(cacheDir, contentFilename)
|
|
|
|
prepareDownloadDir(t)
|
|
|
|
|
|
|
|
// Prepare test server and backend
|
2021-12-06 10:14:39 -05:00
|
|
|
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-02 10:09:37 -05:00
|
|
|
log.WithFields(log.Fields{"method": r.Method, "url": r.URL}).Info("UPSTREAM")
|
|
|
|
|
|
|
|
require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type"))
|
|
|
|
|
|
|
|
w.Header().Set("X-Sendfile", contentPath)
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, contentFilename))
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
ws := startWorkhorseServer(ts.URL)
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
require.NoError(t, os.MkdirAll(cacheDir, 0755))
|
|
|
|
contentBytes := []byte("content")
|
2022-06-09 08:08:25 -04:00
|
|
|
require.NoError(t, os.WriteFile(contentPath, contentBytes, 0644))
|
2020-12-02 10:09:37 -05:00
|
|
|
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/%s", ws.URL, filePath))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
requireAttachmentName(t, resp, contentFilename)
|
|
|
|
|
2022-06-09 08:08:25 -04:00
|
|
|
actual, err := io.ReadAll(resp.Body)
|
2020-12-02 10:09:37 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, resp.Body.Close())
|
|
|
|
|
|
|
|
require.Equal(t, actual, contentBytes, "response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath string) {
|
|
|
|
prepareDownloadDir(t)
|
|
|
|
|
|
|
|
// Prepare test server and backend
|
2021-12-06 10:14:39 -05:00
|
|
|
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-02 10:09:37 -05:00
|
|
|
log.WithFields(log.Fields{"method": r.Method, "url": r.URL}).Info("UPSTREAM")
|
|
|
|
|
|
|
|
require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type"))
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, contentFilename))
|
|
|
|
w.WriteHeader(200)
|
|
|
|
fmt.Fprint(w, "Denied")
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
ws := startWorkhorseServer(ts.URL)
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/%s", ws.URL, filePath))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
requireAttachmentName(t, resp, contentFilename)
|
|
|
|
|
2022-06-09 08:08:25 -04:00
|
|
|
actual, err := io.ReadAll(resp.Body)
|
2020-12-02 10:09:37 -05:00
|
|
|
require.NoError(t, err, "read body")
|
|
|
|
require.NoError(t, resp.Body.Close())
|
|
|
|
|
|
|
|
require.Equal(t, []byte("Denied"), actual, "response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireAttachmentName(t *testing.T, resp *http.Response, filename string) {
|
|
|
|
mediaType, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "attachment", mediaType)
|
|
|
|
require.Equal(t, filename, params["filename"], "filename")
|
|
|
|
}
|