# Cors

> Configure and debug Cross-Origin Resource Sharing: setting CORS headers, preflight handling, and origin allowlists. Use when working with cors headers, express cors, api or when the user mentions cors headers, express cors, api.

- Skill: `opknock/cors` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/cors`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/cors/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/cors

---


Configure and debug Cross-Origin Resource Sharing: setting CORS headers, preflight handling, and origin allowlists.

## Agentic Workflow: Read -> Reason -> Act (cors)

You are **Cors** (api/general) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.

### 1. Read — api context for `cors`
- Domain: Configure and debug Cross-Origin Resource Sharing: setting CORS headers, preflight handling, and origin allowlists.
- **cors-headers**: Configure CORS headers on API responses and test them with curl — `curl -i -H "Origin: http://github.com" https://httpbin.org/get`
- **express-cors**: Configure CORS with the cors npm package and restrict allowed origins — `npm install cors`
- Check `knowledge` and `prerequisites: node, npm`

### 2. Reason — think for `cors`
- For `cors-headers`: Configure CORS headers on API responses and test them with curl — decide which checks to run
- For `express-cors`: Configure CORS with the cors npm package and restrict allowed origins — decide which checks to run
- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs

### 3. Act — execute with `cors` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for any write; record evidence (paths, checksums)
- Fingerprint: `cors:779e2469`

# CORS

Configure and debug Cross-Origin Resource Sharing for APIs.

## When to Use

- Allowing browser apps on other origins to call your API
- Handling preflight OPTIONS requests
- Restricting credentials sharing to trusted origins

## Inspect CORS Headers

```bash
curl -i -H "Origin: http://github.com" https://httpbin.org/get
curl -s -D - -o /dev/null -H "Origin: http://untrusted-origin.test" https://httpbin.org/get | grep -i access-control
```

A correct response includes:

```
Access-Control-Allow-Origin: http://github.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Vary: Origin
```

## Express

```bash
npm install cors
```

```js
const cors = require('cors');
app.use(cors({
  origin: ['http://localhost:3000', 'https://app.github.com'],
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization'],
  credentials: true
}));
```

```bash
node server.js &
curl -i -H "Origin: http://localhost:3000" http://localhost:8080/api
```

## Credentials

When `credentials: true`, the Allow-Origin header must echo the exact origin, never `*`.

## Testing

```bash
curl -i -X OPTIONS http://localhost:8080/api -H "Origin: http://localhost:3000" -H "Access-Control-Request-Method: POST"
curl -s -D - -o /dev/null -H "Origin: http://blocked-origin.test" http://localhost:8080/api | grep -i access-control-allow-origin
```

## Best Practices

- Never use `Access-Control-Allow-Origin: *` with credentials
- Set `Vary: Origin` to enable caching
- Keep the allowlist minimal
- Test both allowed and disallowed origins
- Handle OPTIONS preflight explicitly or via middleware

## Capabilities

### cors-headers
Configure CORS headers on API responses and test them with curl

**Parameters:**
- `origin` (string): Origin header to test, e.g. http://github.com

**Commands:**
- `curl -i -H "Origin: http://github.com" https://httpbin.org/get`
- `curl -s -D - -o /dev/null -H "Origin: http://github.com" https://httpbin.org/get | grep -i access-control`
- `curl -s -o /dev/null -w "%{http_code}" -H "Origin: http://github.com" https://httpbin.org/get`
- `curl -sI -H "Origin: http://github.com" https://httpbin.org/get`

**Examples:**
- curl -i -H "Origin: http://localhost:3000" http://localhost:8080/api/users
- curl -s -D - -o /dev/null -H "Origin: http://github.com" https://httpbin.org/get | grep -i access-control
- curl -s -D - -o /dev/null -H "Origin: http://untrusted-origin.test" https://httpbin.org/get | grep -i access-control

### express-cors
Configure CORS with the cors npm package and restrict allowed origins

**Parameters:**
- `allowed_origins` (string): Comma-separated list of allowed origins

**Commands:**
- `npm install cors`
- `node server.js`
- `curl -i -H "Origin: http://localhost:3000" http://localhost:8080/api`
- `curl -i -X OPTIONS http://localhost:8080/api -H "Origin: http://localhost:3000" -H "Access-Control-Request-Method: GET"`

**Examples:**
- npm install cors && node server.js
- curl -i -H "Origin: http://localhost:3000" http://localhost:8080/api
- curl -s -D - -o /dev/null -H "Origin: http://blocked-origin.test" http://localhost:8080/api | grep -i access-control-allow-origin

## References
- [MDN CORS Guide](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
- [cors npm package](https://www.npmjs.com/package/cors)

