Skip to content

Commit e15a5dc

Browse files
committed
only allow GET/HEAD methods
1 parent 53692a9 commit e15a5dc

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

catalogd/internal/storage/localdir.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"golang.org/x/sync/errgroup"
1818
"golang.org/x/sync/singleflight"
19+
"k8s.io/apimachinery/pkg/util/sets"
1920

2021
"github.com/operator-framework/operator-registry/alpha/declcfg"
2122
)
@@ -193,7 +194,17 @@ func (s *LocalDirV1) StorageServerHandler() http.Handler {
193194
if s.EnableQueryHandler {
194195
mux.HandleFunc(s.RootURL.JoinPath("{catalog}", "api", "v1", "query").Path, s.handleV1Query)
195196
}
196-
return mux
197+
allowedMethodsHandler := func(next http.Handler, allowedMethods ...string) http.Handler {
198+
allowedMethodSet := sets.New[string](allowedMethods...)
199+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
200+
if !allowedMethodSet.Has(r.Method) {
201+
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
202+
return
203+
}
204+
next.ServeHTTP(w, r)
205+
})
206+
}
207+
return allowedMethodsHandler(mux, http.MethodGet, http.MethodHead)
197208
}
198209

199210
func (s *LocalDirV1) handleV1All(w http.ResponseWriter, r *http.Request) {
@@ -242,7 +253,7 @@ func (s *LocalDirV1) handleV1Query(w http.ResponseWriter, r *http.Request) {
242253
return
243254
}
244255
indexReader := idx.Get(catalogFile, schema, pkg, name)
245-
serveJSONLinesQuery(w, indexReader)
256+
serveJSONLinesQuery(w, r, indexReader)
246257
}
247258

248259
func (s *LocalDirV1) catalogData(catalog string) (*os.File, os.FileInfo, error) {
@@ -277,8 +288,13 @@ func serveJSONLines(w http.ResponseWriter, r *http.Request, modTime time.Time, r
277288
http.ServeContent(w, r, "", modTime, rs)
278289
}
279290

280-
func serveJSONLinesQuery(w http.ResponseWriter, rs io.Reader) {
291+
func serveJSONLinesQuery(w http.ResponseWriter, r *http.Request, rs io.Reader) {
281292
w.Header().Add("Content-Type", "application/jsonl")
293+
// Copy the content of the reader to the response writer
294+
// only if it's a Get request
295+
if r.Method == http.MethodHead {
296+
return
297+
}
282298
_, err := io.Copy(w, rs)
283299
if err != nil {
284300
httpError(w, err)

0 commit comments

Comments
 (0)