Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Access-Control-Expose-Headers needed?

Tags:

cors

I was looking for the specific security reasons as to why this was added. It was kind of a WTH moment when I was implementing cors and could see all the headers being returned but I couldn't access them via javascript..

like image 746
Blake Niemyjski Avatar asked Sep 04 '14 18:09

Blake Niemyjski


People also ask

How do I access Access-Control expose headers?

If you want clients to be able to access other headers, you have to use the Access-Control-Expose-Headers header. The value of this header is a comma-delimited list of response headers you want to expose to the client.

Why do we need request header?

The HTTP headers are used to pass additional information between the clients and the server through the request and response header. All the headers are case-insensitive, headers fields are separated by colon, key-value pairs in clear-text string format. The end of the header section denoted by an empty field header.

Why is the header line important in HTTP request messages?

The request-header fields allow the client to pass additional information about the request, and about the client itself, to the server.


2 Answers

CORS is implemented in such a way that it does not break assumptions made in the pre-CORS, same-origin-only world.

In the pre-CORS world, a client could trigger a cross-origin request (for example, via a script tag), but it could not read the response headers.

In order to ensure that CORS doesn't break this assumption, the CORS spec requires the server to give explicit permissions for the client to read those headers (via the Access-Control-Expose-Headers header). This way, unauthorized CORS requests behave as they did in a pre-CORS world.

like image 176
monsur Avatar answered Oct 09 '22 22:10

monsur


Here is the reason why Access-Control-Expose-Headers is needed :

Access-Control-Expose-Headers (optional) - The XMLHttpRequest 2 object has a getResponseHeader() method that returns the value of a particular response header. During a CORS request, the getResponseHeader() method can only access simple response headers. Simple response headers are defined as follows:

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

If you want clients to be able to access other headers, you have to use the Access-Control-Expose-Headers header. The value of this header is a comma-delimited list of response headers you want to expose to the client.

for more reference please dig into the link https://www.html5rocks.com/en/tutorials/cors/

Happy coding !!

like image 29
Trilok Pathak Avatar answered Oct 09 '22 23:10

Trilok Pathak