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

check content-type on handleAdd early #653

Merged
merged 6 commits into from
Dec 6, 2022
Merged
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
2 changes: 1 addition & 1 deletion cmd/estuary-shuttle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ func (s *Shuttle) ServeAPI() error {

content := e.Group("/content")
content.Use(s.AuthRequired(util.PermLevelUpload))
content.POST("/add", withUser(s.handleAdd))
content.POST("/add", util.WithMultipartFormDataChecker(withUser(s.handleAdd)))
content.POST("/add-car", util.WithContentLengthCheck(withUser(s.handleAddCar)))
content.GET("/read/:cont", withUser(s.handleReadContent))
content.POST("/importdeal", withUser(s.handleImportDeal))
Expand Down
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (s *Server) ServeAPI() error {

contmeta := e.Group("/content")
uploads := contmeta.Group("", s.AuthRequired(util.PermLevelUpload))
uploads.POST("/add", withUser(s.handleAdd))
uploads.POST("/add", util.WithMultipartFormDataChecker(withUser(s.handleAdd)))
uploads.POST("/add-ipfs", withUser(s.handleAddIpfs))
uploads.POST("/add-car", util.WithContentLengthCheck(withUser(s.handleAddCar)))
uploads.POST("/create", withUser(s.handleCreateContent))
Expand Down
28 changes: 22 additions & 6 deletions util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,33 @@ func (b Binder) Bind(i interface{}, c echo.Context) error {

func JSONPayloadMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().Header.Get("Content-Type") != "application/json" {
return &HttpError{
Code: http.StatusUnsupportedMediaType,
Reason: ERR_UNSUPPORTED_CONTENT_TYPE,
Details: "this endpoint only supports json payloads",
}
if err := checkContentType(c.Request().Header, "application/json"); err != nil {
return err
}
return next(c)
}
}

func WithMultipartFormDataChecker(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if err := checkContentType(c.Request().Header, "multipart/form-data"); err != nil {
return err
}
return next(c)
}
}

func checkContentType(header http.Header, expectedContentType string) error {
if header.Get("Content-Type") != expectedContentType {
return &HttpError{
Code: http.StatusUnsupportedMediaType,
Reason: ERR_UNSUPPORTED_CONTENT_TYPE,
Details: fmt.Sprintf("this endpoint only supports %s paylods", expectedContentType),
}
}
return nil
}

func DumpBlockstoreTo(ctx context.Context, tc trace.Tracer, from, to blockstore.Blockstore) error {
ctx, span := tc.Start(ctx, "blockstoreCopy")
defer span.End()
Expand Down