Skip to content

Commit

Permalink
fix: allow slashes in local bucket upload/download URLs
Browse files Browse the repository at this point in the history
* fix: Remove encoding and add CORS for upload/download URL handlers.
* fix: allow permissive CORS for local upload/download URLs
  • Loading branch information
tjholm authored Apr 10, 2024
1 parent 5c589ac commit 3dd6d0d
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pkg/cloud/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -319,11 +318,13 @@ func (r *LocalStorageService) PreSignUrl(ctx context.Context, req *storagepb.Sto

var address string = ""

// XXX: Do not URL encode keys (path needs to be preserved)
// TODO: May need to re-write slashes to a non-escapable character format
switch req.Operation {
case storagepb.StoragePreSignUrlRequest_WRITE:
address = fmt.Sprintf("http://localhost:%d/write/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, url.PathEscape(req.Key))
address = fmt.Sprintf("http://localhost:%d/write/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, req.Key)
case storagepb.StoragePreSignUrlRequest_READ:
address = fmt.Sprintf("http://localhost:%d/read/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, url.PathEscape(req.Key))
address = fmt.Sprintf("http://localhost:%d/read/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, req.Key)
}

if address == "" {
Expand All @@ -340,6 +341,21 @@ type StorageOptions struct {
SecretKey string
}

func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, PUT")
w.Header().Set("Access-Control-Allow-Headers", "*")

if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}

next.ServeHTTP(w, r)
})
}

func NewLocalStorageService(opts StorageOptions) (*LocalStorageService, error) {
var err error

Expand All @@ -354,8 +370,9 @@ func NewLocalStorageService(opts StorageOptions) (*LocalStorageService, error) {
}

router := mux.NewRouter()
router.Use(corsMiddleware)

router.HandleFunc("/read/{bucket}/{file}", func(w http.ResponseWriter, r *http.Request) {
router.HandleFunc("/read/{bucket}/{file:.*}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bucket := vars["bucket"]
file := vars["file"]
Expand All @@ -379,7 +396,7 @@ func NewLocalStorageService(opts StorageOptions) (*LocalStorageService, error) {
}
})

router.HandleFunc("/write/{bucket}/{file}", func(w http.ResponseWriter, r *http.Request) {
router.HandleFunc("/write/{bucket}/{file:.*}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bucket := vars["bucket"]
file := vars["file"]
Expand Down

0 comments on commit 3dd6d0d

Please sign in to comment.