@@ -30,7 +30,7 @@ use super::*;
3030pub use crate :: output_substitution:: OutputSubstitution ;
3131use crate :: psbt:: PsbtExt ;
3232use crate :: request:: Request ;
33- use crate :: { PjUri , MAX_CONTENT_LENGTH } ;
33+ use crate :: PjUri ;
3434
3535/// A builder to construct the properties of a `Sender`.
3636#[ derive( Clone ) ]
@@ -278,9 +278,19 @@ impl V1Context {
278278 /// Call this method with response from receiver to continue BIP78 flow. If the response is
279279 /// valid you will get appropriate PSBT that you should sign and broadcast.
280280 #[ inline]
281- pub fn process_response ( self , response : & [ u8 ] ) -> Result < Psbt , ResponseError > {
282- if response. len ( ) > MAX_CONTENT_LENGTH {
283- return Err ( ResponseError :: from ( InternalValidationError :: ContentTooLarge ) ) ;
281+ pub fn process_response (
282+ self ,
283+ response : & [ u8 ] ,
284+ content_length : Option < usize > ,
285+ ) -> Result < Psbt , ResponseError > {
286+ if let Some ( expected_len) = content_length {
287+ if response. len ( ) != expected_len {
288+ return Err ( InternalValidationError :: ContentLengthMismatch {
289+ expected : expected_len,
290+ actual : response. len ( ) ,
291+ }
292+ . into ( ) ) ;
293+ }
284294 }
285295
286296 let res_str = std:: str:: from_utf8 ( response) . map_err ( |_| InternalValidationError :: Parse ) ?;
@@ -426,7 +436,9 @@ mod test {
426436 "message" : "This version of payjoin is not supported."
427437 } )
428438 . to_string ( ) ;
429- match ctx. process_response ( known_json_error. as_bytes ( ) ) {
439+
440+ let content_length = Some ( known_json_error. len ( ) ) ;
441+ match ctx. process_response ( known_json_error. as_bytes ( ) , content_length) {
430442 Err ( ResponseError :: WellKnown ( WellKnownError {
431443 code : ErrorCode :: VersionUnsupported ,
432444 ..
@@ -440,7 +452,9 @@ mod test {
440452 "message" : "This version of payjoin is not supported."
441453 } )
442454 . to_string ( ) ;
443- match ctx. process_response ( invalid_json_error. as_bytes ( ) ) {
455+
456+ let content_length = Some ( invalid_json_error. len ( ) ) ;
457+ match ctx. process_response ( invalid_json_error. as_bytes ( ) , content_length) {
444458 Err ( ResponseError :: Validation ( _) ) => ( ) ,
445459 _ => panic ! ( "Expected unrecognized JSON error" ) ,
446460 }
@@ -449,14 +463,20 @@ mod test {
449463 #[ test]
450464 fn process_response_valid ( ) {
451465 let ctx = create_v1_context ( ) ;
452- let response = ctx. process_response ( PAYJOIN_PROPOSAL . as_bytes ( ) ) ;
466+ let body = PAYJOIN_PROPOSAL . as_bytes ( ) ;
467+
468+ let content_length = Some ( body. len ( ) ) ;
469+ let response = ctx. process_response ( PAYJOIN_PROPOSAL . as_bytes ( ) , content_length) ;
453470 assert ! ( response. is_ok( ) )
454471 }
455472
456473 #[ test]
457474 fn process_response_invalid_psbt ( ) {
458475 let ctx = create_v1_context ( ) ;
459- let response = ctx. process_response ( INVALID_PSBT . as_bytes ( ) ) ;
476+ let body = INVALID_PSBT . as_bytes ( ) ;
477+
478+ let content_length = Some ( body. len ( ) ) ;
479+ let response = ctx. process_response ( INVALID_PSBT . as_bytes ( ) , content_length) ;
460480 match response {
461481 Ok ( _) => panic ! ( "Invalid PSBT should have caused an error" ) ,
462482 Err ( error) => match error {
@@ -477,7 +497,9 @@ mod test {
477497 let invalid_utf8 = & [ 0xF0 ] ;
478498
479499 let ctx = create_v1_context ( ) ;
480- let response = ctx. process_response ( invalid_utf8) ;
500+
501+ let content_length = Some ( invalid_utf8. len ( ) ) ;
502+ let response = ctx. process_response ( invalid_utf8, content_length) ;
481503 match response {
482504 Ok ( _) => panic ! ( "Invalid UTF-8 should have caused an error" ) ,
483505 Err ( error) => match error {
@@ -494,18 +516,22 @@ mod test {
494516
495517 #[ test]
496518 fn process_response_invalid_buffer_len ( ) {
497- let mut data = PAYJOIN_PROPOSAL . as_bytes ( ) . to_vec ( ) ;
498- data. extend ( std :: iter :: repeat ( 0 ) . take ( MAX_CONTENT_LENGTH + 1 ) ) ;
519+ let data = PAYJOIN_PROPOSAL . as_bytes ( ) . to_vec ( ) ;
520+ let bad_content_length = data. len ( ) + 10 ;
499521
500522 let ctx = create_v1_context ( ) ;
501- let response = ctx. process_response ( & data) ;
523+ let response = ctx. process_response ( & data, Some ( bad_content_length ) ) ;
502524 match response {
503525 Ok ( _) => panic ! ( "Invalid buffer length should have caused an error" ) ,
504526 Err ( error) => match error {
505527 ResponseError :: Validation ( e) => {
506528 assert_eq ! (
507529 e. to_string( ) ,
508- ValidationError :: from( InternalValidationError :: ContentTooLarge ) . to_string( )
530+ ValidationError :: from( InternalValidationError :: ContentLengthMismatch {
531+ expected: bad_content_length,
532+ actual: data. len( )
533+ } )
534+ . to_string( )
509535 ) ;
510536 }
511537 _ => panic ! ( "Unexpected error type" ) ,
0 commit comments