Skip to content

CORS Headers #2733

@diegossilveira

Description

@diegossilveira

When trying CORS support on Hapi, I came accross an implementation question. I supposed additionalExposedHeaders with override set to false works exposing extra headers, merging headers manually set. But it not works that way. The following code illustrates that:

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 3001 });

server.route({
  method: 'GET',
  path: '/',
  handler: function (request, reply) {
    var response = reply('Hello, world!')
    response.header('access-control-expose-headers', 'x-custom-header');
    response.header('x-request-id', '12345');
    response.header('x-custom-header', '12345');
  },
  config: {
    cors: {
      override: false,
      additionalExposedHeaders: ['x-request-id']
    }
  }
});

server.start(function () {
  console.log('Server running at:', server.info.uri);
});

Shouldn't it return an access-control-expose-headers with both values x-custom-header and x-request-id? Sometimes the handler function sets an access-control-expose-headers dynamically and is desirable that additionalExposedHeaders values are still set.

I think the following algorithm is suitable when the override flag is set to false:

Is access-control-expose-headers is already set?

  • YES merge values from additionalExposedHeaders
  • NO set header with additionalExposedHeaders value

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions