DEPLOY_URL changes protocol automatically (https to http)
alexkalopsia
TRIALOP

a year ago

Context, I have my Flask python app running correctly on pythonanywhere and Render (same exact codebase connected).

I am testing Railway and I have an issue:

even though I set the env variable DEPLOY_URLhttps://myapp.up.railway.app and my code is

DEPLOY_URL = os.environ.get("DEPLOY_URL", "http://127.0.0.1:8000") and

```
print(f"DEPLOY TO: {DEPLOY_URL}")

    client_id, client_secret = Mastodon.create_app(

        "myapp",

        api_base_url=f"https://{instance}",

        redirect_uris=[

            "urn:ietf:wg:oauth:2.0:oob",

            f"{DEPLOY_URL}/authorized",

        ],

    )
```

the print statement correctly prints https://myapp.up.railway.app, but when I press the Login button on my app, the redirect uri becomes with http protocol https://mydomain.com/oauth/authorize?response_type=code&client_id=XXXXXXXXXXXXXXXXXX&redirect_uri=http://myapp.up.railway.app/authorized?client_id=XXXXXXXXXXXXXXXXXXXX&client_secret=XXXXXXXXXXXXXX&state=XXXXXXXXXXXX

Now, I literally have no idea how this is possible, given that the same thing works perfectly on the other platform. Is there anything that Railway does that might do some protocol change in the url?

Solved

5 Replies

brody
EMPLOYEE

a year ago

> Is there anything that Railway does that might do some protocol change in the url?

Nope, in fact it's the exact opposite, your application is sending a redirect for http and the other platforms mentioned will correct the redirect for you turning it into a redirect for https, on Railway we do not modify anything of this nature, so if your application is sending a redirect to http that's what your client will attempt.


alexkalopsia
TRIALOP

a year ago

Ooooh that makes sense I guess! I mean, I have no idea howf"{DEPLOY_URL}/authorized" would change in protocol, given that it prints correctly right above it, but I guess there is a chance create_app might be doing something odd, I'll look into that, thank you


brody
EMPLOYEE

a year ago

It could be that your application is unaware that's it's running behind a proxy and thus it sees the incoming request at http and sends a redirect for http as well.


alexkalopsia
TRIALOP

a year ago

That was exactly it, following some guidance online I solved the issue by adding

from werkzeug.middleware.proxy_fix import ProxyFix
app.config["PREFERRED_URL_SCHEME"] = "https"

app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)

Thank you very much for the support, it's been incredibly helpful


brody
EMPLOYEE

a year ago

Awesome, glad you could solve it!


Status changed to Solved brody about 1 year ago


Loading...