cf-workers-native 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/documents/page-source.js +1 -1
- package/dist/browser/fetch/host-call.js +6 -1
- package/dist/browser/fetch/native-network.d.ts +1 -1
- package/dist/browser/fetch/relay.js +2 -0
- package/dist/browser/isolated/transport.js +3 -2
- package/dist/browser/request/index.js +3 -2
- package/dist/cloudflare/workers/worker-runtime.js +2 -1
- package/dist/compat/console/index.d.ts +35 -0
- package/dist/compat/console/index.js +92 -0
- package/dist/compat/crypto/LICENSE-3RD-PARTY.md +19 -0
- package/dist/compat/crypto/LICENSE-APACHE +177 -0
- package/dist/compat/crypto/LICENSE.emscripten +102 -0
- package/dist/compat/crypto/LICENSE.musl +193 -0
- package/dist/compat/crypto/LICENSE.openssl +177 -0
- package/dist/compat/crypto/asymmetric.d.ts +13 -0
- package/dist/compat/crypto/asymmetric.js +239 -0
- package/dist/compat/crypto/brorand.cjs +74 -0
- package/dist/compat/crypto/brorand.d.cts +6 -0
- package/dist/compat/crypto/certificate.d.ts +14 -0
- package/dist/compat/crypto/certificate.js +66 -0
- package/dist/compat/crypto/ecdh.d.ts +15 -0
- package/dist/compat/crypto/ecdh.js +109 -0
- package/dist/compat/crypto/hkdf.d.ts +2 -0
- package/dist/compat/crypto/hkdf.js +52 -0
- package/dist/compat/crypto/index.cjs +2 -0
- package/dist/compat/crypto/index.d.cts +2 -0
- package/dist/compat/crypto/index.d.ts +30 -0
- package/dist/compat/crypto/index.js +83 -0
- package/dist/compat/crypto/key-formats.d.ts +6 -0
- package/dist/compat/crypto/key-formats.js +120 -0
- package/dist/compat/crypto/key-state.d.ts +9 -0
- package/dist/compat/crypto/key-state.js +4 -0
- package/dist/compat/crypto/keys.d.ts +50 -0
- package/dist/compat/crypto/keys.js +209 -0
- package/dist/compat/crypto/metadata.d.ts +4 -0
- package/dist/compat/crypto/metadata.js +22 -0
- package/dist/compat/crypto/native-crypto.d.ts +68 -0
- package/dist/compat/crypto/native-crypto.js +569 -0
- package/dist/compat/crypto/prime.d.ts +19 -0
- package/dist/compat/crypto/prime.js +146 -0
- package/dist/compat/crypto/random.d.ts +7 -0
- package/dist/compat/crypto/random.js +123 -0
- package/dist/compat/crypto/rsa.d.ts +5 -0
- package/dist/compat/crypto/rsa.js +173 -0
- package/dist/compat/crypto/scrypt.d.ts +25 -0
- package/dist/compat/crypto/scrypt.js +150 -0
- package/dist/compat/crypto/sign-errors.d.ts +8 -0
- package/dist/compat/crypto/sign-errors.js +13 -0
- package/dist/compat/crypto/sign-handle.d.ts +22 -0
- package/dist/compat/crypto/sign-handle.js +105 -0
- package/dist/compat/crypto/signatures.d.ts +28 -0
- package/dist/compat/crypto/signatures.js +252 -0
- package/dist/compat/crypto/symmetric.d.ts +29 -0
- package/dist/compat/crypto/symmetric.js +104 -0
- package/dist/compat/crypto/validation.d.ts +3 -0
- package/dist/compat/crypto/validation.js +19 -0
- package/dist/compat/crypto/x509.d.ts +30 -0
- package/dist/compat/crypto/x509.js +406 -0
- package/dist/compat/events/errors.d.ts +8 -0
- package/dist/compat/events/errors.js +39 -0
- package/dist/compat/events/index.cjs +2 -0
- package/dist/compat/events/index.d.cts +2 -0
- package/dist/compat/events/index.d.ts +64 -0
- package/dist/compat/events/index.js +792 -0
- package/dist/compat/events/intrinsics.d.ts +1 -0
- package/dist/compat/events/intrinsics.js +2 -0
- package/dist/compat/fs/embedded.d.ts +85 -0
- package/dist/compat/fs/embedded.js +255 -0
- package/dist/compat/fs/index.d.ts +7 -84
- package/dist/compat/fs/index.js +218 -252
- package/dist/compat/fs/mode.d.ts +1 -0
- package/dist/compat/fs/mode.js +13 -0
- package/dist/compat/fs/promises/index.d.ts +1 -1
- package/dist/compat/fs/promises/index.js +1 -1
- package/dist/compat/fs/writable.d.ts +6 -0
- package/dist/compat/fs/writable.js +142 -0
- package/dist/compat/http/symbols.d.ts +5 -0
- package/dist/compat/http/symbols.js +9 -0
- package/dist/compat/http-common/index.d.ts +37 -0
- package/dist/compat/http-common/index.js +43 -0
- package/dist/compat/http-outgoing/index.d.ts +13 -0
- package/dist/compat/http-outgoing/index.js +32 -0
- package/dist/compat/http-server/index.d.ts +18 -0
- package/dist/compat/http-server/index.js +107 -0
- package/dist/compat/module/builtins.d.ts +1 -0
- package/dist/compat/module/builtins.js +101 -0
- package/dist/compat/module/index.d.ts +4 -2
- package/dist/compat/module/index.js +22 -17
- package/dist/compat/module/metadata.d.ts +44 -0
- package/dist/compat/module/metadata.js +127 -0
- package/dist/compat/net/internal-net.js +4 -0
- package/dist/compat/net/sockets.d.ts +5 -0
- package/dist/compat/net/sockets.js +74 -13
- package/dist/compat/process/index.cjs +2 -0
- package/dist/compat/process/index.d.cts +4 -0
- package/dist/compat/process/index.d.ts +18 -0
- package/dist/compat/process/index.js +140 -0
- package/dist/compat/process/metadata.d.ts +135 -0
- package/dist/compat/process/metadata.js +251 -0
- package/dist/compat/process/state.d.ts +4 -0
- package/dist/compat/process/state.js +4 -0
- package/dist/compat/stream-web/LICENSE +22 -0
- package/dist/compat/stream-web/ponyfill.d.ts +1 -1
- package/dist/compat/stream-web/ponyfill.js +2369 -1775
- package/dist/compat/stream-web/runtime.d.ts +1 -1
- package/dist/compat/stream-web/runtime.js +3 -2
- package/dist/compat/stream-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/stream-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/stream-wrap/index.d.ts +1 -0
- package/dist/compat/stream-wrap/index.js +1 -0
- package/dist/compat/tls/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls/LICENSE-APACHE +177 -0
- package/dist/compat/tls/constants.d.ts +7 -0
- package/dist/compat/tls/constants.js +14 -0
- package/dist/compat/tls/errors.d.ts +35 -0
- package/dist/compat/tls/errors.js +68 -0
- package/dist/compat/tls/helpers.d.ts +9 -0
- package/dist/compat/tls/helpers.js +207 -0
- package/dist/compat/tls/index.d.ts +26 -0
- package/dist/compat/tls/index.js +50 -0
- package/dist/compat/tls/jsstream.d.ts +19 -0
- package/dist/compat/tls/jsstream.js +186 -0
- package/dist/compat/tls/native-session.d.ts +3 -0
- package/dist/compat/tls/native-session.js +187 -0
- package/dist/compat/tls/wrap.d.ts +65 -0
- package/dist/compat/tls/wrap.js +544 -0
- package/dist/compat/tls-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/tls-wrap/index.d.ts +7 -0
- package/dist/compat/tls-wrap/index.js +3 -0
- package/dist/compat/util/index.js +43 -1
- package/dist/compat/util/types/index.d.ts +3 -0
- package/dist/compat/util/types/index.js +5 -0
- package/dist/compat/zlib/LICENSE.brotli +19 -0
- package/dist/compat/zlib/LICENSE.emscripten +102 -0
- package/dist/compat/zlib/LICENSE.musl +193 -0
- package/dist/compat/zlib/LICENSE.zstd +30 -0
- package/dist/compat/zlib/compression.d.ts +55 -0
- package/dist/compat/zlib/compression.js +259 -0
- package/dist/compat/zlib/constants.d.ts +172 -0
- package/dist/compat/zlib/constants.js +173 -0
- package/dist/compat/zlib/index.d.ts +180 -0
- package/dist/compat/zlib/index.js +177 -0
- package/dist/compat/zlib/native-codecs.d.ts +17 -0
- package/dist/compat/zlib/native-codecs.js +168 -0
- package/dist/shared/globals/inject.d.ts +2 -1
- package/dist/shared/globals/inject.js +2 -1
- package/dist/shared/globals/runtime.d.ts +1 -2
- package/dist/shared/globals/runtime.js +1 -2
- package/dist/shared/request-scope.d.ts +4 -0
- package/dist/shared/request-scope.js +11 -0
- package/dist/tooling/async.js +36 -14
- package/dist/tooling/shared.d.ts +1 -0
- package/dist/tooling/shared.js +20 -1
- package/dist/tooling/vite.js +4 -3
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +56 -8
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersCapacitorPlugin.java +4 -2
- package/native/ios/NetworkBridge.swift +11 -1
- package/native/ios/SocketBridge.swift +37 -2
- package/native/ios/WorkersCapacitorPlugin.swift +4 -0
- package/package.json +3 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { isIP } from "../net/internal-net.js";
|
|
4
|
+
const urlUtil = {
|
|
5
|
+
canonicalizeIp(ip) {
|
|
6
|
+
// URL's IPv6 serializer expands equivalent spellings to one canonical form.
|
|
7
|
+
if (isIP(ip) === 6)
|
|
8
|
+
return new URL(`http://[${ip}]/`).hostname.slice(1, -1);
|
|
9
|
+
return ip;
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
import { ERR_TLS_CERT_ALTNAME_INVALID, ERR_TLS_CERT_ALTNAME_FORMAT, ERR_OUT_OF_RANGE, } from "./errors.js";
|
|
13
|
+
import { isUint8Array, isArrayBufferView } from "../util/types/index.js";
|
|
14
|
+
// String#toLowerCase() is locale-sensitive so we use
|
|
15
|
+
// a conservative version that only lowercases A-Z.
|
|
16
|
+
function toLowerCase(c) {
|
|
17
|
+
return String.fromCharCode(32 + c.charCodeAt(0));
|
|
18
|
+
}
|
|
19
|
+
function unfqdn(host) {
|
|
20
|
+
return host.replace(/[.]$/, "");
|
|
21
|
+
}
|
|
22
|
+
function splitHost(host) {
|
|
23
|
+
return unfqdn(host).replace(/[A-Z]/g, toLowerCase).split(".");
|
|
24
|
+
}
|
|
25
|
+
function check(hostParts, pattern, wildcards) {
|
|
26
|
+
// Empty strings, null, undefined, etc. never match.
|
|
27
|
+
if (!pattern)
|
|
28
|
+
return false;
|
|
29
|
+
const patternParts = splitHost(pattern);
|
|
30
|
+
if (hostParts.length !== patternParts.length)
|
|
31
|
+
return false;
|
|
32
|
+
// Pattern has empty components, e.g. "bad..example.com".
|
|
33
|
+
if (patternParts.includes(""))
|
|
34
|
+
return false;
|
|
35
|
+
// RFC 6125 allows IDNA U-labels (Unicode) in names but we have no
|
|
36
|
+
// good way to detect their encoding or normalize them so we simply
|
|
37
|
+
// reject them. Control characters and blanks are rejected as well
|
|
38
|
+
// because nothing good can come from accepting them.
|
|
39
|
+
const isBad = (s) => /[^\u0021-\u007F]/u.test(s);
|
|
40
|
+
if (patternParts.some(isBad))
|
|
41
|
+
return false;
|
|
42
|
+
// Check host parts from right to left first.
|
|
43
|
+
for (let i = hostParts.length - 1; i > 0; i -= 1) {
|
|
44
|
+
if (hostParts[i] !== patternParts[i])
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const hostSubdomain = hostParts[0];
|
|
48
|
+
const patternSubdomain = patternParts[0];
|
|
49
|
+
const patternSubdomainParts = patternSubdomain.split("*", 3);
|
|
50
|
+
// Short-circuit when the subdomain does not contain a wildcard.
|
|
51
|
+
// RFC 6125 does not allow wildcard substitution for components
|
|
52
|
+
// containing IDNA A-labels (Punycode) so match those verbatim.
|
|
53
|
+
if (patternSubdomainParts.length === 1 || patternSubdomain.includes("xn--"))
|
|
54
|
+
return hostSubdomain === patternSubdomain;
|
|
55
|
+
if (!wildcards)
|
|
56
|
+
return false;
|
|
57
|
+
// More than one wildcard is always wrong.
|
|
58
|
+
if (patternSubdomainParts.length > 2)
|
|
59
|
+
return false;
|
|
60
|
+
// *.tld wildcards are not allowed.
|
|
61
|
+
if (patternParts.length <= 2)
|
|
62
|
+
return false;
|
|
63
|
+
const { 0: prefix, 1: suffix } = patternSubdomainParts;
|
|
64
|
+
if (prefix.length + suffix.length > hostSubdomain.length)
|
|
65
|
+
return false;
|
|
66
|
+
if (!hostSubdomain.startsWith(prefix))
|
|
67
|
+
return false;
|
|
68
|
+
if (!hostSubdomain.endsWith(suffix))
|
|
69
|
+
return false;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
// This pattern is used to determine the length of escaped sequences within
|
|
73
|
+
// the subject alt names string. It allows any valid JSON string literal.
|
|
74
|
+
// This MUST match the JSON specification (ECMA-404 / RFC8259) exactly.
|
|
75
|
+
const jsonStringPattern =
|
|
76
|
+
// eslint-disable-next-line no-control-regex
|
|
77
|
+
/^"(?:[^"\\\u0000-\u001f]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4}))*"/;
|
|
78
|
+
function splitEscapedAltNames(altNames) {
|
|
79
|
+
const result = [];
|
|
80
|
+
let currentToken = "";
|
|
81
|
+
let offset = 0;
|
|
82
|
+
while (offset !== altNames.length) {
|
|
83
|
+
const nextSep = altNames.indexOf(",", offset);
|
|
84
|
+
const nextQuote = altNames.indexOf('"', offset);
|
|
85
|
+
if (nextQuote !== -1 && (nextSep === -1 || nextQuote < nextSep)) {
|
|
86
|
+
// There is a quote character and there is no separator before the quote.
|
|
87
|
+
currentToken += altNames.substring(offset, nextQuote);
|
|
88
|
+
const match = jsonStringPattern.exec(altNames.substring(nextQuote));
|
|
89
|
+
if (!match) {
|
|
90
|
+
throw new ERR_TLS_CERT_ALTNAME_FORMAT();
|
|
91
|
+
}
|
|
92
|
+
currentToken += JSON.parse(match[0]);
|
|
93
|
+
offset = nextQuote + match[0].length;
|
|
94
|
+
}
|
|
95
|
+
else if (nextSep !== -1) {
|
|
96
|
+
// There is a separator and no quote before it.
|
|
97
|
+
currentToken += altNames.substring(offset, nextSep);
|
|
98
|
+
result.push(currentToken);
|
|
99
|
+
currentToken = "";
|
|
100
|
+
offset = nextSep + 2;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
currentToken += altNames.substring(offset);
|
|
104
|
+
offset = altNames.length;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
result.push(currentToken);
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
export function checkServerIdentity(hostname, cert) {
|
|
111
|
+
const subject = cert.subject;
|
|
112
|
+
const altNames = cert.subjectaltname;
|
|
113
|
+
const dnsNames = [];
|
|
114
|
+
const ips = [];
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-conversion
|
|
116
|
+
hostname = "" + hostname;
|
|
117
|
+
if (altNames) {
|
|
118
|
+
const splitAltNames = altNames.includes('"')
|
|
119
|
+
? splitEscapedAltNames(altNames)
|
|
120
|
+
: altNames.split(", ");
|
|
121
|
+
splitAltNames.forEach((name) => {
|
|
122
|
+
if (name.startsWith("DNS:")) {
|
|
123
|
+
dnsNames.push(name.slice(4));
|
|
124
|
+
}
|
|
125
|
+
else if (name.startsWith("IP Address:")) {
|
|
126
|
+
ips.push(urlUtil.canonicalizeIp(name.slice(11)));
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
let valid = false;
|
|
131
|
+
let reason = "Unknown reason";
|
|
132
|
+
hostname = unfqdn(hostname); // Remove trailing dot for error messages.
|
|
133
|
+
if (isIP(hostname)) {
|
|
134
|
+
valid = ips.includes(urlUtil.canonicalizeIp(hostname));
|
|
135
|
+
if (!valid)
|
|
136
|
+
reason = `IP: ${hostname} is not in the cert's list: ` + ips.join(", ");
|
|
137
|
+
}
|
|
138
|
+
else if (dnsNames.length > 0 || subject?.CN) {
|
|
139
|
+
const hostParts = splitHost(hostname);
|
|
140
|
+
const wildcard = (pattern) => check(hostParts, pattern, true);
|
|
141
|
+
if (dnsNames.length > 0) {
|
|
142
|
+
valid = dnsNames.some(wildcard);
|
|
143
|
+
if (!valid)
|
|
144
|
+
reason = `Host: ${hostname}. is not in the cert's altnames: ${altNames}`;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
// Match against Common Name only if no supported identifiers exist.
|
|
148
|
+
const cn = subject?.CN;
|
|
149
|
+
if (Array.isArray(cn))
|
|
150
|
+
valid = cn.some(wildcard);
|
|
151
|
+
else if (cn)
|
|
152
|
+
valid = wildcard(cn);
|
|
153
|
+
if (!valid)
|
|
154
|
+
reason = `Host: ${hostname}. is not cert's CN: ${cn}`;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
reason = "Cert does not contain a DNS name";
|
|
159
|
+
}
|
|
160
|
+
if (!valid) {
|
|
161
|
+
return new ERR_TLS_CERT_ALTNAME_INVALID(reason, hostname, cert);
|
|
162
|
+
}
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
// Convert protocols array into valid OpenSSL protocols list
|
|
166
|
+
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
|
|
167
|
+
function convertProtocols(protocols) {
|
|
168
|
+
const lens = Array.from({ length: protocols.length }, () => 0);
|
|
169
|
+
const buff = Buffer.allocUnsafe(protocols.reduce((p, c, i) => {
|
|
170
|
+
const len = Buffer.byteLength(c);
|
|
171
|
+
if (len > 255) {
|
|
172
|
+
throw new ERR_OUT_OF_RANGE("The byte length of the protocol at index " +
|
|
173
|
+
`${i} exceeds the maximum length.`, "<= 255", len, true);
|
|
174
|
+
}
|
|
175
|
+
lens[i] = len;
|
|
176
|
+
return p + 1 + len;
|
|
177
|
+
}, 0));
|
|
178
|
+
let offset = 0;
|
|
179
|
+
for (let i = 0, c = protocols.length; i < c; i++) {
|
|
180
|
+
buff[offset++] = lens[i];
|
|
181
|
+
buff.write(protocols[i], offset);
|
|
182
|
+
offset += lens[i];
|
|
183
|
+
}
|
|
184
|
+
return buff;
|
|
185
|
+
}
|
|
186
|
+
export function convertALPNProtocols(protocols, out) {
|
|
187
|
+
// If protocols is Array - translate it into buffer
|
|
188
|
+
if (Array.isArray(protocols)) {
|
|
189
|
+
out.ALPNProtocols = convertProtocols(protocols);
|
|
190
|
+
}
|
|
191
|
+
else if (isUint8Array(protocols)) {
|
|
192
|
+
// Copy new buffer not to be modified by user.
|
|
193
|
+
out.ALPNProtocols = Buffer.from(protocols);
|
|
194
|
+
}
|
|
195
|
+
else if (isArrayBufferView(protocols)) {
|
|
196
|
+
out.ALPNProtocols = Buffer.from(protocols.buffer.slice(protocols.byteOffset, protocols.byteOffset + protocols.byteLength));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
export function createServer() {
|
|
200
|
+
throw new Error("Not implemented");
|
|
201
|
+
}
|
|
202
|
+
export function Server() {
|
|
203
|
+
throw new Error("Not implemented");
|
|
204
|
+
}
|
|
205
|
+
export function getCiphers() {
|
|
206
|
+
throw new Error("Not implemented");
|
|
207
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { checkServerIdentity, convertALPNProtocols, createServer, Server, getCiphers } from "./helpers.js";
|
|
2
|
+
import { createSecureContext, SecureContext } from "../tls-common/index.js";
|
|
3
|
+
import { TLSSocket, connect } from "./wrap.js";
|
|
4
|
+
declare let createSecurePair: any;
|
|
5
|
+
export * from "./constants.js";
|
|
6
|
+
export { TLSSocket, connect, createSecureContext, createServer, checkServerIdentity, SecureContext, Server, convertALPNProtocols, getCiphers, createSecurePair, };
|
|
7
|
+
declare const _default: {
|
|
8
|
+
CLIENT_RENEG_LIMIT: 3;
|
|
9
|
+
CLIENT_RENEG_WINDOW: 600;
|
|
10
|
+
DEFAULT_CIPHERS: "";
|
|
11
|
+
DEFAULT_ECDH_CURVE: "auto";
|
|
12
|
+
DEFAULT_MIN_VERSION: "TLSv1.2";
|
|
13
|
+
DEFAULT_MAX_VERSION: "TLSv1.3";
|
|
14
|
+
rootCertificates: never[];
|
|
15
|
+
SecureContext: typeof SecureContext;
|
|
16
|
+
Server: typeof Server;
|
|
17
|
+
TLSSocket: typeof TLSSocket;
|
|
18
|
+
connect: typeof connect;
|
|
19
|
+
createSecureContext: typeof createSecureContext;
|
|
20
|
+
createServer: typeof createServer;
|
|
21
|
+
checkServerIdentity: typeof checkServerIdentity;
|
|
22
|
+
convertALPNProtocols: typeof convertALPNProtocols;
|
|
23
|
+
getCiphers: typeof getCiphers;
|
|
24
|
+
createSecurePair: () => void;
|
|
25
|
+
};
|
|
26
|
+
export default _default;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Copyright (c) 2017-2022 Cloudflare, Inc.
|
|
2
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
3
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
6
|
+
//
|
|
7
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
8
|
+
// copy of this software and associated documentation files (the
|
|
9
|
+
// "Software"), to deal in the Software without restriction, including
|
|
10
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
12
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
13
|
+
// following conditions:
|
|
14
|
+
//
|
|
15
|
+
// The above copyright notice and this permission notice shall be included
|
|
16
|
+
// in all copies or substantial portions of the Software.
|
|
17
|
+
//
|
|
18
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
19
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
21
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
22
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
23
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
24
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
import { checkServerIdentity, convertALPNProtocols, createServer, Server, getCiphers, } from "./helpers.js";
|
|
26
|
+
import { createSecureContext, SecureContext } from "../tls-common/index.js";
|
|
27
|
+
import * as constants from "./constants.js";
|
|
28
|
+
import { TLSSocket, connect } from "./wrap.js";
|
|
29
|
+
import { ERR_METHOD_NOT_IMPLEMENTED } from "./errors.js";
|
|
30
|
+
let createSecurePair = undefined;
|
|
31
|
+
if (true) {
|
|
32
|
+
createSecurePair = function createSecurePair() {
|
|
33
|
+
throw new ERR_METHOD_NOT_IMPLEMENTED("createSecurePair");
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export * from "./constants.js";
|
|
37
|
+
export { TLSSocket, connect, createSecureContext, createServer, checkServerIdentity, SecureContext, Server, convertALPNProtocols, getCiphers, createSecurePair, };
|
|
38
|
+
export default {
|
|
39
|
+
SecureContext,
|
|
40
|
+
Server,
|
|
41
|
+
TLSSocket,
|
|
42
|
+
connect,
|
|
43
|
+
createSecureContext,
|
|
44
|
+
createServer,
|
|
45
|
+
checkServerIdentity,
|
|
46
|
+
convertALPNProtocols,
|
|
47
|
+
getCiphers,
|
|
48
|
+
createSecurePair,
|
|
49
|
+
...constants,
|
|
50
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Socket } from "../net/internal-net.js";
|
|
2
|
+
declare const kCurrentWriteRequest: unique symbol;
|
|
3
|
+
declare const kCurrentShutdownRequest: unique symbol;
|
|
4
|
+
declare const kPendingShutdownRequest: unique symbol;
|
|
5
|
+
declare const kPendingClose: unique symbol;
|
|
6
|
+
export declare class JSStreamSocket extends Socket {
|
|
7
|
+
stream: Duplex;
|
|
8
|
+
[kCurrentWriteRequest]: null | unknown;
|
|
9
|
+
[kCurrentShutdownRequest]: null | unknown;
|
|
10
|
+
[kPendingShutdownRequest]: null | unknown;
|
|
11
|
+
[kPendingClose]: boolean;
|
|
12
|
+
constructor(stream: Duplex);
|
|
13
|
+
isClosing(): boolean;
|
|
14
|
+
readStart(): number;
|
|
15
|
+
readStop(): number;
|
|
16
|
+
doShutdown(req: unknown): number;
|
|
17
|
+
doClose(): void;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// Copyright (c) 2017-2022 Cloudflare, Inc.
|
|
2
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
3
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
6
|
+
//
|
|
7
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
8
|
+
// copy of this software and associated documentation files (the
|
|
9
|
+
// "Software"), to deal in the Software without restriction, including
|
|
10
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
12
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
13
|
+
// following conditions:
|
|
14
|
+
//
|
|
15
|
+
// The above copyright notice and this permission notice shall be included
|
|
16
|
+
// in all copies or substantial portions of the Software.
|
|
17
|
+
//
|
|
18
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
19
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
21
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
22
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
23
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
24
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
|
|
26
|
+
import { notStrictEqual } from "../assert/index.js";
|
|
27
|
+
import { Socket } from "../net/internal-net.js";
|
|
28
|
+
import { ERR_STREAM_WRAP } from "./errors.js";
|
|
29
|
+
import { toBYOBWeb } from "../stream/duplex.js";
|
|
30
|
+
const kCurrentWriteRequest = Symbol("kCurrentWriteRequest");
|
|
31
|
+
const kCurrentShutdownRequest = Symbol("kCurrentShutdownRequest");
|
|
32
|
+
const kPendingShutdownRequest = Symbol("kPendingShutdownRequest");
|
|
33
|
+
const kPendingClose = Symbol("kPendingClose");
|
|
34
|
+
/* This class serves as a wrapper for when the C++ side of Node wants access
|
|
35
|
+
* to a standard JS stream. For example, TLS or HTTP do not operate on network
|
|
36
|
+
* resources conceptually, although that is the common case and what we are
|
|
37
|
+
* optimizing for; in theory, they are completely composable and can work with
|
|
38
|
+
* any stream resource they see.
|
|
39
|
+
*
|
|
40
|
+
* For the common case, i.e. a TLS socket wrapping around a net.Socket, we
|
|
41
|
+
* can skip going through the JS layer and let TLS access the raw C++ handle
|
|
42
|
+
* of a net.Socket. The flipside of this is that, to maintain composability,
|
|
43
|
+
* we need a way to create "fake" net.Socket instances that call back into a
|
|
44
|
+
* "real" JavaScript stream. JSStreamSocket is exactly this.
|
|
45
|
+
*/
|
|
46
|
+
export class JSStreamSocket extends Socket {
|
|
47
|
+
stream;
|
|
48
|
+
[kCurrentWriteRequest];
|
|
49
|
+
[kCurrentShutdownRequest];
|
|
50
|
+
[kPendingShutdownRequest];
|
|
51
|
+
[kPendingClose];
|
|
52
|
+
constructor(stream) {
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
54
|
+
const closePromise = Promise.withResolvers();
|
|
55
|
+
const openPromise = Promise.withResolvers();
|
|
56
|
+
const webStream = toBYOBWeb(stream);
|
|
57
|
+
Object.assign(webStream.writable, {
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
59
|
+
write: async (data) => {
|
|
60
|
+
stream.write(data);
|
|
61
|
+
},
|
|
62
|
+
closed: closePromise.promise,
|
|
63
|
+
releaseLock: async () => { },
|
|
64
|
+
});
|
|
65
|
+
const handle = {
|
|
66
|
+
reading: true,
|
|
67
|
+
bytesRead: 0,
|
|
68
|
+
bytesWritten: 0,
|
|
69
|
+
socket: {
|
|
70
|
+
startTls() {
|
|
71
|
+
throw new Error("startTls() should not be called for a duplex stream");
|
|
72
|
+
},
|
|
73
|
+
upgraded: false,
|
|
74
|
+
secureTransport: "off",
|
|
75
|
+
closed: closePromise.promise,
|
|
76
|
+
close: async () => {
|
|
77
|
+
queueMicrotask(() => {
|
|
78
|
+
closePromise.resolve();
|
|
79
|
+
});
|
|
80
|
+
return closePromise.promise;
|
|
81
|
+
},
|
|
82
|
+
opened: openPromise.promise,
|
|
83
|
+
readable: webStream.readable,
|
|
84
|
+
writable: webStream.writable,
|
|
85
|
+
},
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
87
|
+
reader: new ReadableStreamBYOBReader(webStream.readable),
|
|
88
|
+
writer: new WritableStreamDefaultWriter(webStream.writable),
|
|
89
|
+
options: {
|
|
90
|
+
host: "0.0.0.0",
|
|
91
|
+
port: 0,
|
|
92
|
+
addressType: 4,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
stream.pause();
|
|
96
|
+
stream.on("error", (err) => this.emit("error", err));
|
|
97
|
+
const ondata = (chunk) => {
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
|
|
99
|
+
if (typeof chunk === "string" || stream.readableObjectMode === true) {
|
|
100
|
+
// Make sure that no further `data` events will happen.
|
|
101
|
+
stream.pause();
|
|
102
|
+
stream.removeListener("data", ondata);
|
|
103
|
+
this.emit("error", new ERR_STREAM_WRAP());
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
// TODO(soon): We need to trigger read() result for _handle.reader.read(buf) call
|
|
107
|
+
// in node:net.
|
|
108
|
+
};
|
|
109
|
+
stream.on("data", ondata);
|
|
110
|
+
stream.once("end", () => {
|
|
111
|
+
closePromise.resolve();
|
|
112
|
+
});
|
|
113
|
+
// Some `Stream` don't pass `hasError` parameters when closed.
|
|
114
|
+
stream.once("close", () => {
|
|
115
|
+
// Errors emitted from `stream` have also been emitted to this instance
|
|
116
|
+
// so that we don't pass errors to `destroy()` again.
|
|
117
|
+
this.destroy();
|
|
118
|
+
});
|
|
119
|
+
super({ handle });
|
|
120
|
+
this.stream = stream;
|
|
121
|
+
this[kCurrentWriteRequest] = null;
|
|
122
|
+
this[kCurrentShutdownRequest] = null;
|
|
123
|
+
this[kPendingShutdownRequest] = null;
|
|
124
|
+
this[kPendingClose] = false;
|
|
125
|
+
this.readable = stream.readable;
|
|
126
|
+
this.writable = stream.writable;
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
128
|
+
handle.socket.closed.then(this.doClose.bind(this));
|
|
129
|
+
openPromise.resolve({});
|
|
130
|
+
// Start reading.
|
|
131
|
+
this.read(0);
|
|
132
|
+
}
|
|
133
|
+
isClosing() {
|
|
134
|
+
return !this.readable || !this.writable;
|
|
135
|
+
}
|
|
136
|
+
readStart() {
|
|
137
|
+
this.stream.resume();
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
readStop() {
|
|
141
|
+
this.stream.pause();
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
doShutdown(req) {
|
|
145
|
+
// TODO(addaleax): It might be nice if we could get into a state where
|
|
146
|
+
// DoShutdown() is not called on streams while a write is still pending.
|
|
147
|
+
//
|
|
148
|
+
// Currently, the only part of the code base where that happens is the
|
|
149
|
+
// TLS implementation, which calls both DoWrite() and DoShutdown() on the
|
|
150
|
+
// underlying network stream inside of its own DoShutdown() method.
|
|
151
|
+
// Working around that on the native side is not quite trivial (yet?),
|
|
152
|
+
// so for now that is supported here.
|
|
153
|
+
if (this[kCurrentWriteRequest] !== null) {
|
|
154
|
+
this[kPendingShutdownRequest] = req;
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
this[kCurrentShutdownRequest] = req;
|
|
158
|
+
if (this[kPendingClose]) {
|
|
159
|
+
// If doClose is pending, the stream & this._handle are gone. We can't do
|
|
160
|
+
// anything. doClose will call finishShutdown with ECANCELED for us shortly.
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
const handle = this._handle;
|
|
164
|
+
notStrictEqual(handle, null);
|
|
165
|
+
queueMicrotask(() => {
|
|
166
|
+
// Ensure that write is dispatched asynchronously.
|
|
167
|
+
this.stream.end();
|
|
168
|
+
});
|
|
169
|
+
return 0;
|
|
170
|
+
}
|
|
171
|
+
doClose() {
|
|
172
|
+
this[kPendingClose] = true;
|
|
173
|
+
const handle = this._handle;
|
|
174
|
+
// When sockets of the "net" module destroyed, they will call
|
|
175
|
+
// `this._handle.close()` which will also emit EOF if not emitted before.
|
|
176
|
+
// This feature makes sockets on the other side emit "end" and "close"
|
|
177
|
+
// even though we haven't called `end()`. As `stream` are likely to be
|
|
178
|
+
// instances of `net.Socket`, calling `stream.destroy()` manually will
|
|
179
|
+
// avoid issues that don't properly close wrapped connections.
|
|
180
|
+
this.stream.destroy();
|
|
181
|
+
queueMicrotask(() => {
|
|
182
|
+
notStrictEqual(handle, null);
|
|
183
|
+
this[kPendingClose] = false;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { NativeTLS } from "../crypto/native-crypto.js";
|
|
2
|
+
function decode(value) {
|
|
3
|
+
return Uint8Array.from(atob(value), (character) => character.charCodeAt(0));
|
|
4
|
+
}
|
|
5
|
+
function encode(value) {
|
|
6
|
+
let text = "";
|
|
7
|
+
for (let i = 0; i < value.length; i += 8192)
|
|
8
|
+
text += String.fromCharCode(...value.subarray(i, i + 8192));
|
|
9
|
+
return btoa(text);
|
|
10
|
+
}
|
|
11
|
+
/** Memory-BIO encryption on the original TCP stream, with mandatory OS trust. */
|
|
12
|
+
export async function nativeTlsSession(call, id, hostname) {
|
|
13
|
+
const tls = new NativeTLS(hostname);
|
|
14
|
+
let stopped = false, eof = false, pumping = false;
|
|
15
|
+
let buffered;
|
|
16
|
+
let incoming;
|
|
17
|
+
let outgoing = Promise.resolve();
|
|
18
|
+
let reading;
|
|
19
|
+
let resolveWatch, rejectWatch;
|
|
20
|
+
const watched = new Promise((resolve, reject) => {
|
|
21
|
+
resolveWatch = resolve;
|
|
22
|
+
rejectWatch = reject;
|
|
23
|
+
});
|
|
24
|
+
void watched.catch(() => { });
|
|
25
|
+
const stop = (error) => {
|
|
26
|
+
if (stopped)
|
|
27
|
+
return;
|
|
28
|
+
stopped = true;
|
|
29
|
+
tls.dispose();
|
|
30
|
+
buffered = undefined;
|
|
31
|
+
reading?.reject(error ?? new Error("TLS session closed"));
|
|
32
|
+
reading = undefined;
|
|
33
|
+
if (error)
|
|
34
|
+
rejectWatch(error);
|
|
35
|
+
else
|
|
36
|
+
resolveWatch({});
|
|
37
|
+
};
|
|
38
|
+
const flush = () => {
|
|
39
|
+
for (const data of tls.drain()) {
|
|
40
|
+
outgoing = outgoing.then(async () => {
|
|
41
|
+
if (stopped)
|
|
42
|
+
throw new Error("TLS session closed");
|
|
43
|
+
await call("WorkersCapacitor", "socketWrite", {
|
|
44
|
+
id,
|
|
45
|
+
chunk: encode(data),
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return outgoing;
|
|
50
|
+
};
|
|
51
|
+
const receive = () => (incoming ??= (async () => {
|
|
52
|
+
const value = (await call("WorkersCapacitor", "socketRead", {
|
|
53
|
+
id,
|
|
54
|
+
maxBytes: 65536,
|
|
55
|
+
}));
|
|
56
|
+
if (stopped)
|
|
57
|
+
throw new Error("TLS session closed");
|
|
58
|
+
if (value.eof)
|
|
59
|
+
throw new Error("TLS connection closed without close_notify");
|
|
60
|
+
if (typeof value.chunk !== "string")
|
|
61
|
+
throw new Error("Invalid TLS transport response");
|
|
62
|
+
tls.feed(decode(value.chunk));
|
|
63
|
+
})().finally(() => {
|
|
64
|
+
incoming = undefined;
|
|
65
|
+
}));
|
|
66
|
+
const retry = async (status) => {
|
|
67
|
+
await flush();
|
|
68
|
+
if (status === -2)
|
|
69
|
+
await receive();
|
|
70
|
+
else if (status !== -3)
|
|
71
|
+
throw new Error(`TLS operation failed (${status})`);
|
|
72
|
+
};
|
|
73
|
+
const deliver = () => {
|
|
74
|
+
if (buffered && reading) {
|
|
75
|
+
const pending = reading;
|
|
76
|
+
reading = undefined;
|
|
77
|
+
const data = buffered.subarray(0, pending.count);
|
|
78
|
+
buffered =
|
|
79
|
+
buffered.byteLength > data.byteLength
|
|
80
|
+
? buffered.subarray(data.byteLength)
|
|
81
|
+
: undefined;
|
|
82
|
+
pending.resolve({ chunk: encode(data) });
|
|
83
|
+
}
|
|
84
|
+
if (eof && !buffered) {
|
|
85
|
+
reading?.resolve({ eof: true });
|
|
86
|
+
reading = undefined;
|
|
87
|
+
resolveWatch({});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const pump = async () => {
|
|
91
|
+
if (pumping || stopped)
|
|
92
|
+
return;
|
|
93
|
+
pumping = true;
|
|
94
|
+
try {
|
|
95
|
+
while (!stopped && !eof && !buffered) {
|
|
96
|
+
const result = tls.read();
|
|
97
|
+
if (result instanceof Uint8Array) {
|
|
98
|
+
buffered = result;
|
|
99
|
+
deliver();
|
|
100
|
+
}
|
|
101
|
+
else if (result === -6) {
|
|
102
|
+
eof = true;
|
|
103
|
+
deliver();
|
|
104
|
+
}
|
|
105
|
+
else
|
|
106
|
+
await retry(result);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
stop(error);
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
pumping = false;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
try {
|
|
117
|
+
for (;;) {
|
|
118
|
+
const status = tls.handshake();
|
|
119
|
+
if (status === 1)
|
|
120
|
+
break;
|
|
121
|
+
await retry(status);
|
|
122
|
+
}
|
|
123
|
+
await flush();
|
|
124
|
+
const certificates = tls.certificates();
|
|
125
|
+
if (!certificates.length)
|
|
126
|
+
throw new Error("TLS peer did not present a certificate");
|
|
127
|
+
// No application reads/writes or successful open are exposed before this.
|
|
128
|
+
await call("WorkersCapacitor", "socketVerifyTls", {
|
|
129
|
+
id,
|
|
130
|
+
servername: hostname,
|
|
131
|
+
certificates: certificates.map(encode),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
stop(error);
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
void pump();
|
|
139
|
+
return async (plugin, method, args) => {
|
|
140
|
+
if (method === "socketClose") {
|
|
141
|
+
stop();
|
|
142
|
+
return call(plugin, method, args);
|
|
143
|
+
}
|
|
144
|
+
if (method === "socketWatch")
|
|
145
|
+
return watched;
|
|
146
|
+
if (stopped)
|
|
147
|
+
throw new Error("TLS session closed");
|
|
148
|
+
if (method === "socketRead") {
|
|
149
|
+
if (reading)
|
|
150
|
+
throw new Error("TLS read already pending");
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
reading = { count: Number(args.maxBytes ?? 65536), resolve, reject };
|
|
153
|
+
deliver();
|
|
154
|
+
void pump();
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
if (method === "socketWrite") {
|
|
159
|
+
const data = decode(String(args.chunk));
|
|
160
|
+
for (let offset = 0; offset < data.length;) {
|
|
161
|
+
const status = tls.write(data.subarray(offset));
|
|
162
|
+
if (status > 0)
|
|
163
|
+
offset += status;
|
|
164
|
+
else
|
|
165
|
+
await retry(status);
|
|
166
|
+
}
|
|
167
|
+
await flush();
|
|
168
|
+
return {};
|
|
169
|
+
}
|
|
170
|
+
if (method === "socketEnd") {
|
|
171
|
+
for (;;) {
|
|
172
|
+
const status = tls.shutdown();
|
|
173
|
+
if (status >= 0)
|
|
174
|
+
break;
|
|
175
|
+
await retry(status);
|
|
176
|
+
}
|
|
177
|
+
await flush();
|
|
178
|
+
return {};
|
|
179
|
+
}
|
|
180
|
+
return await call(plugin, method, args);
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
stop(error);
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|