1
1
import * as stream from 'stream' ;
2
- import * as net from 'net ' ;
2
+ import * as tls from 'tls ' ;
3
3
import * as http from 'http' ;
4
4
import * as https from 'https' ;
5
+ import {
6
+ shouldUseStrictHttps ,
7
+ UPSTREAM_TLS_OPTIONS
8
+ } from 'mockttp/dist/rules/passthrough-handling' ;
5
9
6
10
export type RawHeaders = Array < [ key : string , value : string ] > ;
7
11
@@ -22,6 +26,34 @@ export interface RequestDefinition {
22
26
}
23
27
24
28
export interface RequestOptions {
29
+ /**
30
+ * A list of hostnames for which server certificate and TLS version errors
31
+ * should be ignored (none, by default).
32
+ *
33
+ * If set to 'true', HTTPS errors will be ignored for all hosts. WARNING:
34
+ * Use this at your own risk. Setting this to `true` can open your
35
+ * application to MITM attacks and should never be used over any network
36
+ * that is not completed trusted end-to-end.
37
+ */
38
+ ignoreHostHttpsErrors ?: string [ ] | boolean ;
39
+
40
+ /**
41
+ * An array of additional certificates, which should be trusted as certificate
42
+ * authorities for upstream hosts, in addition to Node.js's built-in certificate
43
+ * authorities.
44
+ *
45
+ * Each certificate should be an object with either a `cert` key and a string
46
+ * or buffer value containing the PEM certificate, or a `certPath` key and a
47
+ * string value containing the local path to the PEM certificate.
48
+ */
49
+ trustAdditionalCAs ?: Array < { cert : Buffer } > ;
50
+
51
+ /**
52
+ * A client certificate that should be used for the connection, if the server
53
+ * requests one during the TLS handshake.
54
+ */
55
+ clientCertificate ?: { pfx : Buffer , passphrase ?: string } ;
56
+
25
57
/**
26
58
* An abort signal, which can be used to cancel the in-process request if
27
59
* required.
@@ -67,9 +99,27 @@ export function sendRequest(
67
99
) : stream . Readable {
68
100
const url = new URL ( requestDefn . url ) ;
69
101
102
+ const strictHttpsChecks = shouldUseStrictHttps ( url . hostname ! , url . port ! , options . ignoreHostHttpsErrors ?? [ ] ) ;
103
+ const caConfig = options . trustAdditionalCAs
104
+ ? {
105
+ ca : tls . rootCertificates . concat (
106
+ options . trustAdditionalCAs . map ( ( { cert } ) => cert . toString ( 'utf8' ) )
107
+ )
108
+ }
109
+ : { } ;
110
+
70
111
const request = ( url . protocol === 'https' ? https : http ) . request ( requestDefn . url , {
71
112
method : requestDefn . method ,
72
- signal : options . abortSignal
113
+ signal : options . abortSignal ,
114
+
115
+ // TLS options (should be effectively identical to Mockttp's passthrough config)
116
+ ...UPSTREAM_TLS_OPTIONS ,
117
+ minVersion : strictHttpsChecks
118
+ ? tls . DEFAULT_MIN_VERSION
119
+ : 'TLSv1' , // Allow TLSv1, if !strict
120
+ rejectUnauthorized : strictHttpsChecks ,
121
+ ...caConfig ,
122
+ ...options . clientCertificate
73
123
} ) ;
74
124
75
125
options . abortSignal ?. addEventListener ( 'abort' , ( ) => {
0 commit comments