Transferring Session ID between Express.js and Socket.io while dealing with iframes from distinct origins

My Node application built with Express.js and Socket.io is facing an issue where they are not sharing the same session ID when running under iframe with different origins. When accessed directly or through iframes with the same origin, everything works fine with the session ID being shared seamlessly between HTTP and Socket.io. However, under iframes with different origins (parent and child), the shared session ID between Express.js and Socket.io fails to work, and I cannot see the cookie set in the Chrome browser's dev tools.

Both origins are non-secure HTTP websites; for example, from (parent) to (child). Since I publish my node backend with port 6175, I access the website directly without going through any web server.

Below is a snippet of the code:

Code snippet for setting up express-session:

const sessionMiddleware = session({
  resave: true,
  saveUninitialized: true,
  secret: config.session.secretKey,
  httpOnly: true,
  ephemeral: true,
  cookie: { maxAge: config.session.maxAge, secure: false, sameSite: "none", path: "/" },
  store: new (FileStore(session))({
    logFn: logger.info,
    reapInterval: config.session.reapInterval,
  })
});

app.use(sessionMiddleware);

Setting up additional middleware for Express.js:

app.use(cors());
app.options('*', cors());
app.use(helmet({ frameguard: false }));
app.use(compression());
app.use(bodyParser.json());
app.use(cookieParser());

Initializing Socket.io with express-session package:

this.io = new Server(server, {
   cors: {
      origin: '*',
      methods: ["GET", "POST", "PUT", "DELETE"],
   },
});

this.socket.io.use((socket, next) => {
   sessionMiddleware(socket.handshake, {}, next);
});

Answer №1

The problem has been resolved in Chrome browser, but unfortunately persists in Safari. To address this issue, you may need to manipulate the URL by adding a session ID and sending it to the server. When configuring the Node server, I only specified the maxAge in the cookie object of express-session. Furthermore, both origins must be secure HTTPS websites for the solution to function properly. For guidance on fixing the issue in Chrome, refer to this resource:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble resolving '___' from the 'home' state? Dealing with an angular ui-router complication?

I am a newcomer to angular and currently following the instructions outlined in this tutorial: https://scotch.io/tutorials/angularjs-multi-step-form-using-ui-router However, I am facing challenges while injecting this module into an existing module. Des ...

Unexpected behavior observed with Async Await

I'm currently working on a feature for my node server that involves using cheerio to extract information from a website. However, I'm facing some unexpected behavior with my functions. Here is the controller code: class ScraperController { s ...

Leveraging the Request npm package synchronously within Meteor 1.3

In my Meteor 1.3.2.4 project, I tried utilizing the synchronous features of the request npm package. I initially followed the instructions provided in this guide article from Meteor and attempted to use Meteor.bindEnvironment. Here's the code: impor ...

Axios failing to include Content-Type in header

I have set up an Odoo instance in the backend and developed a custom module that includes a web controller. Here is the code for the web controller: Web Controller # -*- coding: utf-8 -*- from odoo import http import odoo from odoo.http import Response, ...

Accessing the facebox feature within a dropdown menu

Looking for assistance in creating a function to open a facebox when an option from a drop down list is selected. Here is what I have so far: <select><option value="www.google.com/" id="xxx"></option></select> In the header sectio ...

jQuery and CSS3 for importing and customizing text files

I successfully customized the file input utilizing jQuery and CSS3. However, there is one aspect I couldn't quite figure out. After the user selects an image or file, I want to display the selected file's text at the very bottom of the text like ...

Personalized AngularJS required text

After utilizing the built-in AngularJS directive required and encountering a validation error, I receive a small popup near the field displaying "Please fill out this field". My issue lies in needing the text to be displayed in another language. How should ...

Upon deployment, Dokku mistakenly categorizes my Node.js application as a Go app

After creating a Procfile with the following contents: web: node web.js I updated my package.json file as follows: { "name": "app-express", "version": "0.0.1", "private": true, "description": "web panel", "main": "web.js", "scrip ...

Launching Node and Mongo with docker-compose fails to initiate both services

Issues and Errors: When running docker-compose run mongo, Mongo container starts successfully However, when running docker-compose run iotmap or docker-compose up, only the node container starts but not the Mongo container 1a) Running docker-compose ps ...

Creating a highly innovative server infrastructure tailored for Dojo applications with exceptional features

Recently, I have been using web2py for my projects and have found it incredibly useful in creating RESTful web applications. However, I am now looking to expand my skills in JavaScript by developing a more modern and dynamic client-side application that re ...

Is it possible to load multiple angular applications within a master angular shell application?

I am searching for a method to integrate multiple Angular applications into a single shell Angular application. The concept involves having different teams develop separate Angular apps that can be loaded within a main shell app visible to end users. Thi ...

The pie chart fails to fully display

Check out the repro example I made here. The legend is displaying properly, but the graph is not showing up. What could be causing this unexpected issue? ...

Angular's UI router is directing users to the incorrect URL

Just starting out with Angular 1 and tasked with adding a new feature to an existing webapp. The webapp utilizes jhipster for backend and frontend generation (Angular 1 and uirouter). I attempted to create my own route and state by borrowing from existing ...

Utilize JavaScript to reference any numerical value

I am attempting to create a button that refreshes the page, but only functions on the root / page and any page starting with /page/* (where * can be any number). Below is the code I have written: $('.refresh a').click(function() { var pathNa ...

Exploring the capabilities of arrays within Ajax

Below is the original code I wrote in JavaScript: var wt_val = []; for (i = 0; i<human_wt.length; i++){ var mult; mult = data_list[basket_list[button_port_name][i]].map(x => x*(wt[i]/100)); wt_val.push(mult); ...

Ways to Manage modals responses as services in AngularJS

I am a beginner in the world of JavaScript and AngularJS. I am fascinated by some of the concepts within JS, like trying to create a modal service using the example I found online. I am eager to delve deeper into what exactly is happening under the hood, e ...

Guide on decrypting a file encrypted with C# using Node JS

I currently have encrypted files in C# using DES and PKCS7 encryption. My objective is to decrypt these files in Node JS. The decryption code in C# that I am using appears like this: public string SSFile_Reader( string fileToDecrypt ) { DESCryptoService ...

My experience with jquery addClass and removeClass functions has not been as smooth as I had hoped

I have a series of tables each separated by div tags. Whenever a user clicks on a letter, I want to display only the relevant div tag contents. This can be achieved using the following jQuery code: $(".expand_button").on("click", function() { $(th ...

Is it possible for you to enter "1.00" instead of just 1 when using the input type as number?

I am using Polymer paper-input and I would like to ensure that my input with type "number" always displays 2 decimal points, even when it is a whole number. Instead of just displaying as "1", I want it to be shown as "1.00" at all times. I have tried sett ...

Is it advisable to clean user inputs in express js?

After utilizing express-validator's escape() function to sanitize user inputs and storing the escaped data in the database using parameterized queries, I encountered an issue when rendering the input from the database with the EJS view engine. The esc ...