@@ -2157,3 +2157,71 @@ test('Should pass request params and options to generateRequestId', t => {
2157
2157
2158
2158
transport . request ( params , options , t . error )
2159
2159
} )
2160
+
2161
+ test ( 'Secure json parsing' , t => {
2162
+ t . test ( '__proto__ protection' , t => {
2163
+ t . plan ( 2 )
2164
+ function handler ( req , res ) {
2165
+ res . setHeader ( 'Content-Type' , 'application/json;utf=8' )
2166
+ res . end ( '{"__proto__":{"a":1}}' )
2167
+ }
2168
+
2169
+ buildServer ( handler , ( { port } , server ) => {
2170
+ const pool = new ConnectionPool ( { Connection } )
2171
+ pool . addConnection ( `http://localhost:${ port } ` )
2172
+
2173
+ const transport = new Transport ( {
2174
+ emit : ( ) => { } ,
2175
+ connectionPool : pool ,
2176
+ serializer : new Serializer ( ) ,
2177
+ maxRetries : 3 ,
2178
+ requestTimeout : 30000 ,
2179
+ sniffInterval : false ,
2180
+ sniffOnStart : false
2181
+ } )
2182
+
2183
+ transport . request ( {
2184
+ method : 'GET' ,
2185
+ path : '/hello'
2186
+ } , ( err , { body } ) => {
2187
+ t . true ( err instanceof DeserializationError )
2188
+ t . is ( err . message , 'Object contains forbidden prototype property' )
2189
+ server . stop ( )
2190
+ } )
2191
+ } )
2192
+ } )
2193
+
2194
+ t . test ( 'constructor protection' , t => {
2195
+ t . plan ( 2 )
2196
+ function handler ( req , res ) {
2197
+ res . setHeader ( 'Content-Type' , 'application/json;utf=8' )
2198
+ res . end ( '{"constructor":{"prototype":{"bar":"baz"}}}' )
2199
+ }
2200
+
2201
+ buildServer ( handler , ( { port } , server ) => {
2202
+ const pool = new ConnectionPool ( { Connection } )
2203
+ pool . addConnection ( `http://localhost:${ port } ` )
2204
+
2205
+ const transport = new Transport ( {
2206
+ emit : ( ) => { } ,
2207
+ connectionPool : pool ,
2208
+ serializer : new Serializer ( ) ,
2209
+ maxRetries : 3 ,
2210
+ requestTimeout : 30000 ,
2211
+ sniffInterval : false ,
2212
+ sniffOnStart : false
2213
+ } )
2214
+
2215
+ transport . request ( {
2216
+ method : 'GET' ,
2217
+ path : '/hello'
2218
+ } , ( err , { body } ) => {
2219
+ t . true ( err instanceof DeserializationError )
2220
+ t . is ( err . message , 'Object contains forbidden prototype property' )
2221
+ server . stop ( )
2222
+ } )
2223
+ } )
2224
+ } )
2225
+
2226
+ t . end ( )
2227
+ } )
0 commit comments