You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I mount gradio app on FastAPI, and use CORSMiddleware from FastAPI, it does not seem to apply restriction. It just reflects given origin header of request in access-control-allow-origin of response header . However, if I use just a FastAPI route rather than a gradio app, this does not happen.
Have you searched existing issues? 🔎
I have searched and found no existing issues
Reproduction
Here is the gradio mounted example (example-1):
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import gradio as gr
import uvicorn
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://example.com"]
)
with gr.Blocks() as demo:
gr.Textbox("hello")
app = gr.mount_gradio_app(app, demo, path="/")
uvicorn.run(app, host="0.0.0.0", port=55800)
Hi @SedatDe the reason for this is that Gradio includes its own cors middleware which behaves as you described. Perhaps you can get around this by mounting your cors middleware after mounting the gradio app -- can you try this?
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import gradio as gr
import uvicorn
app = FastAPI()
with gr.Blocks() as demo:
gr.Textbox("hello")
app = gr.mount_gradio_app(app, demo, path="/")
app.add_middleware(
CORSMiddleware,
allow_origins=["https://example.com"]
)
uvicorn.run(app, host="0.0.0.0", port=55800)
Hi @abidlabs thank you for your quick response.
I tried your suggestion, but I still get access-control-allow-origin: https://malicious.com response header.
Describe the bug
If I mount gradio app on FastAPI, and use CORSMiddleware from FastAPI, it does not seem to apply restriction. It just reflects given origin header of request in access-control-allow-origin of response header . However, if I use just a FastAPI route rather than a gradio app, this does not happen.
Have you searched existing issues? 🔎
Reproduction
Here is the gradio mounted example (example-1):
Here is the pure FastAPI example (example-2):
If I make a request with the following
Example-1 returns access-control-allow-origin with whatever is given in origin header.
Example-2 does not return access-control-allow-origin, namely applies the expected restriction.
Screenshot
No response
Logs
No response
System Info
Severity
Blocking usage of gradio
The text was updated successfully, but these errors were encountered: