sec-cors-validate Edge Function — Security
SecurityValidates a request Origin against an allow list and returns the correct CORS response headers to set — supports wildcards and credentials.
Edge function sec-cors-validate Validates a request Origin against an allow list and returns the correct CORS response headers to set — supports wildcards and credentials.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.
npx aerostack add navin/sec-cors-validate Use with AI Assistants
MCPConnect Claude, Cursor, or any MCP-compatible client — then call this function by slug
① Add MCP Server
Add this once — access all Aerostack functions from your AI tool.
{
"mcpServers": {
"aerostack": {
"url": "https://mcp.aerostack.dev",
"type": "http"
}
}
} ② Call this function
Ask your AI to use the call_function tool with this slug:
call_function({
slug: "sec-cors-validate",
args: {
"origin": "example_origin",
"allowList": null,
"allowCredentials": false
}
}) sec-cors-validate — Validate CORS origins
Checks a request's
Originheader against your allow list and returns the exact CORS headers to set. Supports exact matches, wildcard subdomains, and credential-bearing requests.
API
POST /api/sec-cors-validate
Request body
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
origin |
string | ✅ | — | The Origin header value from the incoming request |
allowList |
string[] | ✅ | — | Allowed origins — exact, wildcard (*.example.com), or * |
allowCredentials |
boolean | ❌ | false |
Whether to allow cookies/credentials |
Success response (200)
{
"success": true,
"data": {
"allowed": true,
"headers": {
"Access-Control-Allow-Origin": "https://app.example.com",
"Vary": "Origin",
"Access-Control-Allow-Credentials": "true"
}
}
}
Error responses
| Code | HTTP | When |
|---|---|---|
INVALID_INPUT |
400 | Missing origin or allowList |
INTERNAL_ERROR |
500 | Unexpected error |
Usage
cURL
curl -X POST "$FUNCTION_URL" \
-H "Content-Type: application/json" \
-d '{"origin": "https://app.example.com", "allowList": ["*.example.com"], "allowCredentials": true}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
origin: request.headers.get('origin') ?? '',
allowList: ['https://app.example.com', '*.staging.example.com'],
}),
});
const { data } = await response.json();
if (data.allowed) {
for (const [key, value] of Object.entries(data.headers)) {
response.headers.set(key, value);
}
}
Direct import (Node / Bun / Deno)
import { corsValidate } from '@aerostack/functions/sec-cors-validate';
const { allowed, headers } = corsValidate({
origin: req.headers.origin ?? '',
allowList: ['https://example.com', '*.example.com'],
});
Use Cases
- Implementing dynamic CORS middleware that reads allowed origins from a database or config
- Handling multi-tenant SaaS where each tenant has their own allowed origins
- Building API gateways that need to enforce per-endpoint CORS policies
- Validating preflight OPTIONS requests before allowing the actual request
Notes
- Wildcard pattern
*.example.commatchesapp.example.combut NOTexample.comitself - The global wildcard
*matches any origin — do NOT combine withallowCredentials: true(browsers will reject this) - When
allowed=false,headersis an empty object — do not set any CORS headers - Always set the
Vary: Originheader (included automatically) to prevent caching issues
Metadata
Tags
Publisher
@navin verified
Build and publish your own functions
Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.
More Security Functions
Browse Security Functions →sec-api-key-generate
by @navin
Generates a cryptographically random API key with a custom prefix using a base62 alphabet — no ambiguous characters, URL-safe.
sec-csp-generate
by @navin
Generates a Content-Security-Policy header value from a structured directives object — supports report-only mode and report-uri.
sec-decrypt-aes
by @navin
Decrypts an AES-256-GCM encrypted bundle produced by sec-encrypt-aes — key is derived via SHA-256, auth tag is verified automatically.
sec-encrypt-aes
by @navin
Encrypts a string with AES-256-GCM using Web Crypto — key is derived via SHA-256, output is a portable IV:ciphertext bundle.
sec-hash-sha256
by @navin
Hashes a string using SHA-256 via the Web Crypto API. Supports hex and base64 output. Zero dependencies.
sec-hmac-sign
by @navin
Signs a string payload with HMAC-SHA256 or HMAC-SHA512 using Web Crypto — outputs a hex-encoded signature.
Frequently asked questions
What does the sec-cors-validate function do? +
sec-cors-validate is a serverless edge function for security automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.
How do I deploy the sec-cors-validate function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/sec-cors-validate ``` It will be live on Cloudflare Workers in seconds.
What runtime does sec-cors-validate use? +
sec-cors-validate runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the sec-cors-validate function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.