processing_handler.go 14 KB

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