Commit 7ee6266e authored by Nick Thomas's avatar Nick Thomas

Merge branch 'sh-extract-base-from-multipart-uploads' into 'master'

Fix uploads accelerated by Workhorse not working with paths [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!57889
parents 6e2ec73f f10ca3e5
...@@ -187,6 +187,7 @@ module ObjectStorage ...@@ -187,6 +187,7 @@ module ObjectStorage
hash[:TempPath] = workhorse_local_upload_path hash[:TempPath] = workhorse_local_upload_path
end end
hash[:FeatureFlagExtractBase] = Feature.enabled?(:workhorse_extract_filename_base)
hash[:MaximumSize] = maximum_size if maximum_size.present? hash[:MaximumSize] = maximum_size if maximum_size.present?
end end
end end
......
---
name: workhorse_extract_filename_base
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/57889
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/326379
milestone: '13.11'
type: development
group: group::source code
default_enabled: false
...@@ -441,6 +441,22 @@ RSpec.describe ObjectStorage do ...@@ -441,6 +441,22 @@ RSpec.describe ObjectStorage do
end end
end end
shared_examples 'extracts base filename' do
it "returns true for ExtractsBase" do
expect(subject[:FeatureFlagExtractBase]).to be true
end
context 'when workhorse_extract_filename_base is disabled' do
before do
stub_feature_flags(workhorse_extract_filename_base: false)
end
it "returns false for ExtractsBase" do
expect(subject[:FeatureFlagExtractBase]).to be false
end
end
end
shared_examples 'uses local storage' do shared_examples 'uses local storage' do
it_behaves_like 'returns the maximum size given' do it_behaves_like 'returns the maximum size given' do
it "returns temporary path" do it "returns temporary path" do
...@@ -502,6 +518,7 @@ RSpec.describe ObjectStorage do ...@@ -502,6 +518,7 @@ RSpec.describe ObjectStorage do
end end
it_behaves_like 'uses local storage' it_behaves_like 'uses local storage'
it_behaves_like 'extracts base filename'
end end
context 'when object storage is enabled' do context 'when object storage is enabled' do
...@@ -509,6 +526,8 @@ RSpec.describe ObjectStorage do ...@@ -509,6 +526,8 @@ RSpec.describe ObjectStorage do
allow(Gitlab.config.uploads.object_store).to receive(:enabled) { true } allow(Gitlab.config.uploads.object_store).to receive(:enabled) { true }
end end
it_behaves_like 'extracts base filename'
context 'when direct upload is enabled' do context 'when direct upload is enabled' do
before do before do
allow(Gitlab.config.uploads.object_store).to receive(:direct_upload) { true } allow(Gitlab.config.uploads.object_store).to receive(:direct_upload) { true }
......
...@@ -149,6 +149,8 @@ type Response struct { ...@@ -149,6 +149,8 @@ type Response struct {
ProcessLsifReferences bool ProcessLsifReferences bool
// The maximum accepted size in bytes of the upload // The maximum accepted size in bytes of the upload
MaximumSize int64 MaximumSize int64
// Feature flag used to determine whether to strip the multipart filename of any directories
FeatureFlagExtractBase bool
} }
// singleJoiningSlash is taken from reverseproxy.go:singleJoiningSlash // singleJoiningSlash is taken from reverseproxy.go:singleJoiningSlash
......
...@@ -63,6 +63,8 @@ type SaveFileOpts struct { ...@@ -63,6 +63,8 @@ type SaveFileOpts struct {
PresignedCompleteMultipart string PresignedCompleteMultipart string
// PresignedAbortMultipart is a presigned URL for AbortMultipartUpload // PresignedAbortMultipart is a presigned URL for AbortMultipartUpload
PresignedAbortMultipart string PresignedAbortMultipart string
// FeatureFlagExtractBase uses the base of the filename and strips directories
FeatureFlagExtractBase bool
} }
// UseWorkhorseClientEnabled checks if the options require direct access to object storage // UseWorkhorseClientEnabled checks if the options require direct access to object storage
...@@ -88,16 +90,17 @@ func GetOpts(apiResponse *api.Response) (*SaveFileOpts, error) { ...@@ -88,16 +90,17 @@ func GetOpts(apiResponse *api.Response) (*SaveFileOpts, error) {
} }
opts := SaveFileOpts{ opts := SaveFileOpts{
LocalTempPath: apiResponse.TempPath, FeatureFlagExtractBase: apiResponse.FeatureFlagExtractBase,
RemoteID: apiResponse.RemoteObject.ID, LocalTempPath: apiResponse.TempPath,
RemoteURL: apiResponse.RemoteObject.GetURL, RemoteID: apiResponse.RemoteObject.ID,
PresignedPut: apiResponse.RemoteObject.StoreURL, RemoteURL: apiResponse.RemoteObject.GetURL,
PresignedDelete: apiResponse.RemoteObject.DeleteURL, PresignedPut: apiResponse.RemoteObject.StoreURL,
PutHeaders: apiResponse.RemoteObject.PutHeaders, PresignedDelete: apiResponse.RemoteObject.DeleteURL,
UseWorkhorseClient: apiResponse.RemoteObject.UseWorkhorseClient, PutHeaders: apiResponse.RemoteObject.PutHeaders,
RemoteTempObjectID: apiResponse.RemoteObject.RemoteTempObjectID, UseWorkhorseClient: apiResponse.RemoteObject.UseWorkhorseClient,
Deadline: time.Now().Add(timeout), RemoteTempObjectID: apiResponse.RemoteObject.RemoteTempObjectID,
MaximumSize: apiResponse.MaximumSize, Deadline: time.Now().Add(timeout),
MaximumSize: apiResponse.MaximumSize,
} }
if opts.LocalTempPath != "" && opts.RemoteID != "" { if opts.LocalTempPath != "" && opts.RemoteID != "" {
......
...@@ -57,13 +57,18 @@ func TestSaveFileOptsLocalAndRemote(t *testing.T) { ...@@ -57,13 +57,18 @@ func TestSaveFileOptsLocalAndRemote(t *testing.T) {
func TestGetOpts(t *testing.T) { func TestGetOpts(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
multipart *api.MultipartUploadParams multipart *api.MultipartUploadParams
customPutHeaders bool customPutHeaders bool
putHeaders map[string]string putHeaders map[string]string
FeatureFlagExtractBase bool
}{ }{
{ {
name: "Single upload", name: "Single upload",
},
{
name: "Single upload w/ FeatureFlagExtractBase enabled",
FeatureFlagExtractBase: true,
}, { }, {
name: "Multipart upload", name: "Multipart upload",
multipart: &api.MultipartUploadParams{ multipart: &api.MultipartUploadParams{
...@@ -93,6 +98,7 @@ func TestGetOpts(t *testing.T) { ...@@ -93,6 +98,7 @@ func TestGetOpts(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
apiResponse := &api.Response{ apiResponse := &api.Response{
FeatureFlagExtractBase: test.FeatureFlagExtractBase,
RemoteObject: api.RemoteObject{ RemoteObject: api.RemoteObject{
Timeout: 10, Timeout: 10,
ID: "id", ID: "id",
...@@ -108,6 +114,7 @@ func TestGetOpts(t *testing.T) { ...@@ -108,6 +114,7 @@ func TestGetOpts(t *testing.T) {
opts, err := filestore.GetOpts(apiResponse) opts, err := filestore.GetOpts(apiResponse)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, apiResponse.FeatureFlagExtractBase, opts.FeatureFlagExtractBase)
require.Equal(t, apiResponse.TempPath, opts.LocalTempPath) require.Equal(t, apiResponse.TempPath, opts.LocalTempPath)
require.WithinDuration(t, deadline, opts.Deadline, time.Second) require.WithinDuration(t, deadline, opts.Deadline, time.Second)
require.Equal(t, apiResponse.RemoteObject.ID, opts.RemoteID) require.Equal(t, apiResponse.RemoteObject.ID, opts.RemoteID)
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"path/filepath"
"strings" "strings"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
...@@ -114,6 +115,10 @@ func (rew *rewriter) handleFilePart(ctx context.Context, name string, p *multipa ...@@ -114,6 +115,10 @@ func (rew *rewriter) handleFilePart(ctx context.Context, name string, p *multipa
filename := p.FileName() filename := p.FileName()
if opts.FeatureFlagExtractBase {
filename = filepath.Base(filename)
}
if strings.Contains(filename, "/") || filename == "." || filename == ".." { if strings.Contains(filename, "/") || filename == "." || filename == ".." {
return fmt.Errorf("illegal filename: %q", filename) return fmt.Errorf("illegal filename: %q", filename)
} }
......
...@@ -325,14 +325,20 @@ func TestInvalidFileNames(t *testing.T) { ...@@ -325,14 +325,20 @@ func TestInvalidFileNames(t *testing.T) {
defer os.RemoveAll(tempPath) defer os.RemoveAll(tempPath)
for _, testCase := range []struct { for _, testCase := range []struct {
filename string filename string
code int code int
FeatureFlagExtractBase bool
expectedPrefix string
}{ }{
{"foobar", 200}, // sanity check for test setup below {"foobar", 200, false, "foobar"}, // sanity check for test setup below
{"foo/bar", 500}, {"foo/bar", 500, false, ""},
{"/../../foobar", 500}, {"foo/bar", 200, true, "bar"},
{".", 500}, {"foo/bar/baz", 200, true, "baz"},
{"..", 500}, {"/../../foobar", 500, false, ""},
{"/../../foobar", 200, true, "foobar"},
{".", 500, false, ""},
{"..", 500, false, ""},
{"./", 500, false, ""},
} { } {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
...@@ -350,10 +356,12 @@ func TestInvalidFileNames(t *testing.T) { ...@@ -350,10 +356,12 @@ func TestInvalidFileNames(t *testing.T) {
apiResponse := &api.Response{TempPath: tempPath} apiResponse := &api.Response{TempPath: tempPath}
preparer := &DefaultPreparer{} preparer := &DefaultPreparer{}
opts, _, err := preparer.Prepare(apiResponse) opts, _, err := preparer.Prepare(apiResponse)
opts.FeatureFlagExtractBase = testCase.FeatureFlagExtractBase
require.NoError(t, err) require.NoError(t, err)
HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &SavedFileTracker{Request: httpRequest}, opts) HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &SavedFileTracker{Request: httpRequest}, opts)
require.Equal(t, testCase.code, response.Code) require.Equal(t, testCase.code, response.Code)
require.Equal(t, testCase.expectedPrefix, opts.TempFilePrefix)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment