@@ -2,9 +2,9 @@ use std::io::Cursor;
22
33pub const CHACHA20_POLY1305_NONCE_LEN : usize = 32 ;
44pub const POLY1305_TAG_SIZE : usize = 16 ;
5- pub const ENCAPSULATED_MESSAGE_BYTES : usize = 65536 ;
6- pub const BHTTP_REQ_BYTES : usize =
7- ENCAPSULATED_MESSAGE_BYTES - ( CHACHA20_POLY1305_NONCE_LEN + POLY1305_TAG_SIZE ) ;
5+ pub const OHTTP_OVERHEAD : usize = CHACHA20_POLY1305_NONCE_LEN + POLY1305_TAG_SIZE ;
6+ pub const ENCAPSULATED_MESSAGE_BYTES : usize = 8192 ;
7+ pub const BHTTP_REQ_BYTES : usize = ENCAPSULATED_MESSAGE_BYTES - OHTTP_OVERHEAD ;
88
99#[ derive( Debug ) ]
1010pub enum GatewayError {
@@ -25,7 +25,6 @@ impl std::fmt::Display for GatewayError {
2525
2626impl std:: error:: Error for GatewayError { }
2727
28- /// Represents the decapsulated HTTP request extracted from OHTTP
2928pub struct DecapsulatedRequest {
3029 pub method : String ,
3130 pub uri : String ,
@@ -90,18 +89,27 @@ pub fn encapsulate_ohttp_response(
9089 GatewayError :: InternalServerError ( format ! ( "BHTTP serialization failed: {}" , e) )
9190 } ) ?;
9291
92+ if bhttp_bytes. len ( ) > BHTTP_REQ_BYTES {
93+ return Err ( GatewayError :: InternalServerError ( format ! (
94+ "BHTTP response too large: {} > {}" ,
95+ bhttp_bytes. len( ) ,
96+ BHTTP_REQ_BYTES
97+ ) ) ) ;
98+ }
99+
93100 bhttp_bytes. resize ( BHTTP_REQ_BYTES , 0 ) ;
94101
95102 let ohttp_res = res_ctx. encapsulate ( & bhttp_bytes) . map_err ( |e| {
96103 GatewayError :: InternalServerError ( format ! ( "OHTTP encapsulation failed: {}" , e) )
97104 } ) ?;
98105
99- assert ! (
100- ohttp_res. len( ) == ENCAPSULATED_MESSAGE_BYTES ,
101- "Unexpected OHTTP response size: {} != {}" ,
102- ohttp_res. len( ) ,
103- ENCAPSULATED_MESSAGE_BYTES
104- ) ;
106+ if ohttp_res. len ( ) != ENCAPSULATED_MESSAGE_BYTES {
107+ return Err ( GatewayError :: InternalServerError ( format ! (
108+ "Unexpected OHTTP response size: {} != {}" ,
109+ ohttp_res. len( ) ,
110+ ENCAPSULATED_MESSAGE_BYTES
111+ ) ) ) ;
112+ }
105113
106114 Ok ( ohttp_res)
107115}
0 commit comments