ionic-cli icon indicating copy to clipboard operation
ionic-cli copied to clipboard

Origin is always present on proxy for ionic serve

Open mburger81 opened this issue 9 years ago • 8 comments

Short description of the problem:

We need to call an endpoint, which does control for CORS. As described in many posts and howto's, using proxy configuring it in ionic.config.json like this

"proxies": [{
      "path": "/cloud",
      "proxyUrl": "https://blabla.com"
  }]

should resolve the CORS issue

But it seems there is a bug, because it doesn't work. We sniff with wireshark the headers which are passed after proxy to proxUrl and using ionic serve in the header there is always the origin header like this: image So there is http://localhost:8100 for origin but there should not be origin header.

What behavior are you expecting?

Proxy should NO pass origin header to resolve CORS issue

Post the output of ionic info below please global packages: @ionic/cli-utils : 1.0.0 Cordova CLI : 7.0.0 Ionic CLI : 3.0.0

local packages: @ionic/app-scripts : 1.3.7 @ionic/cli-plugin-cordova : 1.0.0 @ionic/cli-plugin-ionic-angular : 1.0.0 Ionic Framework : ionic-angular 3.0.1

System: Node : v6.10.2 OS : Linux 4.10 Xcode : not installed ios-deploy : not installed ios-sim : not installed

mburger81 avatar May 12 '17 09:05 mburger81

Can someone help for this bug?

mburger81 avatar May 15 '17 16:05 mburger81

I Opened also a forum request for this issue, to have more attention. But I think this should be a cli bug! https://forum.ionicframework.com/t/origin-is-always-present-on-proxy-for-ionic-serve/90450

mburger81 avatar May 16 '17 07:05 mburger81

I go further to the bug. It seems also the old CLI using proxy was always sending the origin from ionic serve.

Perhaps you can implement a new FR, to omit sending origin, this is needed on CORS check for SERVER side not only for BROWSER side which are the proxy do for now, I think this should be a valid request?

I check your code and you are probably using this middleware, right? https://www.npmjs.com/package/http-proxy-middleware

There you have an option called changeOrigin and this event:

function onProxyReq(proxyReq, req, res) {
    // add custom header to request 
    proxyReq.setHeader('x-added', 'foobar');
    // or log the req 
}

Perhaps we can do something with that?

Edit: Okay you probably use this middleware: https://github.com/gonzalocasas/node-proxy-middleware

mburger81 avatar May 16 '17 12:05 mburger81

We found a workaround, we start another node proxy which is handling the Origin and others like cookie rewriting, this is our server.js we use for standalone proxy

var http = require('http'),
    httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer({});
proxy.on('proxyReq', function(proxyReq, req, res, options) {
    proxyReq.setHeader('Origin', 'https://bla.bla.com');
});

proxy.on('proxyRes', function (proxyRes, req, res) {

    let existingCookies = proxyRes.headers['set-cookie'],
    rewrittenCookies = [];

    if (existingCookies !== undefined) {
        if (!Array.isArray(existingCookies)) {
            existingCookies = [existingCookies];
        }

        for (let i = 0; i < existingCookies.length; i++) {
             rewrittenCookies.push(existingCookies[i].replace(/;\s*?(Secure)/i, ''));
        }

        proxyRes.headers['set-cookie'] = rewrittenCookies;
    }
});

var server = http.createServer(function(req, res) {

  proxy.web(req, res, {
    target: 'https://bla.bla.com',
    changeOrigin: true
  });
});

console.log("listening on port 5050")
server.listen(5050);

And our ionic.config.json for the proxy part is like this

"proxies": [{
      "path": "/cloud",
      "proxyUrl": "http://localhost:5050/"
  }]

mburger81 avatar May 24 '17 13:05 mburger81

@mburger81 Hi

I am facing the same issue and i tried to use ionic proxy as well as your method of working arround it . Both unsuccessfully. Maybe i am doing somethign wrong. Please help me out.

Firstly my environment info

ionic info

global packages:

    @ionic/cli-plugin-proxy : 1.2.0
    @ionic/cli-utils        : 1.1.2
    Cordova CLI             : 7.0.1 
    Ionic CLI               : 3.1.2

local packages:

    @ionic/app-scripts              : 1.3.7
    @ionic/cli-plugin-cordova       : 1.1.2
    @ionic/cli-plugin-ionic-angular : 1.1.2
    Ionic Framework                 : ionic-angular 3.2.1

System:

    Node       : v7.10.0
    OS         : macOS Sierra
    Xcode      : Xcode 8.3.2 Build version 8E2002 
    ios-deploy : 1.9.1 
    ios-sim    : not installed

First attempt arround cors using ionic proxy

I added the below to my ionic.config.json

"proxies": [
    	{
        "path": "/j_security_check",
        "proxyURL": "https://mobile-qa.xyz.org/webapp"
	    },
	    {
        "path": "/rest/private/accounts",
        "proxyUrl": "https://mobile-qa.xyz.org/webapp"

	    }
	]

issue with this approach

when ever i do ionic serve, i get the below error message [15:23:59] Failed to read the projects ionic.config.json file: Parameter "url" must be a string, not undefined

Second attempt using your example shown above. i installed the following http-proxy server npm install http-proxy --save-dev

and added the server.js file as you showed in project's root folder.

issue with this approach

  1. I get a red underline in sublime text under require keyword. Where did you create your server.js?
  2. once you create server.js. do you configure it in webpack to start along with the project? how are you starting it?

I will be waiting for your reply. My brain is fried for today.. any help is much appreciated boss.

JayRaparla avatar May 30 '17 20:05 JayRaparla

Hi @JayRaparla First attempt and second attempt honestly have nothing to do with each other.

For the first attempt: You have a typing error for proxyURL, ...Url and not ...URL, upper case error.

For the second attempt: I do not install an http-proxy in ionic project!

var http = require('http'),
    httpProxy = require('http-proxy');

I'm not sure but I think http and http-proxy always are available and if not, you have to install it globally.

Your questions:

  1. I created my server.js in a subfolder under my ionic project, I called the directory node_cloud_proxy and where I have my server.js
  2. I'm not so good on webpack, so I wasn't able to launch it with ionic serve, so I launch node server.js from a second terminal, I know it is not NICE :o, but to my defense I didn't google so much :) perhpase you find a solution and can help me too? :)

BTW In your ionic.config.json you are redirect it to an https server, but you have to redirect it still to an http localhost server which is listening your server.js, and there you can redirect to your https server.

Let me know if I can help you

mburger81 avatar May 31 '17 17:05 mburger81

@mburger81 Thanks Buddy. I just saw this and it makes sense. I will try it out and let you know how it goes. Really appreciate you taking the time.

JayRaparla avatar May 31 '17 21:05 JayRaparla

I close this issue for the reason that on @ionic/angular we can use the default angular proxy which resolves this issue!

Thx

mburger81 avatar Apr 17 '19 14:04 mburger81