Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Sharing Session ID between Express.js and Socket.io under iframe different origin

I have a problem with my Node application using Express.js, Socket.io and they both share the same session ID. If I open my website without iframe or iframe with same origins, it works perfectly fine (same session ID sharing between HTTP and Socket.io). However, the shared session ID between Express.js and Socket.io doesn't work when it runs under iframe with different origins (parent and child). I don't see the cookie set into Chrome browser under dev tools.

Both origins are non-secure HTTP website (e.g. http://ip.ad.dr.ess/ (parent) to http://ip.ad.dr.ess:6175/ (child)). I access my website directly without going through any web server because I publish my node backend with this port 6175.

Here's the sample code:

Set up express-session:

const sessionMiddleware = session({
  resave: true,
  saveUninitialized: true,
  secret: config.session.secretKey,
  httpOnly: true,  // dont let browser javascript access cookie ever
  ephemeral: true, // delete this cookie while browser close
  cookie: { maxAge: config.session.maxAge, secure: false, sameSite: "none", path: "/" },
  store: new (FileStore(session))({
    logFn: logger.info,
    reapInterval: config.session.reapInterval,
  })
});

app.use(sessionMiddleware);

Set 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());

Set up 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);
});
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The issue is fixed in Chrome browser but not in Safari so you might have to work with URL by appending it with session ID and get it on the server. On the Node server, I set only maxAge in cookie object of express-session and both origins must be secure HTTPs website to make it work. Follow this link to fix in Chrome browser: https://serverfault.com/questions/1010706/setup-samesite-none-value-in-nginx-webserver

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda