MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / newRequest

Method newRequest

handlers/processing/handler.go:97–148  ·  view source on GitHub ↗

newRequest extracts image url and processing options from request URL and verifies them

(req *http.Request)

Source from the content-addressed store, hash-verified

95
96// newRequest extracts image url and processing options from request URL and verifies them
97func (h *Handler) newRequest(req *http.Request) (*request, *server.Error) {
98 // let's extract signature and valid request path from a request
99 path, signature, err := handlers.SplitPathSignature(req)
100 if err != nil {
101 return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategoryPathParsing)
102 }
103
104 // verify the signature (if any)
105 if err = h.Security().VerifySignature(req.Context(), signature, path); err != nil {
106 return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategorySecurity)
107 }
108
109 // parse image url and processing options
110 features := h.ClientFeaturesDetector().Features(req.Header)
111 o, imageURL, err := h.OptionsParser().ParsePath(req.Context(), path, &features)
112 if err != nil {
113 return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategoryPathParsing)
114 }
115
116 // get image origin and create monitoring meta object
117 imageOrigin := monitoring.MetaURLOrigin(imageURL)
118
119 mm := monitoring.Meta{
120 monitoring.MetaSourceImageURL: imageURL,
121 monitoring.MetaSourceImageOrigin: imageOrigin,
122 monitoring.MetaOptions: o.Map(),
123 }
124
125 // set error reporting and monitoring context
126 errorreport.SetMetadata(req, "Source Image URL", imageURL)
127 errorreport.SetMetadata(req, "Source Image Origin", imageOrigin)
128 errorreport.SetMetadata(req, "Options", o.NestedMap())
129
130 h.Monitoring().SetMetadata(req.Context(), mm)
131
132 // verify that image URL came from the valid source
133 err = h.Security().VerifySourceURL(imageURL)
134 if err != nil {
135 return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategorySecurity)
136 }
137
138 return &request{
139 HandlerContext: h,
140
141 imageURL: imageURL,
142 path: path,
143 opts: o,
144 features: &features,
145 monitoringMeta: mm,
146 req: req,
147 }, nil
148}

Callers 1

ExecuteMethod · 0.95

Calls 15

SplitPathSignatureFunction · 0.92
NewErrorFunction · 0.92
WrapFunction · 0.92
MetaURLOriginFunction · 0.92
SetMetadataFunction · 0.92
VerifySignatureMethod · 0.80
FeaturesMethod · 0.80
ParsePathMethod · 0.80
MapMethod · 0.80
NestedMapMethod · 0.80
VerifySourceURLMethod · 0.80
SecurityMethod · 0.65

Tested by

no test coverage detected