Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct content-type for ipfs get #2673

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion commands/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,32 @@ const (
)

var mimeTypes = map[string]string{
cmds.Gzip: "application/gzip",
cmds.Protobuf: "application/protobuf",
cmds.JSON: "application/json",
cmds.Tar: "application/x-tar",
cmds.XML: "application/xml",
cmds.Text: "text/plain",
}

var xssSafeMimeTypes = []string{
mimeTypes[cmds.Gzip],
mimeTypes[cmds.JSON],
mimeTypes[cmds.Tar],
mimeTypes[cmds.Text],
mimeTypes[cmds.XML],
mimeTypes[cmds.Protobuf],
}

func xssSafeMimeType(mime string) bool {
for _, t := range xssSafeMimeTypes {
if t == mime {
return true
}
}
return false
}

type ServerConfig struct {
// Headers is an optional map of headers that is written out.
Headers map[string][]string
Expand Down Expand Up @@ -251,7 +271,9 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
if _, ok := res.Output().(io.Reader); ok {
// set streams output type to text to avoid issues with browsers rendering
// html pages on priveleged api ports
mime = "text/plain"
if !xssSafeMimeType(mime) {
mime = mimeTypes[cmds.Text]
}
h.Set(streamHeader, "1")
}

Expand Down
5 changes: 3 additions & 2 deletions commands/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ type EncodingType string

// Supported EncodingType constants.
const (
Gzip = "gzip"
JSON = "json"
XML = "xml"
Protobuf = "protobuf"
Tar = "tar"
Text = "text"
// TODO: support more encoding types
XML = "xml"
)

func marshalJson(value interface{}) (io.Reader, error) {
Expand Down
6 changes: 6 additions & 0 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ may also specify the level of compression by specifying '-l=<1-9>'.
return
}

// set the correct mime type for tar stream
req.SetOption(cmds.EncShort, cmds.Tar)
if cmplvl != gzip.NoCompression {
req.SetOption(cmds.EncShort, cmds.Gzip)
}

archive, _, _ := req.Option("archive").Bool()
reader, err := uarchive.DagArchive(ctx, dn, p.String(), node.DAG, archive, cmplvl)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions test/sharness/t0090-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ test_get_fail() {
'
}

test_expect_success "ipfs get response has the correct content-type" '
curl -I "http://localhost:$PORT_API/api/v0/get?arg=$HASH" | grep "^Content-Type: application/x-tar"
'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a negative test in here, too.

# should work offline
test_get_cmd

Expand Down