azukaar / Cosmos-Server

☁️ The Most Secure and Easy Selfhosted Home Server. Take control of your data and privacy without sacrificing security and stability (Authentication, anti-DDOS, anti-bot)
https://cosmos-cloud.io
Other
3.65k stars 130 forks source link

[BUG]: Chaining two levels of Cosmos is not possible #204

Closed athiffau closed 9 months ago

athiffau commented 9 months ago

What happened?

I am trying to chain two level of proxy using Cosmos but I get a StatusBadRequest (400) when the /cosmos/api/me end-point is loaded

What should have happened?

It should load the endpoint on the second layer instead of the first layer.

How to reproduce the bug?

I've been working on trying to chain two layers of proxy using Cosmos for a few days without success so I started to step through the code and this is why I think it currently cannot work. I'm not sure how to report this so hopefully this makes sense.

On xxx.xxx.xxx.193 I have a ubuntu server + cosmos installed = first layer I have the hostname of the first layer set to mydomain.com [internal IP xxx.xxx.xxx.193] *.mydomain.com is pointing to [public ip] -> OPNSense firewall -> xxx.xxx.xxx.193 mydomain has wildcard SSL with DNS challenge setup = working perfectly. I have a bunch of apps running on app[x].mydomain.com = working perfectly; note that some are in docker containers but some are proxied to external, standalone boxes. One example is HomeAssitant [Internal IP xxx.xxx.xxx.164] = working perfectly I create a proxy url as devbox.mydomain.com with target set to http://xxx.xxx.xxx.106

On xxx.xxx.xxx.106 I have a ubuntu server + cosmos installed = second layer SSL is disabled on the dev box. hostname is set to devbox.mydomain.com

I launch the url devbox.mydomain.com and this is what I see:

                if og != reqHostNoPort {
            PushShieldMetrics("hostname")
            Error("Invalid Hostname " + r.Host + " for request.", nil)
            w.WriteHeader(http.StatusBadRequest)
            http.Error(w, "Bad Request: Invalid hostname. Use your domain instead of your IP to access your server. Check logs if more details are needed.", http.StatusBadRequest)

From what I can tell, cosmos running in layer 1 starts parsing the url and run its checks and compares 'mydomain.com' to 'devbox.mydomain.com' which results in an invalid hostname. I think it's due to the redirect.

The solution is maybe to change function EnsureHostnameCosmosAPI [middleware.go] to evaluate all proxied hostnames as well - similar to what is done in function EnsureHostname [middleware.go]

Relevant log output

No response

Other details

No response

System details

azukaar commented 9 months ago

This is by design. This is done to prevent apps to overwrite Cosmos' endpoint into phishing pages to steal credentials and cannot be solved. If you want to access a Cosmos instance from another one, the only solution is Constellation