processing_handler.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "slices"
  9. "strconv"
  10. "strings"
  11. "time"
  12. log "github.com/sirupsen/logrus"
  13. "golang.org/x/sync/semaphore"
  14. "github.com/imgproxy/imgproxy/v3/config"
  15. "github.com/imgproxy/imgproxy/v3/cookies"
  16. "github.com/imgproxy/imgproxy/v3/errorreport"
  17. "github.com/imgproxy/imgproxy/v3/etag"
  18. "github.com/imgproxy/imgproxy/v3/ierrors"
  19. "github.com/imgproxy/imgproxy/v3/imagedata"
  20. "github.com/imgproxy/imgproxy/v3/imagefetcher"
  21. "github.com/imgproxy/imgproxy/v3/imagetype"
  22. "github.com/imgproxy/imgproxy/v3/imath"
  23. "github.com/imgproxy/imgproxy/v3/metrics"
  24. "github.com/imgproxy/imgproxy/v3/metrics/stats"
  25. "github.com/imgproxy/imgproxy/v3/options"
  26. "github.com/imgproxy/imgproxy/v3/processing"
  27. "github.com/imgproxy/imgproxy/v3/router"
  28. "github.com/imgproxy/imgproxy/v3/security"
  29. "github.com/imgproxy/imgproxy/v3/svg"
  30. "github.com/imgproxy/imgproxy/v3/vips"
  31. )
  32. var (
  33. queueSem *semaphore.Weighted
  34. processingSem *semaphore.Weighted
  35. headerVaryValue string
  36. )
  37. func initProcessingHandler() {
  38. if config.RequestsQueueSize > 0 {
  39. queueSem = semaphore.NewWeighted(int64(config.RequestsQueueSize + config.Workers))
  40. }
  41. processingSem = semaphore.NewWeighted(int64(config.Workers))
  42. vary := make([]string, 0)
  43. if config.AutoWebp || config.EnforceWebp || config.AutoAvif || config.EnforceAvif {
  44. vary = append(vary, "Accept")
  45. }
  46. if config.EnableClientHints {
  47. vary = append(vary, "Sec-CH-DPR", "DPR", "Sec-CH-Width", "Width")
  48. }
  49. headerVaryValue = strings.Join(vary, ", ")
  50. }
  51. func setCacheControl(rw http.ResponseWriter, force *time.Time, originHeaders map[string]string) {
  52. ttl := -1
  53. if _, ok := originHeaders["Fallback-Image"]; ok && config.FallbackImageTTL > 0 {
  54. ttl = config.FallbackImageTTL
  55. }
  56. if force != nil && (ttl < 0 || force.Before(time.Now().Add(time.Duration(ttl)*time.Second))) {
  57. ttl = imath.Min(config.TTL, imath.Max(0, int(time.Until(*force).Seconds())))
  58. }
  59. if config.CacheControlPassthrough && ttl < 0 && originHeaders != nil {
  60. if val, ok := originHeaders["Cache-Control"]; ok && len(val) > 0 {
  61. rw.Header().Set("Cache-Control", val)
  62. return
  63. }
  64. if val, ok := originHeaders["Expires"]; ok && len(val) > 0 {
  65. if t, err := time.Parse(http.TimeFormat, val); err == nil {
  66. ttl = imath.Max(0, int(time.Until(t).Seconds()))
  67. }
  68. }
  69. }
  70. if ttl < 0 {
  71. ttl = config.TTL
  72. }
  73. if ttl > 0 {
  74. rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", ttl))
  75. } else {
  76. rw.Header().Set("Cache-Control", "no-cache")
  77. }
  78. }
  79. func setLastModified(rw http.ResponseWriter, originHeaders map[string]string) {
  80. if config.LastModifiedEnabled {
  81. if val, ok := originHeaders["Last-Modified"]; ok && len(val) != 0 {
  82. rw.Header().Set("Last-Modified", val)
  83. }
  84. }
  85. }
  86. func setVary(rw http.ResponseWriter) {
  87. if len(headerVaryValue) > 0 {
  88. rw.Header().Set("Vary", headerVaryValue)
  89. }
  90. }
  91. func setCanonical(rw http.ResponseWriter, originURL string) {
  92. if config.SetCanonicalHeader {
  93. if strings.HasPrefix(originURL, "https://") || strings.HasPrefix(originURL, "http://") {
  94. linkHeader := fmt.Sprintf(`<%s>; rel="canonical"`, originURL)
  95. rw.Header().Set("Link", linkHeader)
  96. }
  97. }
  98. }
  99. func respondWithImage(reqID string, r *http.Request, rw http.ResponseWriter, statusCode int, resultData *imagedata.ImageData, po *options.ProcessingOptions, originURL string, originData *imagedata.ImageData) {
  100. var contentDisposition string
  101. if len(po.Filename) > 0 {
  102. contentDisposition = resultData.Type.ContentDisposition(po.Filename, po.ReturnAttachment)
  103. } else {
  104. contentDisposition = resultData.Type.ContentDispositionFromURL(originURL, po.ReturnAttachment)
  105. }
  106. rw.Header().Set("Content-Type", resultData.Type.Mime())
  107. rw.Header().Set("Content-Disposition", contentDisposition)
  108. setCacheControl(rw, po.Expires, originData.Headers)
  109. setLastModified(rw, originData.Headers)
  110. setVary(rw)
  111. setCanonical(rw, originURL)
  112. if config.EnableDebugHeaders {
  113. rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(originData.Data)))
  114. rw.Header().Set("X-Origin-Width", resultData.Headers["X-Origin-Width"])
  115. rw.Header().Set("X-Origin-Height", resultData.Headers["X-Origin-Height"])
  116. rw.Header().Set("X-Result-Width", resultData.Headers["X-Result-Width"])
  117. rw.Header().Set("X-Result-Height", resultData.Headers["X-Result-Height"])
  118. }
  119. rw.Header().Set("Content-Security-Policy", "script-src 'none'")
  120. rw.Header().Set("Content-Length", strconv.Itoa(len(resultData.Data)))
  121. rw.WriteHeader(statusCode)
  122. _, err := rw.Write(resultData.Data)
  123. var ierr *ierrors.Error
  124. if err != nil {
  125. ierr = newResponseWriteError(err)
  126. if config.ReportIOErrors {
  127. sendErr(r.Context(), "IO", ierr)
  128. errorreport.Report(ierr, r)
  129. }
  130. }
  131. router.LogResponse(
  132. reqID, r, statusCode, ierr,
  133. log.Fields{
  134. "image_url": originURL,
  135. "processing_options": po,
  136. },
  137. )
  138. }
  139. func respondWithNotModified(reqID string, r *http.Request, rw http.ResponseWriter, po *options.ProcessingOptions, originURL string, originHeaders map[string]string) {
  140. setCacheControl(rw, po.Expires, originHeaders)
  141. setVary(rw)
  142. rw.WriteHeader(304)
  143. router.LogResponse(
  144. reqID, r, 304, nil,
  145. log.Fields{
  146. "image_url": originURL,
  147. "processing_options": po,
  148. },
  149. )
  150. }
  151. func sendErr(ctx context.Context, errType string, err error) {
  152. send := true
  153. if ierr, ok := err.(*ierrors.Error); ok {
  154. switch ierr.StatusCode() {
  155. case http.StatusServiceUnavailable:
  156. errType = "timeout"
  157. case 499:
  158. // Don't need to send a "request cancelled" error
  159. send = false
  160. }
  161. }
  162. if send {
  163. metrics.SendError(ctx, errType, err)
  164. }
  165. }
  166. func sendErrAndPanic(ctx context.Context, errType string, err error) {
  167. sendErr(ctx, errType, err)
  168. panic(err)
  169. }
  170. func checkErr(ctx context.Context, errType string, err error) {
  171. if err == nil {
  172. return
  173. }
  174. sendErrAndPanic(ctx, errType, err)
  175. }
  176. func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
  177. stats.IncRequestsInProgress()
  178. defer stats.DecRequestsInProgress()
  179. ctx := r.Context()
  180. path := r.RequestURI
  181. if queryStart := strings.IndexByte(path, '?'); queryStart >= 0 {
  182. path = path[:queryStart]
  183. }
  184. if len(config.PathPrefix) > 0 {
  185. path = strings.TrimPrefix(path, config.PathPrefix)
  186. }
  187. path = strings.TrimPrefix(path, "/")
  188. signature := ""
  189. if signatureEnd := strings.IndexByte(path, '/'); signatureEnd > 0 {
  190. signature = path[:signatureEnd]
  191. path = path[signatureEnd:]
  192. } else {
  193. sendErrAndPanic(ctx, "path_parsing", newInvalidURLErrorf(
  194. http.StatusNotFound, "Invalid path: %s", path),
  195. )
  196. }
  197. path = fixPath(path)
  198. if err := security.VerifySignature(signature, path); err != nil {
  199. sendErrAndPanic(ctx, "security", err)
  200. }
  201. po, imageURL, err := options.ParsePath(path, r.Header)
  202. checkErr(ctx, "path_parsing", err)
  203. var imageOrigin any
  204. if u, uerr := url.Parse(imageURL); uerr == nil {
  205. imageOrigin = u.Scheme + "://" + u.Host
  206. }
  207. errorreport.SetMetadata(r, "Source Image URL", imageURL)
  208. errorreport.SetMetadata(r, "Source Image Origin", imageOrigin)
  209. errorreport.SetMetadata(r, "Processing Options", po)
  210. metricsMeta := metrics.Meta{
  211. metrics.MetaSourceImageURL: imageURL,
  212. metrics.MetaSourceImageOrigin: imageOrigin,
  213. metrics.MetaProcessingOptions: po.Diff().Flatten(),
  214. }
  215. metrics.SetMetadata(ctx, metricsMeta)
  216. err = security.VerifySourceURL(imageURL)
  217. checkErr(ctx, "security", err)
  218. if po.Raw {
  219. streamOriginImage(ctx, reqID, r, rw, po, imageURL)
  220. return
  221. }
  222. // SVG is a special case. Though saving to svg is not supported, SVG->SVG is.
  223. if !vips.SupportsSave(po.Format) && po.Format != imagetype.Unknown && po.Format != imagetype.SVG {
  224. sendErrAndPanic(ctx, "path_parsing", newInvalidURLErrorf(
  225. http.StatusUnprocessableEntity,
  226. "Resulting image format is not supported: %s", po.Format,
  227. ))
  228. }
  229. imgRequestHeader := make(http.Header)
  230. var etagHandler etag.Handler
  231. if config.ETagEnabled {
  232. etagHandler.ParseExpectedETag(r.Header.Get("If-None-Match"))
  233. if etagHandler.SetActualProcessingOptions(po) {
  234. if imgEtag := etagHandler.ImageEtagExpected(); len(imgEtag) != 0 {
  235. imgRequestHeader.Set("If-None-Match", imgEtag)
  236. }
  237. }
  238. }
  239. if config.LastModifiedEnabled {
  240. if modifiedSince := r.Header.Get("If-Modified-Since"); len(modifiedSince) != 0 {
  241. imgRequestHeader.Set("If-Modified-Since", modifiedSince)
  242. }
  243. }
  244. if queueSem != nil {
  245. acquired := queueSem.TryAcquire(1)
  246. if !acquired {
  247. panic(newTooManyRequestsError())
  248. }
  249. defer queueSem.Release(1)
  250. }
  251. // The heavy part starts here, so we need to restrict worker number
  252. func() {
  253. defer metrics.StartQueueSegment(ctx)()
  254. err = processingSem.Acquire(ctx, 1)
  255. if err != nil {
  256. // We don't actually need to check timeout here,
  257. // but it's an easy way to check if this is an actual timeout
  258. // or the request was canceled
  259. checkErr(ctx, "queue", router.CheckTimeout(ctx))
  260. // We should never reach this line as err could be only ctx.Err()
  261. // and we've already checked for it. But beter safe than sorry
  262. sendErrAndPanic(ctx, "queue", err)
  263. }
  264. }()
  265. defer processingSem.Release(1)
  266. stats.IncImagesInProgress()
  267. defer stats.DecImagesInProgress()
  268. statusCode := http.StatusOK
  269. originData, err := func() (*imagedata.ImageData, error) {
  270. defer metrics.StartDownloadingSegment(ctx, metrics.Meta{
  271. metrics.MetaSourceImageURL: metricsMeta[metrics.MetaSourceImageURL],
  272. metrics.MetaSourceImageOrigin: metricsMeta[metrics.MetaSourceImageOrigin],
  273. })()
  274. downloadOpts := imagedata.DownloadOptions{
  275. Header: imgRequestHeader,
  276. CookieJar: nil,
  277. }
  278. if config.CookiePassthrough {
  279. downloadOpts.CookieJar, err = cookies.JarFromRequest(r)
  280. checkErr(ctx, "download", err)
  281. }
  282. return imagedata.Download(ctx, imageURL, "source image", downloadOpts, po.SecurityOptions)
  283. }()
  284. var nmErr imagefetcher.NotModifiedError
  285. switch {
  286. case err == nil:
  287. defer originData.Close()
  288. case errors.As(err, &nmErr):
  289. if config.ETagEnabled && len(etagHandler.ImageEtagExpected()) != 0 {
  290. rw.Header().Set("ETag", etagHandler.GenerateExpectedETag())
  291. }
  292. h := make(map[string]string)
  293. for k := range nmErr.Headers() {
  294. h[k] = nmErr.Headers().Get(k)
  295. }
  296. respondWithNotModified(reqID, r, rw, po, imageURL, h)
  297. return
  298. default:
  299. // This may be a request timeout error or a request cancelled error.
  300. // Check it before moving further
  301. checkErr(ctx, "timeout", router.CheckTimeout(ctx))
  302. ierr := ierrors.Wrap(err, 0)
  303. if config.ReportDownloadingErrors {
  304. ierr = ierrors.Wrap(ierr, 0, ierrors.WithShouldReport(true))
  305. }
  306. sendErr(ctx, "download", ierr)
  307. if imagedata.FallbackImage == nil {
  308. panic(ierr)
  309. }
  310. // We didn't panic, so the error is not reported.
  311. // Report it now
  312. if ierr.ShouldReport() {
  313. errorreport.Report(ierr, r)
  314. }
  315. log.WithField("request_id", reqID).Warningf("Could not load image %s. Using fallback image. %s", imageURL, ierr.Error())
  316. if config.FallbackImageHTTPCode > 0 {
  317. statusCode = config.FallbackImageHTTPCode
  318. } else {
  319. statusCode = ierr.StatusCode()
  320. }
  321. originData = imagedata.FallbackImage
  322. }
  323. checkErr(ctx, "timeout", router.CheckTimeout(ctx))
  324. if config.ETagEnabled && statusCode == http.StatusOK {
  325. imgDataMatch := etagHandler.SetActualImageData(originData)
  326. rw.Header().Set("ETag", etagHandler.GenerateActualETag())
  327. if imgDataMatch && etagHandler.ProcessingOptionsMatch() {
  328. respondWithNotModified(reqID, r, rw, po, imageURL, originData.Headers)
  329. return
  330. }
  331. }
  332. checkErr(ctx, "timeout", router.CheckTimeout(ctx))
  333. // Skip processing svg with unknown or the same destination imageType
  334. // if it's not forced by AlwaysRasterizeSvg option
  335. // Also skip processing if the format is in SkipProcessingFormats
  336. shouldSkipProcessing := (originData.Type == po.Format || po.Format == imagetype.Unknown) &&
  337. (slices.Contains(po.SkipProcessingFormats, originData.Type) ||
  338. originData.Type == imagetype.SVG && !config.AlwaysRasterizeSvg)
  339. if shouldSkipProcessing {
  340. if originData.Type == imagetype.SVG && config.SanitizeSvg {
  341. sanitized, svgErr := svg.Sanitize(originData)
  342. checkErr(ctx, "svg_processing", svgErr)
  343. defer sanitized.Close()
  344. respondWithImage(reqID, r, rw, statusCode, sanitized, po, imageURL, originData)
  345. return
  346. }
  347. respondWithImage(reqID, r, rw, statusCode, originData, po, imageURL, originData)
  348. return
  349. }
  350. if !vips.SupportsLoad(originData.Type) {
  351. sendErrAndPanic(ctx, "processing", newInvalidURLErrorf(
  352. http.StatusUnprocessableEntity,
  353. "Source image format is not supported: %s", originData.Type,
  354. ))
  355. }
  356. // At this point we can't allow requested format to be SVG as we can't save SVGs
  357. if po.Format == imagetype.SVG {
  358. sendErrAndPanic(ctx, "processing", newInvalidURLErrorf(
  359. http.StatusUnprocessableEntity,
  360. "Resulting image format is not supported: svg",
  361. ))
  362. }
  363. resultData, err := func() (*imagedata.ImageData, error) {
  364. defer metrics.StartProcessingSegment(ctx, metrics.Meta{
  365. metrics.MetaProcessingOptions: metricsMeta[metrics.MetaProcessingOptions],
  366. })()
  367. return processing.ProcessImage(ctx, originData, po)
  368. }()
  369. checkErr(ctx, "processing", err)
  370. defer resultData.Close()
  371. checkErr(ctx, "timeout", router.CheckTimeout(ctx))
  372. respondWithImage(reqID, r, rw, statusCode, resultData, po, imageURL, originData)
  373. }