From f3a5182b5f85e02063d5d10c02f4026b22188e98 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 13 Aug 2023 06:03:06 -0700 Subject: [PATCH] Ignore invalid content type header values (#2326) Fixes #2322. --- src/subcommand/server.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 8b1b1476b8..59d46286cf 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -815,9 +815,8 @@ impl Server { header::CONTENT_TYPE, inscription .content_type() - .unwrap_or("application/octet-stream") - .parse() - .unwrap(), + .and_then(|content_type| content_type.parse().ok()) + .unwrap_or(HeaderValue::from_static("application/octet-stream")), ); headers.insert( header::CONTENT_SECURITY_POLICY, @@ -2201,6 +2200,18 @@ mod tests { assert!(body.is_empty()); } + #[test] + fn content_response_bad_content_type() { + let (headers, body) = Server::content_response(Inscription::new( + Some("\n".as_bytes().to_vec()), + Some(Vec::new()), + )) + .unwrap(); + + assert_eq!(headers["content-type"], "application/octet-stream"); + assert!(body.is_empty()); + } + #[test] fn text_preview() { let server = TestServer::new_with_regtest();