From 4e74c8d25f3243ba032cb2f5e9d933de4e8129b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 24 Jan 2025 13:56:57 +0100 Subject: [PATCH] Work around dream-encoding footgun Dream-encoding adds a Transfer-Encoding header to a response. If that response is an error code it will go through the error handler which may change the response - but the new response *does not* go through the dream-encoding middleware! This means we may rewrite the response with a plaintext message but still have the Transfer-Encoding header! This makes clients very unhappy. --- lib/builder_web.ml | 14 +++++++++++++- lib/dune | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/builder_web.ml b/lib/builder_web.ml index c8460f1..848ee24 100644 --- a/lib/builder_web.ml +++ b/lib/builder_web.ml @@ -797,7 +797,19 @@ let error_template error _debug_info suggested_response = in Dream.set_header suggested_response "Content-Type" Dream.text_html; Dream.set_body suggested_response @@ string_of_html html; - Lwt.return suggested_response + (* NOTE: this does the same job as the dream-encoding middleware; + the middleware is not triggered in error templates *) + let preferred_algorithm = + Option.bind error.request + Dream_encoding.preferred_content_encoding + in + begin match preferred_algorithm with + | Some algorithm -> + let+ body = Dream.body suggested_response in + Dream_encoding.with_encoded_body body ~algorithm suggested_response + | None -> + Lwt.return suggested_response + end | _ -> Lwt.return suggested_response diff --git a/lib/dune b/lib/dune index 9418899..ff25562 100644 --- a/lib/dune +++ b/lib/dune @@ -2,4 +2,4 @@ (name builder_web) (libraries builder builder_db dream tyxml bos duration ohex caqti-lwt opamdiff ptime.clock.os cmarkit tar tar.gz tar-unix owee solo5-elftool decompress.de - decompress.gz uri digestif)) + decompress.gz uri digestif dream-encoding)) -- 2.47.1