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

Skip request if origin is not allowed #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Skip request if origin is not allowed
  • Loading branch information
Miguel Martins committed Mar 30, 2017
commit 11abb36910b80affb66e0c56de1ad75aef3ac275
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function(options) {
// FIXME: origin can be promise
origin = options.origin(ctx);
if (!origin) {
return next();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a breaking change here, this will make requests which not from browser all failed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dead-horse isn't that covered by L53 already?

return;
}
} else {
origin = options.origin || requestOrigin;
Expand Down Expand Up @@ -99,7 +99,7 @@ module.exports = function(options) {
// The request is outside the scope of this specification.
if (!ctx.get('Access-Control-Request-Method')) {
// this not preflight request, ignore it
return next();
return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kurayama I think this is debatable and if the spec says it's of scope, then it is my understanding we should let the request pass on to the next middleware.

}

ctx.set('Access-Control-Allow-Origin', origin);
Expand Down
17 changes: 6 additions & 11 deletions test/cors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe('cors.test.js', function() {
.expect(204, done);
});

it('should not Preflight Request if request missing Access-Control-Request-Method', function(done) {
it('should skip if request missing Access-Control-Request-Method', function(done) {
request(app.listen())
.options('/')
.set('Origin', 'http://koajs.com')
.expect(200, done);
.expect(404, done);
});

it('should always set `Vary` to Origin', function(done) {
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('cors.test.js', function() {
const app = new Koa();
app.use(cors({
origin: function(ctx) {
if (ctx.url === '/forbin') {
if (ctx.get('origin') === 'forbin') {
return false;
}
return '*';
Expand All @@ -93,16 +93,11 @@ describe('cors.test.js', function() {
ctx.body = {foo: 'bar'};
});

it('should disable cors', function(done) {
it('should skip if origin not allowed', function(done) {
request(app.listen())
.get('/forbin')
.set('Origin', 'http://koajs.com')
.expect({foo: 'bar'})
.expect(200, function(err, res) {
assert(!err);
assert(!res.headers['access-control-allow-origin']);
done();
});
.set('Origin', 'forbin')
.expect(404, done);
});

it('should set access-control-allow-origin to *', function(done) {
Expand Down