Talgat Omarov
Talgat Omarov

Reputation: 61

Hosting Google Cloud Run using Firebase Hosting raises Mixed Content error

I have a Python (FastAPI with uvicorn) application running in Docker container on Google Cloud Run (fully-managed version). I also have a separate web app running on Firebase which uses Firebase hosting. I want to integrate Cloud Run container with Firebase app using Firebase hosting. I set up the hosting config as written in https://firebase.google.com/docs/hosting/cloud-run.

"rewrites": [
  {
    "source": "/api/**",
    "run": {
      "serviceId": "myapp",
      "region": "us-central1"
    }
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
]

My Firebase app calls API using the following code

const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({payload  : payload, temperature: temperature})
    }

fetch('/api/generate', requestOptions)...

However, when I try doing so I get the following error from the browser console:

Mixed Content: The page at 'https://mydomain.web.app/' was loaded over HTTPS, but requested an insecure resource 'http://myapp-somerandomhash-uc.a.run.app/api/generate'. This request has been blocked; the content must be served over HTTPS.

(assuming the Firebase app is hosted on https://mydomain.web.app and Cloud Run app is hosted on https://myapp-somerandomhash-uc.a.run.app)

Current behaviour:

  1. The Firebase app calls https://mydomain.web.app/api/generate
  2. Firebase Hosting redirects it to http://myapp-somerandomhash-uc.a.run.app/api/generate
  3. (I get the above-mentioned error)
  4. The Cloud Run container app again redirects it to https://myapp-somerandomhash-uc.a.run.app/api/generate

Desired behaviour:

  1. The Firebase app calls https://mydomain.web.app/api/generate
  2. Gets response without explicit redirection or redirect to https version

Logs from Cloud Run:

INFO: 169.254.8.129:21990 - "POST /api/generate HTTP/1.1" 307 Temporary Redirect
POST 307 314 B 8ms Chrome 83 https://myapp-somerandomhash-uc.a.run.app/api/generate

Are there any suggestions about how to fix this problem?


Upvotes: 4

Views: 1191

Answers (1)

Talgat Omarov
Talgat Omarov

Reputation: 61

I managed to fix my problem. My developing environment was insensitive to trailing slashes, so I missed mismatch between .../api/generate/ and ../api/generate. Thus, after fixing this mismatch everything worked as expected.

I am not entirely sure which part of my stack caused the mentioned-error, but at least with Cloud Run (FastAPI + uvicorn) and Firebase Hosting trailing slashes seems to be important.

Upvotes: 2

Related Questions