Skip to content

Commit

Permalink
fix(cors): missing credentials option
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Oct 8, 2019
1 parent c798471 commit 0080ad3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/Cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export class Cors {
static allowedMethods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];
static allowedHeaders = [];
static maxAge = 600;
static origin = '*';
static origin = '';
static credentials = false;

static preflight(req: http.IncomingMessage, res: http.ServerResponse): boolean {
const origin = getHeader(req, 'origin');
if (!origin) return false;
res.setHeader('Access-Control-Allow-Origin', Cors.origin || origin);
Cors.credentials && res.setHeader('Access-Control-Allow-Credentials', 'true');
const isPreflight = getHeader(req, 'access-control-request-method') && req.method === 'OPTIONS';
if (!isPreflight) return false;
res.setHeader('Access-Control-Allow-Methods', Cors.allowedMethods.toString());
Expand Down
10 changes: 10 additions & 0 deletions test/Cors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,15 @@ describe('CORS', function() {
Cors.preflight(req, res);
expect(res.header('Access-Control-Allow-Headers')).to.be.undefined;
});
it('should set `Access-Control-Allow-Credentials` header', function() {
Cors.credentials = true;
Cors.preflight(req, res);
expect(res.header('Access-Control-Allow-Credentials')).to.be.equal('true');
});
it('should set custom origin', function() {
Cors.origin = '*';
Cors.preflight(req, res);
expect(res.header('Access-Control-Allow-Origin')).to.be.equal('*');
});
});
});

0 comments on commit 0080ad3

Please sign in to comment.