Skip to content

Commit

Permalink
Fix/preflight (#71)
Browse files Browse the repository at this point in the history
* fix: set cypress as dev dependency

* chore: remove checks for releeae it

* chore: restore release-it

* fix: deal with preflight
  • Loading branch information
agoldis authored Feb 17, 2023
1 parent 3fd36bb commit 2fc3df5
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 124 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@
"ignoreVersion": true,
"infile": "CHANGELOG.md"
}
},
"git": {
"requireBranch": "main",
"commitMessage": "chore: release v${version}"
},
"hooks": {
"before:init": "run-s clean build"
}
},
"prettier": {
Expand Down
183 changes: 105 additions & 78 deletions src/http-proxy/lib/http-proxy/common.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var common = exports,
url = require('url'),
extend = require('util')._extend,
required = require('requires-port');
var common = exports,
url = require('url'),
extend = require('util')._extend,
required = require('requires-port');

var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
isSSL = /^https|wss/;
isSSL = /^https|wss/;

/**
* Simple Regex for testing if protocol is https
Expand All @@ -24,81 +24,101 @@ common.isSSL = isSSL;
* @param {Object} Options Config object passed to the proxy
* @param {ClientRequest} Req Request Object
* @param {String} Forward String to select forward or target
* 
*
* @return {Object} Outgoing Object with all required properties set
*
* @api private
*/

common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.port = options[forward || 'target'].port ||
(isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
common.setupOutgoing = function (outgoing, options, req, forward) {
const _options = { ...options };

['host', 'hostname', 'socketPath', 'pfx', 'key',
'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
function(e) { outgoing[e] = options[forward || 'target'][e]; }
);
// requests to api-proxy wouldn't be intercepted whatsoever
// route preflight requests to api.cypress.io and hope for their mercy
if (req.url === '/preflight') {
_options.target = new URL('https://api.cypress.io');
}
outgoing.port =
_options[forward || 'target'].port ||
(isSSL.test(_options[forward || 'target'].protocol) ? 443 : 80);

[
'host',
'hostname',
'socketPath',
'pfx',
'key',
'passphrase',
'cert',
'ca',
'ciphers',
'secureProtocol',
].forEach(function (e) {
outgoing[e] = _options[forward || 'target'][e];
});

outgoing.method = options.method || req.method;
outgoing.method = _options.method || req.method;
outgoing.headers = extend({}, req.headers);

if (options.headers){
extend(outgoing.headers, options.headers);
if (_options.headers) {
extend(outgoing.headers, _options.headers);
}

if (options.auth) {
outgoing.auth = options.auth;
}

if (options.ca) {
outgoing.ca = options.ca;
if (_options.auth) {
outgoing.auth = _options.auth;
}

if (isSSL.test(options[forward || 'target'].protocol)) {
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
if (_options.ca) {
outgoing.ca = _options.ca;
}

if (isSSL.test(_options[forward || 'target'].protocol)) {
outgoing.rejectUnauthorized =
typeof _options.secure === 'undefined' ? true : _options.secure;
}

outgoing.agent = options.agent || false;
outgoing.localAddress = options.localAddress;
outgoing.agent = _options.agent || false;
outgoing.localAddress = _options.localAddress;

//
// Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
// as node core doesn't handle this COMPLETELY properly yet.
//
if (!outgoing.agent) {
outgoing.headers = outgoing.headers || {};
if (typeof outgoing.headers.connection !== 'string'
|| !upgradeHeader.test(outgoing.headers.connection)
) { outgoing.headers.connection = 'close'; }
if (
typeof outgoing.headers.connection !== 'string' ||
!upgradeHeader.test(outgoing.headers.connection)
) {
outgoing.headers.connection = 'close';
}
}


// the final path is target path + relative path requested by user:
var target = options[forward || 'target'];
var targetPath = target && options.prependPath !== false
? (target.path || '')
: '';
var target = _options[forward || 'target'];
var targetPath =
target && _options.prependPath !== false ? target.path || '' : '';

//
// Remark: Can we somehow not use url.parse as a perf optimization?
//
var outgoingPath = !options.toProxy
? (url.parse(req.url).path || '')
var outgoingPath = !_options.toProxy
? url.parse(req.url).path || ''
: req.url;

//
// Remark: ignorePath will just straight up ignore whatever the request's
// path is. This can be labeled as FOOT-GUN material if you do not know what
// you are doing and are using conflicting options.
//
outgoingPath = !options.ignorePath ? outgoingPath : '';
outgoingPath = !_options.ignorePath ? outgoingPath : '';

outgoing.path = common.urlJoin(targetPath, outgoingPath);

if (options.changeOrigin) {
if (_options.changeOrigin) {
outgoing.headers.host =
required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
required(outgoing.port, _options[forward || 'target'].protocol) &&
!hasPort(outgoing.host)
? outgoing.host + ':' + outgoing.port
: outgoing.host;
}
Expand All @@ -116,13 +136,13 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
* // => Socket
*
* @param {Socket} Socket instance to setup
* 
*
* @return {Socket} Return the configured socket.
*
* @api private
*/

common.setupSocket = function(socket) {
common.setupSocket = function (socket) {
socket.setTimeout(0);
socket.setNoDelay(true);

Expand All @@ -140,12 +160,10 @@ common.setupSocket = function(socket) {
*
* @api private
*/
common.getPort = function(req) {
common.getPort = function (req) {
var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';

return res ?
res[1] :
common.hasEncryptedConnection(req) ? '443' : '80';
return res ? res[1] : common.hasEncryptedConnection(req) ? '443' : '80';
};

/**
Expand All @@ -157,7 +175,7 @@ common.getPort = function(req) {
*
* @api private
*/
common.hasEncryptedConnection = function(req) {
common.hasEncryptedConnection = function (req) {
return Boolean(req.connection.encrypted || req.connection.pair);
};

Expand All @@ -169,15 +187,15 @@ common.hasEncryptedConnection = function(req) {
* @api private
*/

common.urlJoin = function() {
//
// We do not want to mess with the query string. All we want to touch is the path.
//
common.urlJoin = function () {
//
// We do not want to mess with the query string. All we want to touch is the path.
//
var args = Array.prototype.slice.call(arguments),
lastIndex = args.length - 1,
last = args[lastIndex],
lastSegs = last.split('?'),
retSegs;
lastIndex = args.length - 1,
last = args[lastIndex],
lastSegs = last.split('?'),
retSegs;

args[lastIndex] = lastSegs.shift();

Expand All @@ -186,10 +204,12 @@ common.urlJoin = function() {
// joining e.g. ['', 'am']
//
retSegs = [
args.filter(Boolean).join('/')
.replace(/\/+/g, '/')
.replace('http:/', 'http://')
.replace('https:/', 'https://')
args
.filter(Boolean)
.join('/')
.replace(/\/+/g, '/')
.replace('http:/', 'http://')
.replace('https:/', 'https://'),
];

// Only join the query string if it exists so we don't have trailing a '?'
Expand All @@ -198,7 +218,7 @@ common.urlJoin = function() {
// Handle case where there could be multiple ? in the URL.
retSegs.push.apply(retSegs, lastSegs);

return retSegs.join('?')
return retSegs.join('?');
};

/**
Expand All @@ -210,30 +230,37 @@ common.urlJoin = function() {
*
* @api private
*/
common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
common.rewriteCookieProperty = function rewriteCookieProperty(
header,
config,
property
) {
if (Array.isArray(header)) {
return header.map(function (headerElement) {
return rewriteCookieProperty(headerElement, config, property);
});
}
return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
var newValue;
if (previousValue in config) {
newValue = config[previousValue];
} else if ('*' in config) {
newValue = config['*'];
} else {
//no match, return previous value
return match;
}
if (newValue) {
//replace value
return prefix + newValue;
} else {
//remove value
return '';
return header.replace(
new RegExp('(;\\s*' + property + '=)([^;]+)', 'i'),
function (match, prefix, previousValue) {
var newValue;
if (previousValue in config) {
newValue = config[previousValue];
} else if ('*' in config) {
newValue = config['*'];
} else {
//no match, return previous value
return match;
}
if (newValue) {
//replace value
return prefix + newValue;
} else {
//remove value
return '';
}
}
});
);
};

/**
Expand All @@ -245,4 +272,4 @@ common.rewriteCookieProperty = function rewriteCookieProperty(header, config, pr
*/
function hasPort(host) {
return !!~host.indexOf(':');
};
}
Loading

0 comments on commit 2fc3df5

Please sign in to comment.