webpack-dev-server will change the Referer header or give a option to do this
Referer: http://localhost:4000/ sent to the server, and server rejected the request.
Add a changeReferer just like changeOrigin
Some server will check csrftoken with Referer, if Referer: localhost is provided, the request will be rejected by the server.
Now I use this as a temp solution.
bypass: (req, res) => {
if (req.headers && req.headers.referer)
req.headers.referer = req.headers.referer.replace('localhost:4000', 'THE TARGET SITE')
}
@Jack-Works thanks for creating a proper issue, and for checking in. Those configuration options are passed directly to http-proxy-middleware. webpack-dev-server doesn't control the options available, so you'll have to create an issue with that project to request that option.
@Jack-Works Did you manage to find a proper solution for this?
For anyone who might run into csrf issues:
Here is how I handled it:
const fs = require('fs');
module.exports = {
devServer: {
proxy: {
'^/api': {
target: 'https://example.com',
ws: true,
changeOrigin: true,
bypass: (req, res) => {
if (req.headers && req.headers.referer) {
url = new URL(req.headers.referer);
url.host = 'example.com';
url.port = '';
req.headers.referer = url.href;
}
},
cookieDomainRewrite: '',
},
},
http2: true,
https: {
key: fs.readFileSync('./cert/key.pem'),
cert: fs.readFileSync('./cert/cert.pem'),
},
},
};
Most helpful comment
For anyone who might run into csrf issues:
Here is how I handled it: