-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtls.ts
225 lines (206 loc) · 6.03 KB
/
tls.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-bitwise */
import { Options } from 'got-cjs';
const supportsFirefoxFully = Number(process.versions.node.split('.')[0]) >= 17;
const SSL_OP_TLSEXT_PADDING = 1 << 4;
const SSL_OP_NO_ENCRYPT_THEN_MAC = 1 << 19;
// OpenSSL supports secp256r1. It's just reffered to as prime256v1.
const ecdhCurve = {
firefox: (supportsFirefoxFully ? [
'X25519',
'prime256v1',
'secp384r1',
'secp521r1',
'ffdhe2048',
'ffdhe3072',
] : [
'X25519',
'prime256v1',
'secp384r1',
'secp521r1',
]).join(':'),
chrome: [
'X25519',
'prime256v1',
'secp384r1',
].join(':'),
safari: [
'X25519',
'prime256v1',
'secp384r1',
'secp521r1',
].join(':'),
} as const;
export const sigalgs = {
firefox: [
'ecdsa_secp256r1_sha256',
'ecdsa_secp384r1_sha384',
'ecdsa_secp521r1_sha512',
'rsa_pss_rsae_sha256',
'rsa_pss_rsae_sha384',
'rsa_pss_rsae_sha512',
'rsa_pkcs1_sha256',
'rsa_pkcs1_sha384',
'rsa_pkcs1_sha512',
'ECDSA+SHA1',
'rsa_pkcs1_sha1',
].join(':'),
chrome: [
'ecdsa_secp256r1_sha256',
'rsa_pss_rsae_sha256',
'rsa_pkcs1_sha256',
'ecdsa_secp384r1_sha384',
'rsa_pss_rsae_sha384',
'rsa_pkcs1_sha384',
'rsa_pss_rsae_sha512',
'rsa_pkcs1_sha512',
].join(':'),
safari: [
'ecdsa_secp256r1_sha256',
'rsa_pss_rsae_sha256',
'rsa_pkcs1_sha256',
'ecdsa_secp384r1_sha384',
'ECDSA+SHA1',
'rsa_pss_rsae_sha384',
'rsa_pkcs1_sha384',
'rsa_pss_rsae_sha512',
'rsa_pkcs1_sha512',
'RSA+SHA1',
].join(':'),
} as const;
export const knownCiphers = {
chrome: [
// Chrome v92
'TLS_AES_128_GCM_SHA256',
'TLS_AES_256_GCM_SHA384',
'TLS_CHACHA20_POLY1305_SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-CHACHA20-POLY1305',
'ECDHE-RSA-CHACHA20-POLY1305',
// Legacy:
'ECDHE-RSA-AES128-SHA',
'ECDHE-RSA-AES256-SHA',
'AES128-GCM-SHA256',
'AES256-GCM-SHA384',
'AES128-SHA',
'AES256-SHA',
].join(':'),
firefox: [
// Firefox v91
'TLS_AES_128_GCM_SHA256',
'TLS_CHACHA20_POLY1305_SHA256',
'TLS_AES_256_GCM_SHA384',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-CHACHA20-POLY1305',
'ECDHE-RSA-CHACHA20-POLY1305',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'ECDHE-RSA-AES256-GCM-SHA384',
// Legacy:
'ECDHE-ECDSA-AES256-SHA',
'ECDHE-ECDSA-AES128-SHA',
'ECDHE-RSA-AES128-SHA',
'ECDHE-RSA-AES256-SHA',
'AES128-GCM-SHA256',
'AES256-GCM-SHA384',
'AES128-SHA',
'AES256-SHA',
'DES-CBC3-SHA',
].join(':'),
safari: [
// Safari v14
'TLS_AES_128_GCM_SHA256',
'TLS_AES_256_GCM_SHA384',
'TLS_CHACHA20_POLY1305_SHA256',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-CHACHA20-POLY1305',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-CHACHA20-POLY1305',
// Legacy:
'ECDHE-ECDSA-AES256-SHA384',
'ECDHE-ECDSA-AES128-SHA256',
'ECDHE-ECDSA-AES256-SHA',
'ECDHE-ECDSA-AES128-SHA',
'ECDHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES128-SHA256',
'ECDHE-RSA-AES256-SHA',
'ECDHE-RSA-AES128-SHA',
'AES256-GCM-SHA384',
'AES128-GCM-SHA256',
'AES256-SHA256',
'AES128-SHA256',
'AES256-SHA',
'AES128-SHA',
'ECDHE-ECDSA-DES-CBC3-SHA',
'ECDHE-RSA-DES-CBC3-SHA',
'DES-CBC3-SHA',
].join(':'),
} as const;
export const minVersion = {
firefox: 'TLSv1.2',
chrome: 'TLSv1',
safari: 'TLSv1.2',
} as const;
export const maxVersion = {
firefox: 'TLSv1.3',
chrome: 'TLSv1.3',
safari: 'TLSv1.3',
} as const;
export const secureOptions = {
firefox: SSL_OP_TLSEXT_PADDING | SSL_OP_NO_ENCRYPT_THEN_MAC,
chrome: SSL_OP_TLSEXT_PADDING | SSL_OP_NO_ENCRYPT_THEN_MAC,
safari: SSL_OP_TLSEXT_PADDING | SSL_OP_NO_ENCRYPT_THEN_MAC,
} as const;
export const requestOCSP = {
firefox: true,
chrome: true,
safari: true,
};
type BrowserName = 'chrome' | 'firefox' | 'safari' | undefined;
const getUserAgent = (headers: Record<string, string | string[] | undefined>): string | undefined => {
for (const [header, value] of Object.entries(headers)) {
if (header.toLowerCase() === 'user-agent') {
return value as string;
}
}
return undefined;
};
const getBrowser = (userAgent?: string): BrowserName | undefined => {
if (!userAgent) {
return;
}
let browser;
if (userAgent.includes('Firefox')) {
browser = 'firefox';
} else if (userAgent.includes('Chrome')) {
browser = 'chrome';
} else {
browser = 'safari';
}
return browser as BrowserName;
};
export function tlsHook(options: Options): void {
const { https } = options;
if (https.ciphers || https.signatureAlgorithms || https.minVersion || https.maxVersion) {
return;
}
// Let's default to Firefox settings as it has low failure rates
const browser = getBrowser(getUserAgent(options.headers)) ?? 'firefox';
https.ciphers = knownCiphers[browser];
https.signatureAlgorithms = sigalgs[browser];
https.ecdhCurve = ecdhCurve[browser];
https.minVersion = minVersion[browser];
https.maxVersion = maxVersion[browser];
// @ts-expect-error Private use
options._unixOptions = {
// @ts-expect-error Private use
...options._unixOptions,
secureOptions: secureOptions[browser],
requestOCSP: requestOCSP[browser],
};
}