From 6af916c3f4842451eb7560c20f696e0f8c77da8a Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 10 Nov 2021 16:48:46 +0100 Subject: [PATCH] fix: non ascii buffer response in http server (#12728) --- Cargo.lock | 10 +++++++++ cli/tests/unit/http_test.ts | 28 ++++++++++++++++++++++++++ serde_v8/Cargo.toml | 1 + serde_v8/src/magic/string_or_buffer.rs | 3 ++- serde_v8/tests/de.rs | 10 +++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 301b74d31c..09052e2eb0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3263,6 +3263,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.130" @@ -3315,6 +3324,7 @@ version = "0.17.0" dependencies = [ "bencher", "serde", + "serde_bytes", "serde_json", "v8", ] diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index d947282dbe..a6f80eb2c1 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -999,3 +999,31 @@ unitTest( await Promise.all([server(), client()]); }, ); + +unitTest( + { permissions: { net: true } }, + async function httpServerRespondNonAsciiUint8Array() { + const promise = (async () => { + const listener = Deno.listen({ port: 4501 }); + const conn = await listener.accept(); + listener.close(); + const httpConn = Deno.serveHttp(conn); + const e = await httpConn.nextRequest(); + assert(e); + const { request, respondWith } = e; + assertEquals(request.body, null); + await respondWith( + new Response(new Uint8Array([128]), {}), + ); + httpConn.close(); + })(); + + const resp = await fetch("http://localhost:4501/"); + console.log(resp.headers); + assertEquals(resp.status, 200); + const body = await resp.arrayBuffer(); + assertEquals(new Uint8Array(body), new Uint8Array([128])); + + await promise; + }, +); diff --git a/serde_v8/Cargo.toml b/serde_v8/Cargo.toml index c425bed223..1d4c7bd9e1 100644 --- a/serde_v8/Cargo.toml +++ b/serde_v8/Cargo.toml @@ -11,6 +11,7 @@ description = "Rust to V8 serialization and deserialization" [dependencies] serde = { version = "1.0.130", features = ["derive"] } +serde_bytes = "0.11" v8 = "0.34.0" [dev-dependencies] diff --git a/serde_v8/src/magic/string_or_buffer.rs b/serde_v8/src/magic/string_or_buffer.rs index 4518fe082c..edde2adcd1 100644 --- a/serde_v8/src/magic/string_or_buffer.rs +++ b/serde_v8/src/magic/string_or_buffer.rs @@ -30,8 +30,9 @@ impl<'de> serde::Deserialize<'de> for StringOrBuffer { #[derive(serde::Deserialize)] #[serde(untagged)] enum StringOrBufferInner { - String(String), + #[serde(with = "serde_bytes")] Buffer(Vec), + String(String), } impl StringOrBufferInner { diff --git a/serde_v8/tests/de.rs b/serde_v8/tests/de.rs index 366d6d2b5d..fee5325186 100644 --- a/serde_v8/tests/de.rs +++ b/serde_v8/tests/de.rs @@ -173,6 +173,16 @@ fn de_string_or_buffer() { assert_eq!(sob.as_slice(), &[0x68, 0x65, 0x6C, 0x6C, 0x6F]); }); + dedo("new Uint8Array([97])", |scope, v| { + let sob: serde_v8::StringOrBuffer = serde_v8::from_v8(scope, v).unwrap(); + assert_eq!(sob.as_slice(), &[97]); + }); + + dedo("new Uint8Array([128])", |scope, v| { + let sob: serde_v8::StringOrBuffer = serde_v8::from_v8(scope, v).unwrap(); + assert_eq!(sob.as_slice(), &[128]); + }); + dedo( "(Uint8Array.from([0x68, 0x65, 0x6C, 0x6C, 0x6F]))", |scope, v| {