spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.5k stars 3.31k forks source link

Websocket handshake response missing added headers #2436

Open andrewkcarter opened 2 years ago

andrewkcarter commented 2 years ago

Describe the bug spring-boot-dependencies:2.5.2 spring-cloud-dependencies:2020.0.4 (spring-cloud-gateway-server:3.0.4)

We are using SCG to front a set of web applications implemented in Streamlit. Streamlit applications establish a websocket connection which is used to dynamically return data to the page. When the websocket connection is estabished, the streamlit server returns a Set-Cookie header that contains an XSRF token. This token is then used for operations such as file upload.

Screen Shot 2021-11-09 at 10 16 59 AM

After placing this app behind SCG, we noticed that we are no longer receiving the "additional" response headers. They are being lost as the response transits through the gateway.

I have put together a basic setup that replicates the issue using a simple Node.js websocket server, a client HTML page, and the SCG setup.

Steps to Reproduce

  1. Run the node server.mjs file:
    
    // install the one dependency:
    // npm install ws

import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 5678 });

wss.on('headers', (headers) => { headers.push('Set-Cookie: _xsrf=1234599'); headers.push('X-Other-Header: hello'); }); wss.on('connection', function connection(ws) { console.log('Connection') ws.send('hi from the server'); });


2. Open the `client.html` in your browser. It should establish a connection to the websocket server and display `hi from the server` on the page. 

<!DOCTYPE html>

WebSocket demo

3. Open up the network tab, and you should also observe that the browser received two response headers (`Set-Cookie` and `X-Other-Header`).

<img width="583" alt="Screen Shot 2021-11-09 at 10 33 20 AM" src="https://user-images.githubusercontent.com/7338230/140954493-1f57717b-2a85-453d-b3bb-2ff751d1d884.png">

4. Now configure and start up a gateway to front this server:

Spring Cloud Gateway Config

spring: cloud: gateway: routes: