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

132 option to set the allowed cors headers #133

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
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: 11 additions & 2 deletions documentation/05_advanced_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,21 @@ const moduleImporter = async (specifier: string) => import(specifier);

startServer(moduleImporter).then(server =>
{
server.addMiddleware(new CorsMiddleware('https://jitar.dev'));
//server.addMiddleware(new CorsMiddleware()); // allow all origins and headers
//server.addMiddleware(new CorsMiddleware('https://jitar.dev')); // allow specific origin and all headers
server.addMiddleware(new CorsMiddleware('https://jitar.dev', 'Content-Type, Authorization')); // allow specific origin and headers
});
```

The first argument sets the [Access-Control-Allow-Origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin) header. This header only supports a single origin or a wildcard. The latter is the default when no origin is provided.

The second argument sets the [Access-Control-Allow-Headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers) header. This header supports a comma separated list of headers or a wildcard. The latter is the default when no headers are provided.

{:.alert-info}
The CORS middleware only supports a single origin or a wildcard. The latter is the default when no origin is provided.
The [Access-Control-Allow-Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods) header is always set to GET and POST because these are by default supported by the [RPC API](#rpc).

{:.alert-warning}
The [RPC API](#rpc) does not provide custom headers set by middleware in OPTIONS requests. This means that CORS preflight requests are not supported yet. Don't hesitate to create a [feature request](https://github.com/MaskingTechnology/jitar/issues/new/choose) if you need it. This helps up prioritize our time.

---

Expand Down
15 changes: 9 additions & 6 deletions packages/jitar/src/runtime/middleware/CorsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import NextHandler from '../types/NextHandler.js';

export default class CorsMiddleware implements Middleware
{
#origin: string;
#methods = ['GET', 'POST'];
#allowOrigin: string;
#allowMethods = 'GET, POST';
#allowHeaders: string;

constructor(origin = '*')
constructor(origin = '*', headers = '*')
{
this.#origin = origin;
this.#allowOrigin = origin;
this.#allowHeaders = headers;
}

async handle(fqn: string, version: Version, args: Map<string, unknown>, headers: Map<string, string>, next: NextHandler): Promise<unknown>
Expand All @@ -24,7 +26,8 @@ export default class CorsMiddleware implements Middleware

#setHeaders(headers: Map<string, string>): void
{
headers.set('Access-Control-Allow-Origin', this.#origin);
headers.set('Access-Control-Allow-Methods', this.#methods.join(', '));
headers.set('Access-Control-Allow-Origin', this.#allowOrigin);
headers.set('Access-Control-Allow-Methods', this.#allowMethods);
headers.set('Access-Control-Allow-Headers', this.#allowHeaders);
}
}