Skip to content

Commit

Permalink
fix: filter out filename path (#2489)
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol authored Sep 13, 2021
1 parent c81f90f commit 3116f9a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/api/bzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"mime"
"net/http"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -412,10 +413,10 @@ func (s *server) serveManifestEntry(
manifestEntry manifest.Entry,
etag bool,
) {

additionalHeaders := http.Header{}
mtdt := manifestEntry.Metadata()
if fname, ok := mtdt[manifest.EntryMetadataFilenameKey]; ok {
fname = filepath.Base(fname) // only keep the file name
additionalHeaders["Content-Disposition"] =
[]string{fmt.Sprintf("inline; filename=\"%s\"", fname)}
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/api/bzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,38 @@ func TestBzzFiles(t *testing.T) {
}
})

t.Run("filter out filename path", func(t *testing.T) {
fileName := "my-pictures.jpeg"
fileNameWithPath := "../../" + fileName

var resp api.BzzUploadResponse

_ = jsonhttptest.Request(t, client, http.MethodPost,
fileUploadResource+"?name="+fileNameWithPath, http.StatusCreated,
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)),
jsonhttptest.WithRequestHeader("Content-Type", "image/jpeg; charset=utf-8"),
jsonhttptest.WithUnmarshalJSONResponse(&resp),
)

rootHash := resp.Reference.String()
rcvdHeader := jsonhttptest.Request(t, client, http.MethodGet,
fileDownloadResource(rootHash), http.StatusOK,
jsonhttptest.WithExpectedResponse(simpleData),
)
cd := rcvdHeader.Get("Content-Disposition")
_, params, err := mime.ParseMediaType(cd)
if err != nil {
t.Fatal(err)
}
if params["filename"] != fileName {
t.Fatalf("want filename %s, got %s", fileName, params["filename"])
}
if rcvdHeader.Get("Content-Type") != "image/jpeg; charset=utf-8" {
t.Fatal("Invalid content type detected")
}
})

t.Run("check-content-type-detection", func(t *testing.T) {
fileName := "my-pictures.jpeg"
rootHash := "4f9146b3813ccbd7ce45a18be23763d7e436ab7a3982ef39961c6f3cd4da1dcf"
Expand Down

0 comments on commit 3116f9a

Please sign in to comment.