@@ -1141,6 +1141,74 @@ describe("[AWS Lambda] Request Utils", () => {
11411141 }
11421142 } ) ;
11431143
1144+ test ( "should not treat a charset/boundary parameter as the MIME type" , async ( ) => {
1145+ // `; charset=utf-8` and a boundary containing `utf8` used to match the
1146+ // content type as text, so the payload was UTF-8 decoded and every
1147+ // invalid byte was replaced with U+FFFD — unrecoverable.
1148+ const binaryData = Buffer . from ( [ 0xff , 0xfe , 0x00 , 0x80 , 0x01 ] ) ;
1149+
1150+ for ( const contentType of [
1151+ "application/octet-stream; charset=utf-8" ,
1152+ "multipart/form-data; boundary=--utf8x" ,
1153+ ] ) {
1154+ const response = new Response ( binaryData , {
1155+ status : 200 ,
1156+ headers : { "Content-Type" : contentType } ,
1157+ } ) ;
1158+
1159+ const awsBody = await awsResponseBody ( response ) ;
1160+
1161+ expect ( awsBody . isBase64Encoded ) . toBe ( true ) ;
1162+ expect ( Buffer . from ( awsBody . body , "base64" ) ) . toEqual ( binaryData ) ;
1163+ }
1164+ } ) ;
1165+
1166+ test ( "should treat +json/+xml structured syntax suffixes as text" , async ( ) => {
1167+ const content = '<svg xmlns="http://www.w3.org/2000/svg" />' ;
1168+
1169+ for ( const contentType of [
1170+ "image/svg+xml" ,
1171+ "application/xhtml+xml" ,
1172+ "application/ld+json" ,
1173+ "application/manifest+json; charset=utf-8" ,
1174+ ] ) {
1175+ const response = new Response ( content , {
1176+ status : 200 ,
1177+ headers : { "Content-Type" : contentType } ,
1178+ } ) ;
1179+
1180+ const awsBody = await awsResponseBody ( response ) ;
1181+
1182+ expect ( awsBody . body ) . toBe ( content ) ;
1183+ expect ( awsBody . isBase64Encoded ) . toBeUndefined ( ) ;
1184+ }
1185+ } ) ;
1186+
1187+ test ( "should keep binary types with a `+` suffix binary" , async ( ) => {
1188+ const binaryData = Buffer . from ( [ 0x00 , 0x61 , 0x73 , 0x6d ] ) ;
1189+ const response = new Response ( binaryData , {
1190+ status : 200 ,
1191+ headers : { "Content-Type" : "application/wasm" } ,
1192+ } ) ;
1193+
1194+ const awsBody = await awsResponseBody ( response ) ;
1195+
1196+ expect ( awsBody . isBase64Encoded ) . toBe ( true ) ;
1197+ expect ( Buffer . from ( awsBody . body , "base64" ) ) . toEqual ( binaryData ) ;
1198+ } ) ;
1199+
1200+ test ( "should still detect text types carrying parameters" , async ( ) => {
1201+ const response = new Response ( "Hello, World!" , {
1202+ status : 200 ,
1203+ headers : { "Content-Type" : "text/plain; charset=utf-8" } ,
1204+ } ) ;
1205+
1206+ const awsBody = await awsResponseBody ( response ) ;
1207+
1208+ expect ( awsBody . body ) . toBe ( "Hello, World!" ) ;
1209+ expect ( awsBody . isBase64Encoded ) . toBeUndefined ( ) ;
1210+ } ) ;
1211+
11441212 test ( "should handle missing content-type header" , async ( ) => {
11451213 const response = new Response ( "content" , { status : 200 } ) ;
11461214
0 commit comments