I am trying to enable CORS in FastAPI on my localhost with credentials enabled. According to the docs we must explicitly set allow_origins in this case:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware, Response
app = FastAPI()
app.add_middleware(
CORSMiddleware, allow_origins=['http://localhost:8000'],
allow_credentials=True, allow_methods=['*'], allow_headers=['*'])
@app.get('/')
def main():
return Response('OK', status_code=200)
However, when making a request it does fail with
CORS Missing Allow Origin [Cross-source (cross-origin) request blocked: The same origin policy disallows reading the remote resource at
http://[::1]:8000/create_session/none. (Reason: CORS header'Access-Control-Allow-Origin'is missing). Status code: 200.]
const response = await fetch('http://localhost:8000/', {credentials: "include"});
The client is Firefox with a local file (file://.../main.html) opened.
I already tried all solutions from How can I enable CORS in FastAPI?. What's wrong?
Edit: My question is not a duplicate of How to access FastAPI backend from a different machine/IP on the same local network?, because the server and the client are on the same (local)host. Nevertheless I tried setting the suggested --host 0.0.0.0 and the error remains the same.
Also it's not a duplicate of FastAPI is not returning cookies to React frontend, because it suggests setting the CORS origin in the same way as the official docs, which does not work as I described above.
Since you haven't provided the actual CORS error in your question, it should be specified here for future readers visiting this post. When sending a JavaScript HTTP request to a server, in this case a FastAPI backend, through a local HTML file that has been loaded into the browser via a
file:///URL—e.g., by dragging-dropping the file into the browser or just double-clicking the file—with the URL in the browser's address bar looking like this:browsers that apply CORS would output the following errors. If you are using Chrome (or a chromium-based) browser, you would see a
CORS error(Cross-Origin Resource Sharing error:MissingAllowOriginHeader) under theStatuscolumn in theNetworktab/section, indicating that the response to the CORS request is missing the requiredAccess-Control-Allow-Originheader, in order to determine whether or not the resource can be accessed. The error is made more clear by looking at the console, after submitting the request:If you are using FireFox instead, you would see a similar error in the console:
The reason for the error(s) raised above is that you are trying to perform a cross-origin request through a script that is running via a
file:///URL. In that case, the client's origin isnull, but you only addedhttp://localhost:8000to the allowed origins. Thus, a quick fix would be to addnullto theoriginslist, e.g.,Access-Control-Allow-Origin: null. In FastAPI, you could do that as shown in the example below (however, it is not recommended to do so—see more details below the example).Working Example
app.py
index.html
Note
However, it is not recommended doing that, as it is insecure. As described in MDN's documentation:
Another great article on the risks of using the
nullorigin, as well as misconfiguring CORS in general, can be found here.As explained in several related questions, as shown here and here, one should rather serve the HTML file(s), e.g.,
index.htmlabove, using a local web server. FastAPI can do that for you as well, using theHTMLResponseor evenJinja2Templates, as demonstrated in this answer, this answer, as well as this, this and this. Thus, I would strongly recommend having a look at those posts and serve your HTML file(s) through FastAPI, which is pretty straightforward, instead of addingnullto theorigins.Related answers on CORS can also be found here and here.