1+ import { ok , strictEqual } from 'node:assert' ;
2+ import { createServer } from 'node:http' ;
3+ import test from 'node:test' ;
4+ import formidable , { errors } from '../../src/index.js' ;
5+
6+
7+
8+ let server ;
9+ let port = 13000 ;
10+
11+ test . beforeEach ( ( ) => {
12+ // Increment port to avoid conflicts between tests
13+ port += 1 ;
14+ server = createServer ( ) ;
15+ } ) ;
16+
17+ test . afterEach ( ( ) => {
18+ return new Promise ( ( resolve ) => {
19+ if ( server . listening ) {
20+ server . close ( ( ) => resolve ( ) ) ;
21+ } else {
22+ resolve ( ) ;
23+ }
24+ } ) ;
25+ } ) ;
26+
27+ test ( 'prototype contamination' , async ( t ) => {
28+ server . on ( 'request' , async ( req , res ) => {
29+ const form = formidable ( ) ;
30+
31+ const [ fields , files ] = await form . parse ( req ) ;
32+ strictEqual ( typeof String ( fields ) , 'string' , "the toString method should not be compromised" ) ;
33+
34+ res . writeHead ( 200 ) ;
35+ res . end ( "ok" ) ;
36+
37+ } ) ;
38+
39+ await new Promise ( resolve => server . listen ( port , resolve ) ) ;
40+
41+ const body = `{"toString":"x","hasOwnProperty":"x","a":5}` ;
42+
43+ const resClient = await fetch ( String ( new URL ( `http:localhost:${ port } /` ) ) , {
44+ method : 'POST' ,
45+ headers : {
46+ 'Content-Length' : body . length ,
47+ Host : `localhost:${ port } ` ,
48+ 'Content-Type' : 'text/json;' ,
49+ } ,
50+ body
51+ } ) ;
52+
53+ strictEqual ( resClient . status , 200 ) ;
54+
55+ const text = await resClient . text ( ) ;
56+
57+ t . ok ( true )
58+ } ) ;
0 commit comments