CORS (Cross-Origin Resource Sharing) ​
Cross-Origin Resource Sharing (CORS) is a W3C mechanism allowing web applications loaded in one origin to access resources located on a different domain.
HTTPX CORS Middleware ​
HTTPX provides standard CORS handling:
zig
const std = @import("std");
const httpx = @import("httpx");
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const io = std.Io.Threaded.global_single_threaded.io();
var server = try httpx.Server.init(allocator, io, .{ .port = 8080 });
defer server.deinit();
// Built-in CORS middleware (handles pre-flight + headers)
try server.use(httpx.middleware.cors);
try server.get("/api/user", userHandler);
try server.run();
}
fn userHandler(ctx: *httpx.Context) anyerror!httpx.Response {
_ = ctx;
return .{ .status = 200, .body = "{\"user\":\"Alice\"}", .contentType = "application/json" };
}Security Recommendations ​
- Avoid
*with Credentials: Never pairAccess-Control-Allow-Origin: *withAccess-Control-Allow-Credentials: true. - Whitelist Exact Origins: Validate the incoming
Originheader against an explicit allowed set rather than echoing arbitrary origins.
