child_process: harden fork arguments validation · nodejs/node@9ad5106 (original) (raw)

``

1

`+

'use strict';

`

``

2

`+

const common = require('../common');

`

``

3

`+

const fixtures = require('../common/fixtures');

`

``

4

`+

const assert = require('assert');

`

``

5

`+

const { fork } = require('child_process');

`

``

6

+

``

7

`` +

// This test check the arguments of fork method

``

``

8

`+

// Refs: https://github.com/nodejs/node/issues/20749

`

``

9

`+

const expectedEnv = { foo: 'bar' };

`

``

10

+

``

11

`` +

// Ensure that first argument modulePath must be provided

``

``

12

`+

// and be of type string

`

``

13

`+

{

`

``

14

`+

const invalidModulePath = [

`

``

15

`+

0,

`

``

16

`+

true,

`

``

17

`+

undefined,

`

``

18

`+

null,

`

``

19

`+

[],

`

``

20

`+

{},

`

``

21

`+

() => {},

`

``

22

`+

Symbol('t')

`

``

23

`+

];

`

``

24

`+

invalidModulePath.forEach((modulePath) => {

`

``

25

`+

common.expectsError(() => fork(modulePath), {

`

``

26

`+

code: 'ERR_INVALID_ARG_TYPE',

`

``

27

`+

type: TypeError,

`

``

28

`+

message: /^The "modulePath" argument must be of type string/

`

``

29

`+

});

`

``

30

`+

});

`

``

31

+

``

32

`+

const cp = fork(fixtures.path('child-process-echo-options.js'));

`

``

33

`+

cp.on(

`

``

34

`+

'exit',

`

``

35

`+

common.mustCall((code) => {

`

``

36

`+

assert.strictEqual(code, 0);

`

``

37

`+

})

`

``

38

`+

);

`

``

39

`+

}

`

``

40

+

``

41

`` +

// Ensure that the second argument of fork

``

``

42

`` +

// and fork should parse options

``

``

43

`+

// correctly if args is undefined or null

`

``

44

`+

{

`

``

45

`+

const invalidSecondArgs = [

`

``

46

`+

0,

`

``

47

`+

true,

`

``

48

`+

() => {},

`

``

49

`+

Symbol('t')

`

``

50

`+

];

`

``

51

`+

invalidSecondArgs.forEach((arg) => {

`

``

52

`+

common.expectsError(

`

``

53

`+

() => {

`

``

54

`+

fork(fixtures.path('child-process-echo-options.js'), arg);

`

``

55

`+

},

`

``

56

`+

{

`

``

57

`+

code: 'ERR_INVALID_ARG_VALUE',

`

``

58

`+

type: TypeError

`

``

59

`+

}

`

``

60

`+

);

`

``

61

`+

});

`

``

62

+

``

63

`+

const argsLists = [undefined, null, []];

`

``

64

+

``

65

`+

argsLists.forEach((args) => {

`

``

66

`+

const cp = fork(fixtures.path('child-process-echo-options.js'), args, {

`

``

67

`+

env: Object.assign({}, process.env, expectedEnv)

`

``

68

`+

});

`

``

69

+

``

70

`+

cp.on(

`

``

71

`+

'message',

`

``

72

`+

common.mustCall(({ env }) => {

`

``

73

`+

assert.strictEqual(env.foo, expectedEnv.foo);

`

``

74

`+

})

`

``

75

`+

);

`

``

76

+

``

77

`+

cp.on(

`

``

78

`+

'exit',

`

``

79

`+

common.mustCall((code) => {

`

``

80

`+

assert.strictEqual(code, 0);

`

``

81

`+

})

`

``

82

`+

);

`

``

83

`+

});

`

``

84

`+

}

`

``

85

+

``

86

`+

// Ensure that the third argument should be type of object if provided

`

``

87

`+

{

`

``

88

`+

const invalidThirdArgs = [

`

``

89

`+

0,

`

``

90

`+

true,

`

``

91

`+

() => {},

`

``

92

`+

Symbol('t')

`

``

93

`+

];

`

``

94

`+

invalidThirdArgs.forEach((arg) => {

`

``

95

`+

common.expectsError(

`

``

96

`+

() => {

`

``

97

`+

fork(fixtures.path('child-process-echo-options.js'), [], arg);

`

``

98

`+

},

`

``

99

`+

{

`

``

100

`+

code: 'ERR_INVALID_ARG_VALUE',

`

``

101

`+

type: TypeError

`

``

102

`+

}

`

``

103

`+

);

`

``

104

`+

});

`

``

105

`+

}

`