@@ -38,7 +38,7 @@ def parse_length_prefixed_chunk(body)
3838 raise "Malformed length-prefixed RSC payload: missing tab separator" unless content_length_hex
3939
4040 content_start = header_end + 1
41- content_length = Integer ( content_length_hex , 16 )
41+ content_length = parse_content_length ( content_length_hex )
4242 content = body . byteslice ( content_start , content_length )
4343 raise "Malformed length-prefixed RSC payload: truncated content" unless content &.bytesize == content_length
4444
@@ -48,6 +48,12 @@ def parse_length_prefixed_chunk(body)
4848 ]
4949 end
5050
51+ def parse_content_length ( content_length_hex )
52+ Integer ( content_length_hex , 16 )
53+ rescue ArgumentError
54+ raise "Malformed length-prefixed RSC payload: invalid hex length '#{ content_length_hex } '"
55+ end
56+
5157 def parse_payload_metadata ( metadata_json , content )
5258 metadata = JSON . parse ( metadata_json . force_encoding ( Encoding ::UTF_8 ) )
5359 content . force_encoding ( Encoding ::UTF_8 )
@@ -63,6 +69,13 @@ def expect_valid_rsc_payload
6369 expect ( chunks . any? { |chunk | chunk . key? ( "html" ) } ) . to be ( true )
6470 end
6571
72+ it "raises a malformed payload error for invalid hex content lengths" do
73+ payload = { payloadType : "string" } . to_json
74+
75+ expect { parse_length_prefixed_chunk ( "#{ payload } \t zz\n content" . b ) }
76+ . to raise_error ( RuntimeError , "Malformed length-prefixed RSC payload: invalid hex length 'zz'" )
77+ end
78+
6679 it "streams a valid RSC payload for ServerComponentsPage" do
6780 get "/rsc_payload/ServerComponentsPage" , params : { props : "{}" }
6881 expect_valid_rsc_payload
0 commit comments