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
When sending a credentialed request , the llama.cpp server does not respond with the correct CORS preflight response.
Environment and Context
You can use this super simple llama client to test out the behavior. Write the url in the input field, for instance I have http://localhost:8083/v1/chat/completions) and press the "send with auth" button to see that it does not work. Open browser console to inspect that the OPTIONS flight request fails.
The almost identical request that does not use a credentialed request works as expected.
<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="viewport" content="width=device-width, initial-scale=1"><title>llama.cpp server CORS</title></head><body><buttonclass="without">Send without auth</button><buttonclass="with">Send with auth</button><inputtype="text" class="url" placeholder="localhost:8080/v1/completion"><pre><codeclass="out"></code></pre><scripttype="text/javascript">const$out=document.querySelector(".out");const$btn_without=document.querySelector(".without");const$btn_with=document.querySelector(".with");const$url=document.querySelector(".url");$btn_with.addEventListener("click",asyncfunction(){constres=awaitfetch($url.value,{method: "POST",headers: {Authorization: "no key","Content-Type": "application/json",},body: JSON.stringify({messages: [{role: "user",content: "What is the color of the sky?"}]})});$out.innerText=JSON.stringify(JSON.parse(awaitres.text()),null,4);});$btn_without.addEventListener("click",asyncfunction(){constres=awaitfetch($url.value,{method: "POST",headers: {"Content-Type": "application/json",},body: JSON.stringify({messages: [{role: "user",content: "What is the color of the sky?"}]})});$out.innerText=JSON.stringify(JSON.parse(awaitres.text()),null,4);});</script></body></html>
The text was updated successfully, but these errors were encountered:
This issue is relevant for browser-based clients that consume the server provided by llama.cpp. I've personally encountered it within an Obsidian plugin, but it can occur in any browser-based environment such as vscode, electron-based chat clients and any website.
My older post has some more details about how to implement this behavior correctly: #4198 (comment)
In short, for the preflight request
Response header Access-Control-Allow-Origin should be dynamically set to request.Origin, not *. That is the origin that the request came from, this is different for each application/computer/server etc, httplib.hpp stores this in req.origin iirc.
Response header Access-Control-Allow-Credentials should be set explicitly to true
I'm not 100% sure if any other headers have to be set to anything, I'd have to experiment a bit.
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Expected Behavior
I expect to be able to send either credentialed or uncredentialed responses without a difference in behavior in the server.
read here on MDN about "credentials" in the heading "Functional overview"
Current Behavior
When sending a credentialed request , the llama.cpp server does not respond with the correct CORS preflight response.
Environment and Context
You can use this super simple llama client to test out the behavior. Write the url in the input field, for instance I have
http://localhost:8083/v1/chat/completions
) and press the "send with auth" button to see that it does not work. Open browser console to inspect that the OPTIONS flight request fails.The almost identical request that does not use a credentialed request works as expected.
The text was updated successfully, but these errors were encountered: