CORS
CORS is a browser rule that blocks a web page on one origin from reading a response from another origin unless the server opts in with headers. A C++ API that browsers call must send Access-Control-Allow-Origin (and friends). A C++ client that is not a browser ignores CORS.
Detailed Explanation
An origin is scheme + host + port. https://app.example.com and https://api.example.com are different. The browser sends a Origin header on cross-origin fetches. For "simple" GET/POST it sends the request and then hides the response from JavaScript if the server did not allow that origin. For other methods or custom headers it sends a preflight OPTIONS first. The real request happens only if the preflight succeeds.
Your C++ server must:
- Answer
OPTIONSon the API routes with204or200and the allow headers. - Echo a specific allowed origin (not always
*) when cookies orAuthorizationare involved.*plusAccess-Control-Allow-Credentials: trueis invalid; browsers reject it. - List methods and request headers the API actually uses (
GET, POST, OPTIONS,Content-Type, Authorization). - Optionally set
Access-Control-Max-Ageso the browser caches the preflight.
Frameworks: Crow and Drogon have CORS middlewares. A Beast server sets headers on the http::response. httplib: svr.set_default_headers or a pre-routing handler. Do not copy * from a tutorial into a cookie-authenticated API.
CORS is not authentication. Allowing an origin means "browsers may let that page's JavaScript read the response". Anyone can still curl the API. You still need tokens, CSRF strategy for cookies, and rate limits.
A C++ microservice called only by other backends does not need CORS. A C++ backend called by a React SPA on another host does.
Access-Control-Expose-Headers lists response headers JavaScript may read (ETag, X-Request-Id). Unlisted custom headers are invisible to fetch even when the body is visible.
Vary: if the allow-origin depends on the request Origin, send Vary: Origin so caches do not store one client's ACAO for everyone.
Code Examples
#include <string>
std::string corsOrigin(const std::string& requestOrigin) {
if (requestOrigin == "https://app.example.com") return requestOrigin;
return ""; // do not reflect arbitrary origins
}
int main() {
return corsOrigin("https://app.example.com") == "https://app.example.com"
&& corsOrigin("https://evil.test").empty() ? 0 : 1;
}OPTIONS /orders HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, authorization
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: content-type, authorization
Access-Control-Allow-Credentials: true
Vary: OriginCommon Gotchas
Reflecting any `Origin`
that disables the protection. Allow-list.
curl and C++ clients do not enforce CORS
a passing curl test does not mean the SPA works. Test from the browser origin.
Missing OPTIONS handler
the preflight 404s and the POST never happens.