Skip to content

Commit b40a423

Browse files
authored
fix: MM session loss (#84)
1 parent 0e67c8f commit b40a423

File tree

4 files changed

+66
-56
lines changed

4 files changed

+66
-56
lines changed

src/client.js

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,31 @@ class ZitiClient {
8585
let name = cookie.substring(0, cookie.indexOf("="));
8686
let value = cookie.substring(cookie.indexOf("=") + 1);
8787
let cookie_value = value.substring(0, value.indexOf(";"));
88-
let parts = value.split(";");
89-
let cookiePath;
90-
let expires;
91-
for (let j = 0; j < parts.length; j++) {
92-
let part = parts[j].trim();
93-
part = part.toLowerCase();
94-
if ( part.startsWith("path") ) {
95-
cookiePath = part.substring(part.indexOf("=") + 1);
96-
}
97-
else if ( part.startsWith("expires") ) {
98-
expires = new Date( part.substring(part.indexOf("=") + 1) );
99-
}
100-
else if ( part.startsWith("httponly") ) {
101-
httpOnly = true;
88+
if (cookie_value !== ''){
89+
let parts = value.split(";");
90+
let cookiePath;
91+
let expires;
92+
for (let j = 0; j < parts.length; j++) {
93+
let part = parts[j].trim();
94+
part = part.toLowerCase();
95+
if ( part.startsWith("path") ) {
96+
cookiePath = part.substring(part.indexOf("=") + 1);
97+
}
98+
else if ( part.startsWith("expires") ) {
99+
expires = new Date( part.substring(part.indexOf("=") + 1) );
100+
}
101+
else if ( part.startsWith("httponly") ) {
102+
httpOnly = true;
103+
}
102104
}
105+
106+
zitiCookies[name] = cookie_value;
107+
108+
// console.log('=====> CookieInterceptor ZITI_COOKIES (after): ', zitiCookies);
109+
110+
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
103111
}
104112

105-
zitiCookies[name] = cookie_value;
106-
107-
// console.log('=====> CookieInterceptor ZITI_COOKIES (after): ', zitiCookies);
108-
109-
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
110-
111113
release();
112114

113115
})()
@@ -462,24 +464,27 @@ class ZitiClient {
462464
let name = cookie.substring(0, cookie.indexOf("="));
463465
let value = cookie.substring(cookie.indexOf("=") + 1);
464466
let cookie_value = value.substring(0, value.indexOf(";"));
465-
let parts = value.split(";");
466-
for (let j = 0; j < parts.length; j++) {
467-
let part = parts[j].trim();
468-
if ( part.startsWith("Path") ) {
469-
cookiePath = part.substring(part.indexOf("=") + 1);
470-
}
471-
else if ( part.startsWith("Expires") ) {
472-
expires = new Date( part.substring(part.indexOf("=") + 1) );
473-
}
474-
else if ( part.startsWith("HttpOnly") ) {
475-
httpOnly = true;
467+
if (cookie_value !== ''){
468+
let parts = value.split(";");
469+
for (let j = 0; j < parts.length; j++) {
470+
let part = parts[j].trim();
471+
if ( part.startsWith("Path") ) {
472+
cookiePath = part.substring(part.indexOf("=") + 1);
473+
}
474+
else if ( part.startsWith("Expires") ) {
475+
expires = new Date( part.substring(part.indexOf("=") + 1) );
476+
}
477+
else if ( part.startsWith("HttpOnly") ) {
478+
httpOnly = true;
479+
}
476480
}
481+
482+
zitiCookies[name] = cookie_value;
483+
484+
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
485+
486+
Cookies.set(name, cookie_value, { expires: expires, path: cookiePath});
477487
}
478-
479-
480-
zitiCookies[name] = cookie_value;
481-
482-
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
483488
}
484489
}
485490
}
@@ -633,8 +638,7 @@ zitiFetch = async ( url, opts ) => {
633638
} else { // the request is targeting the raw internet
634639

635640
ziti._ctx.logger.warn('zitiFetch(): no associated serviceConfig, bypassing intercept of [%s]', url);
636-
var resp = window.realFetch(url, opts);
637-
return resp;
641+
return window.realFetch(url, opts);
638642
}
639643

640644
/**
@@ -734,24 +738,28 @@ zitiFetch = async ( url, opts ) => {
734738
let name = cookie.substring(0, cookie.indexOf("="));
735739
let value = cookie.substring(cookie.indexOf("=") + 1);
736740
let cookie_value = value.substring(0, value.indexOf(";"));
737-
let parts = value.split(";");
738-
for (let j = 0; j < parts.length; j++) {
739-
let part = parts[j].trim();
740-
if ( part.startsWith("Path") ) {
741-
cookiePath = part.substring(part.indexOf("=") + 1);
742-
}
743-
else if ( part.startsWith("Expires") ) {
744-
expires = new Date( part.substring(part.indexOf("=") + 1) );
745-
}
746-
else if ( part.startsWith("HttpOnly") ) {
747-
httpOnly = true;
741+
if (cookie_value !== ''){
742+
let parts = value.split(";");
743+
for (let j = 0; j < parts.length; j++) {
744+
let part = parts[j].trim();
745+
if ( part.startsWith("Path") ) {
746+
cookiePath = part.substring(part.indexOf("=") + 1);
747+
}
748+
else if ( part.startsWith("Expires") ) {
749+
expires = new Date( part.substring(part.indexOf("=") + 1) );
750+
}
751+
else if ( part.startsWith("HttpOnly") ) {
752+
httpOnly = true;
753+
}
748754
}
749-
}
750755

751756

752-
zitiCookies[name] = cookie_value;
757+
zitiCookies[name] = cookie_value;
758+
759+
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
753760

754-
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
761+
Cookies.set(name, cookie_value, { expires: expires, path: cookiePath});
762+
}
755763
}
756764
}
757765
}

src/http/request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ HttpRequest.prototype.getRequestOptions = async function() {
219219
throw new Error('Only HTTP(S) protocols are supported');
220220
}
221221

222-
if (parsedURL.port !== '') {
222+
if ((parsedURL.port !== '') && (parsedURL.port !== '80')) {
223223
headers.set('Host', parsedURL.hostname + ":" + parsedURL.port);
224224
} else {
225225
headers.set('Host', parsedURL.hostname);
@@ -264,7 +264,7 @@ HttpRequest.prototype.getRequestOptions = async function() {
264264
let cookieHeaderValue = '';
265265
for (const cookie in cookieObject) {
266266
if (cookieObject.hasOwnProperty(cookie)) {
267-
cookieHeaderValue += cookie + '=' + cookieObject[cookie] + ';';
267+
cookieHeaderValue += cookie + '=' + cookieObject[cookie] + '; ';
268268
}
269269
}
270270
headers.set('Cookie', cookieHeaderValue);

src/http/ziti-websocket-wrapper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ async function initAsClient(websocket, address, protocols, options) {
566566
var newUrl = new URL( address );
567567
newUrl.hostname = zitiConfig.httpAgent.target.host;
568568
newUrl.port = zitiConfig.httpAgent.target.port;
569-
ziti._ctx.logger.trace( 'ZitiWebSocketWrapper: transformed URL: ', newUrl.toString());
569+
ziti._ctx.logger.debug( 'ZitiWebSocketWrapper: transformed URL: ', newUrl.toString());
570570

571571
serviceName = await ziti._ctx.shouldRouteOverZiti( newUrl );
572572

@@ -618,7 +618,7 @@ async function initAsClient(websocket, address, protocols, options) {
618618

619619
for (const cookie in zitiCookies) {
620620
if (zitiCookies.hasOwnProperty(cookie)) {
621-
cookieString += cookie + '=' + zitiCookies[cookie] + ';';
621+
cookieString += cookie + '=' + zitiCookies[cookie] + '; ';
622622
}
623623
}
624624

src/http/ziti-xhr.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ function ZitiXMLHttpRequest () {
282282

283283
settings.body = data;
284284

285+
settings.headers = headers;
286+
285287
response = await fetch(settings.url, settings);
286288

287289
self.status = response.status;

0 commit comments

Comments
 (0)