Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added whitelist to cors docs #1245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/docs/security/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ If your API requires a token to be sent in the `Authorization` header, then the

## CORS Requests and Cookies

If your API uses cookies (for authentication for example), then you should specify it in the hook with the `Access-Control-Allow-Credentials` header.
If your API uses cookies (for authentication for example), then you should specify it in the hook with the `Access-Control-Allow-Credentials` header. You should use a whitelist of allowed origins in this case.

```typescript
@Hook(ctx => response => {
response.setHeader('Access-Control-Allow-Origin', ctx.request.get('Origin') || '*');
response.setHeader('Access-Control-Allow-Credentials', 'true');
const whitelist = ['http://localhost:8000']; // get this data from Config / Env
const origin = ctx.request.get('Origin') || 'null';
if (whitelist.includes(origin)) {
response.setHeader('Access-Control-Allow-Origin', origin);
response.setHeader('Access-Control-Allow-Credentials', 'true');
} else {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Credentials', 'false');
}
})
```

Expand Down