mockttp
Version:
Mock HTTP server for testing HTTP clients and stubbing webservices
474 lines • 22.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WsStepLookup = exports.DelayStepImpl = exports.TimeoutStepImpl = exports.ResetConnectionStepImpl = exports.CloseConnectionStepImpl = exports.RejectWebSocketStepImpl = exports.ListenWebSocketStepImpl = exports.EchoWebSocketStepImpl = exports.PassThroughWebSocketStepImpl = void 0;
const buffer_1 = require("buffer");
const url = require("url");
const http = require("http");
const https = require("https");
const _ = require("lodash");
const WebSocket = require("ws");
// These were internal ws modules before 8.20.0, now officially exported.
// @types/ws doesn't include types for these yet, so we type them manually:
const { PerMessageDeflate, extension: wsExtension } = WebSocket;
const serialization_1 = require("../../serialization/serialization");
const request_step_impls_1 = require("../requests/request-step-impls");
Object.defineProperty(exports, "CloseConnectionStepImpl", { enumerable: true, get: function () { return request_step_impls_1.CloseConnectionStepImpl; } });
Object.defineProperty(exports, "DelayStepImpl", { enumerable: true, get: function () { return request_step_impls_1.DelayStepImpl; } });
Object.defineProperty(exports, "ResetConnectionStepImpl", { enumerable: true, get: function () { return request_step_impls_1.ResetConnectionStepImpl; } });
Object.defineProperty(exports, "TimeoutStepImpl", { enumerable: true, get: function () { return request_step_impls_1.TimeoutStepImpl; } });
const url_1 = require("../../util/url");
const socket_util_1 = require("../../util/socket-util");
const request_utils_1 = require("../../util/request-utils");
const header_utils_1 = require("../../util/header-utils");
const http_agents_1 = require("../http-agents");
const rule_parameters_1 = require("../rule-parameters");
const passthrough_handling_1 = require("../passthrough-handling");
const websocket_step_definitions_1 = require("./websocket-step-definitions");
const match_replace_1 = require("../match-replace");
function isOpen(socket) {
return socket.readyState === WebSocket.OPEN;
}
// Based on ws's validation.js
function isValidStatusCode(code) {
return ( // Standard code:
code >= 1000 &&
code <= 1014 &&
code !== 1004 &&
code !== 1005 &&
code !== 1006) || ( // Application-specific code:
code >= 3000 && code <= 4999);
}
const INVALID_STATUS_REGEX = /Invalid WebSocket frame: invalid status code (\d+)/;
function pipeWebSocket(inSocket, outSocket) {
const onPipeFailed = (op) => (err) => {
if (!err)
return;
inSocket.close();
console.error(`Websocket ${op} failed`, err);
};
inSocket.on('message', (msg, isBinary) => {
if (isOpen(outSocket)) {
outSocket.send(msg, { binary: isBinary }, onPipeFailed('message'));
}
});
inSocket.on('close', (num, reason) => {
if (isValidStatusCode(num)) {
try {
outSocket.close(num, reason);
}
catch (e) {
console.warn(e);
outSocket.close();
}
}
else {
outSocket.close();
}
});
inSocket.on('ping', (data) => {
if (isOpen(outSocket))
outSocket.ping(data, undefined, onPipeFailed('ping'));
});
inSocket.on('pong', (data) => {
if (isOpen(outSocket))
outSocket.pong(data, undefined, onPipeFailed('pong'));
});
// If either socket has an general error (connection failure, but also could be invalid WS
// frames) then we kill the raw connection upstream to simulate a generic connection error:
inSocket.on('error', (err) => {
console.log(`Error in proxied WebSocket:`, err);
const rawOutSocket = outSocket;
if (err.message.match(INVALID_STATUS_REGEX)) {
const status = parseInt(INVALID_STATUS_REGEX.exec(err.message)[1]);
// Simulate errors elsewhere by messing with ws internals. This may break things,
// that's effectively on purpose: we're simulating the client going wrong:
const buf = buffer_1.Buffer.allocUnsafe(2);
buf.writeUInt16BE(status); // status comes from readUInt16BE, so always fits
const sender = rawOutSocket._sender;
sender.sendFrame(sender.constructor.frame(buf, {
fin: true,
rsv1: false,
opcode: 0x08,
mask: true,
readOnly: false
}), () => {
rawOutSocket._socket.destroy();
});
}
else {
// Unknown error, just kill the connection with no explanation
rawOutSocket._socket.destroy();
}
});
}
function mirrorRejection(downstreamSocket, upstreamRejectionResponse, simulateConnectionErrors) {
return new Promise((resolve) => {
if (downstreamSocket.writable) {
const { statusCode, statusMessage, rawHeaders } = upstreamRejectionResponse;
downstreamSocket.write(rawResponse(statusCode || 500, statusMessage || 'Unknown error', (0, header_utils_1.pairFlatRawHeaders)(rawHeaders)));
upstreamRejectionResponse.pipe(downstreamSocket);
upstreamRejectionResponse.on('end', resolve);
upstreamRejectionResponse.on('error', (error) => {
console.warn('Error receiving WebSocket upstream rejection response:', error);
if (simulateConnectionErrors) {
(0, socket_util_1.resetOrDestroy)(downstreamSocket);
}
else {
downstreamSocket.destroy();
}
resolve();
});
// The socket is being optimistically written to and then killed - we don't care
// about any more errors occuring here.
downstreamSocket.on('error', () => {
resolve();
});
}
}).catch(() => { });
}
const rawResponse = (statusCode, statusMessage, headers = []) => `HTTP/1.1 ${statusCode} ${statusMessage}\r\n` +
_.map(headers, ([key, value]) => `${key}: ${value}`).join('\r\n') +
'\r\n\r\n';
/**
* Create a client-mode WebSocket on an existing stream, bypassing the normal
* HTTP handshake. This is used when we've already performed the upgrade
* handshake ourselves via http.request. We do this with custom APIs so that
* we can fully control the handshake and mirror exact configurations.
*/
function createWebSocketFromStream(socket, head, options = {}) {
const maxPayload = options.maxPayload ?? 0;
const ws = new WebSocket(null, undefined, { maxPayload });
ws._isServer = false; // Client mode: mask frames per RFC 6455
if (options.extensions) {
ws._extensions = options.extensions;
}
ws.setSocket(socket, head, {
allowSynchronousEvents: true,
maxPayload,
skipUTF8Validation: true // Preserve even invalid weird stuff
});
return ws;
}
class PassThroughWebSocketStepImpl extends websocket_step_definitions_1.PassThroughWebSocketStep {
wsServer;
initializeWsServer() {
if (this.wsServer)
return;
this.wsServer = new WebSocket.Server({
noServer: true,
perMessageDeflate: true,
skipUTF8Validation: true // Preserve even invalid weird stuff
});
this.wsServer.on('connection', (ws) => {
pipeWebSocket(ws, ws.upstreamWebSocket);
pipeWebSocket(ws.upstreamWebSocket, ws);
});
}
_trustedCACertificates;
async trustedCACertificates() {
if (!this.extraCACertificates.length)
return undefined;
if (!this._trustedCACertificates) {
this._trustedCACertificates = (0, passthrough_handling_1.getTrustedCAs)(undefined, this.extraCACertificates);
}
return this._trustedCACertificates;
}
async handle(req, socket, head, options) {
this.initializeWsServer();
let reqUrl = req.url;
let { protocol, pathname, search: query } = url.parse(reqUrl);
let rawHeaders = req.rawHeaders;
// Actual IP address or hostname
let hostAddress = req.destination.hostname;
// Same as hostAddress, unless it's an IP, in which case it's our best guess of the
// functional 'name' for the host (from Host header or SNI).
let hostname = (0, passthrough_handling_1.getEffectiveHostname)(hostAddress, socket, rawHeaders);
let port = req.destination.port.toString();
const reqMessage = req;
const isH2Downstream = (0, request_utils_1.isHttp2)(req);
hostAddress = await (0, passthrough_handling_1.getClientRelativeHostname)(hostAddress, req.remoteIpAddress, (0, passthrough_handling_1.getDnsLookupFunction)(this.lookupOptions));
if (this.transformRequest) {
const originalHostname = hostname;
({ protocol, hostname, port, reqUrl, rawHeaders } = (0, passthrough_handling_1.applyDestinationTransforms)(this.transformRequest, {
isH2Downstream,
rawHeaders,
port,
protocol,
hostname,
pathname,
query
}));
// If you modify the hostname, we also treat that as modifying the
// resulting destination in turn:
if (hostname !== originalHostname) {
hostAddress = hostname;
}
}
const destination = {
hostname: hostAddress,
port: port
? parseInt(port, 10)
: (0, url_1.getDefaultPort)(protocol ?? 'http')
};
await this.connectUpstream(destination, reqUrl, reqMessage, rawHeaders, socket, head, options);
}
async connectUpstream(destination, wsUrl, req, rawHeaders, incomingSocket, head, options) {
const parsedUrl = url.parse(wsUrl);
const effectiveHostname = parsedUrl.hostname; // N.b. not necessarily the same as destination
const effectivePort = (0, url_1.getEffectivePort)(parsedUrl);
const trustedCAs = await this.trustedCACertificates();
const proxySettingSource = (0, rule_parameters_1.assertParamDereferenced)(this.proxyConfig);
const agent = await (0, http_agents_1.getAgent)({
protocol: parsedUrl.protocol,
hostname: effectiveHostname,
port: effectivePort,
proxySettingSource,
tryHttp2: false, // We don't support websockets over H2 yet
keepAlive: false // Not a thing for websockets: they take over the whole connection
});
// Strip any extension offers we can't handle (i.e. anything other than
// permessage-deflate) to prevent the upstream from accepting them and causing trouble:
const extensionHeaderValues = (0, header_utils_1.findRawHeaders)(rawHeaders, 'sec-websocket-extensions');
if (extensionHeaderValues.length > 0) {
try {
const parsed = wsExtension.parse(extensionHeaderValues.map(([_k, v]) => v).join(', '));
// This is very unlikely - approximately zero other extensions exist in any form.
const hasUnsupported = Object.keys(parsed)
.some(name => name !== PerMessageDeflate.extensionName);
if (hasUnsupported) {
rawHeaders = rawHeaders.filter(([key]) => key.toLowerCase() !== 'sec-websocket-extensions');
if (parsed[PerMessageDeflate.extensionName]) {
rawHeaders.push(['Sec-WebSocket-Extensions', wsExtension.format({
[PerMessageDeflate.extensionName]: parsed[PerMessageDeflate.extensionName]
})]);
}
}
}
catch {
// If we can't parse the client's offer, forward it as-is and let
// the upstream handle/reject it:
}
}
// Build the upstream request manually, mirroring the input as closely as possible:
const isSecure = parsedUrl.protocol === 'wss:';
const httpModule = isSecure ? https : http;
const upstreamReqOptions = {
hostname: destination.hostname,
port: destination.port,
path: parsedUrl.path,
headers: (0, header_utils_1.flattenPairedRawHeaders)(rawHeaders),
setDefaultHeaders: false, // No auto-headers - we exactly mirror the client
method: req.method,
agent,
lookup: (0, passthrough_handling_1.getDnsLookupFunction)(this.lookupOptions),
...(isSecure ? (0, passthrough_handling_1.getUpstreamTlsOptions)({
hostname: effectiveHostname,
port: effectivePort,
ignoreHostHttpsErrors: this.ignoreHostHttpsErrors,
clientCertificateHostMap: this.clientCertificateHostMap,
trustedCAs,
}) : {})
};
const upstreamReq = httpModule.request(upstreamReqOptions);
// Track the upstream WebSocket so the incomingSocket error handler can close it:
let upstreamWebSocket;
if (options.emitEventCallback) {
// This effectively matches the URL preprocessing logic in MockttpServer.preprocessRequest,
// so that the resulting event matches the req.url property elsewhere.
const urlHost = (0, passthrough_handling_1.getEffectiveHostname)(effectiveHostname, req.socket, rawHeaders);
const wsProtocol = parsedUrl.protocol.replace(/^http/, 'ws').replace(/:$/, '');
const subprotocols = (0, header_utils_1.findRawHeaders)(rawHeaders, 'sec-websocket-protocol')
.flatMap(([_k, v]) => v.split(',').map(s => s.trim()).filter(s => !!s));
options.emitEventCallback('passthrough-websocket-connect', {
method: req.method,
protocol: wsProtocol,
hostname: urlHost,
port: effectivePort.toString(),
path: parsedUrl.path || '/',
rawHeaders,
subprotocols
});
}
if (options.keyLogStream) {
upstreamReq.on('socket', (socket) => {
socket.on('keylog', (line) => options.keyLogStream.write(line));
});
}
upstreamReq.on('upgrade', (upstreamRes, upstreamSocket, upgradeHead) => {
// Handle permessage-deflate extension negotiation. If the upstream server
// committed to extensions we can't set up, we must kill the connection rather
// than silently mishandling compressed frames:
const responseExtensionHeader = upstreamRes.headers['sec-websocket-extensions'];
let extensions;
try {
if (responseExtensionHeader) {
const parsed = wsExtension.parse(responseExtensionHeader);
if (parsed[PerMessageDeflate.extensionName]) {
const pmd = new PerMessageDeflate({}, false); // false = client mode
pmd.accept(parsed[PerMessageDeflate.extensionName]);
extensions = { [PerMessageDeflate.extensionName]: pmd };
}
}
}
catch (e) {
console.warn('Failed to negotiate WebSocket extensions:', e);
upstreamSocket.destroy();
incomingSocket.end();
return;
}
upstreamWebSocket = createWebSocketFromStream(upstreamSocket, upgradeHead, {
maxPayload: 0,
extensions
});
// Set req.headers to match exactly what the upstream confirmed, so ws's
// handleUpgrade negotiates the same values downstream without any issues
// from malformed original headers:
if (!extensions) {
delete req.headers['sec-websocket-extensions'];
}
// For WS's sake, we simplify the subprotocol header to only the upstream-selected value so
// that it can just accept as is, and ignore any other badly behaved client's headers.
const serverProtocol = upstreamRes.headers['sec-websocket-protocol'];
if (serverProtocol?.trim()) {
req.headers['sec-websocket-protocol'] = serverProtocol;
}
else {
delete req.headers['sec-websocket-protocol'];
}
this.wsServer.handleUpgrade(req, incomingSocket, head, (ws) => {
ws.upstreamWebSocket = upstreamWebSocket;
incomingSocket.emit('ws-upgrade', ws);
this.wsServer.emit('connection', ws);
});
});
upstreamReq.on('response', (upstreamRes) => {
console.log(`Unexpected websocket response from ${wsUrl}: ${upstreamRes.statusCode}`);
mirrorRejection(incomingSocket, upstreamRes, this.simulateConnectionErrors);
});
upstreamReq.on('error', (e) => {
console.warn(e);
if (this.simulateConnectionErrors) {
(0, socket_util_1.resetOrDestroy)(incomingSocket);
}
else {
incomingSocket.end();
}
});
incomingSocket.on('error', () => {
if (upstreamWebSocket) {
upstreamWebSocket.close(1011);
}
else {
upstreamReq.destroy();
}
});
upstreamReq.end();
}
/**
* @internal
*/
static deserialize(data, channel, { ruleParams }) {
// Backward compat for old clients:
if (data.forwarding && !data.transformRequest?.replaceHost) {
const [targetHost, setProtocol] = data.forwarding.targetHost.split('://').reverse();
data.transformRequest ??= {};
data.transformRequest.replaceHost = {
targetHost,
updateHostHeader: data.forwarding.updateHostHeader ?? true
};
data.transformRequest.setProtocol = setProtocol;
}
return _.create(this.prototype, {
...data,
proxyConfig: (0, serialization_1.deserializeProxyConfig)(data.proxyConfig, channel, ruleParams),
simulateConnectionErrors: data.simulateConnectionErrors ?? false,
extraCACertificates: data.extraCACertificates || [],
ignoreHostHttpsErrors: data.ignoreHostCertificateErrors,
clientCertificateHostMap: _.mapValues(data.clientCertificateHostMap, ({ pfx, passphrase }) => ({ pfx: (0, serialization_1.deserializeBuffer)(pfx), passphrase })),
transformRequest: data.transformRequest ? {
...data.transformRequest,
...(data.transformRequest?.matchReplaceHost !== undefined ? {
matchReplaceHost: {
...data.transformRequest.matchReplaceHost,
replacements: (0, match_replace_1.deserializeMatchReplaceConfiguration)(data.transformRequest.matchReplaceHost.replacements)
}
} : {}),
...(data.transformRequest?.matchReplacePath !== undefined ? {
matchReplacePath: (0, match_replace_1.deserializeMatchReplaceConfiguration)(data.transformRequest.matchReplacePath)
} : {}),
...(data.transformRequest?.matchReplaceQuery !== undefined ? {
matchReplaceQuery: (0, match_replace_1.deserializeMatchReplaceConfiguration)(data.transformRequest.matchReplaceQuery)
} : {}),
} : undefined
});
}
}
exports.PassThroughWebSocketStepImpl = PassThroughWebSocketStepImpl;
class EchoWebSocketStepImpl extends websocket_step_definitions_1.EchoWebSocketStep {
wsServer;
initializeWsServer() {
if (this.wsServer)
return;
this.wsServer = new WebSocket.Server({
noServer: true,
perMessageDeflate: true,
skipUTF8Validation: true // Preserve even invalid weird stuff
});
this.wsServer.on('connection', (ws) => {
pipeWebSocket(ws, ws);
});
}
async handle(req, socket, head) {
this.initializeWsServer();
this.wsServer.handleUpgrade(req, socket, head, (ws) => {
socket.emit('ws-upgrade', ws);
this.wsServer.emit('connection', ws);
});
}
}
exports.EchoWebSocketStepImpl = EchoWebSocketStepImpl;
class ListenWebSocketStepImpl extends websocket_step_definitions_1.ListenWebSocketStep {
wsServer;
initializeWsServer() {
if (this.wsServer)
return;
this.wsServer = new WebSocket.Server({
noServer: true,
perMessageDeflate: true,
skipUTF8Validation: true // Accept even invalid weird stuff
});
this.wsServer.on('connection', (ws) => {
// Accept but ignore the incoming websocket data
ws.resume();
});
}
async handle(req, socket, head) {
this.initializeWsServer();
this.wsServer.handleUpgrade(req, socket, head, (ws) => {
socket.emit('ws-upgrade', ws);
this.wsServer.emit('connection', ws);
});
}
}
exports.ListenWebSocketStepImpl = ListenWebSocketStepImpl;
class RejectWebSocketStepImpl extends websocket_step_definitions_1.RejectWebSocketStep {
async handle(req, socket) {
socket.write(rawResponse(this.statusCode, this.statusMessage, (0, header_utils_1.objectHeadersToRaw)(this.headers)));
if (this.body)
socket.end(this.body);
socket.destroy();
}
}
exports.RejectWebSocketStepImpl = RejectWebSocketStepImpl;
exports.WsStepLookup = {
'ws-passthrough': PassThroughWebSocketStepImpl,
'ws-echo': EchoWebSocketStepImpl,
'ws-listen': ListenWebSocketStepImpl,
'ws-reject': RejectWebSocketStepImpl,
'close-connection': request_step_impls_1.CloseConnectionStepImpl,
'reset-connection': request_step_impls_1.ResetConnectionStepImpl,
'timeout': request_step_impls_1.TimeoutStepImpl,
'delay': request_step_impls_1.DelayStepImpl,
'wait-for-request-body': request_step_impls_1.WaitForRequestBodyStepImpl
};
//# sourceMappingURL=websocket-step-impls.js.map