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

fix: Chrome browser console warning about unsafe header access-control-expose-headers when calling Cloud Function #2095

Merged
merged 10 commits into from
Apr 22, 2024
7 changes: 5 additions & 2 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ const RESTController = {
let response;
try {
response = JSON.parse(xhr.responseText);
const availableHeaders = typeof xhr.getAllResponseHeaders === 'function' ? xhr.getAllResponseHeaders() : "";
headers = {};
if (typeof xhr.getResponseHeader === 'function' && xhr.getResponseHeader('access-control-expose-headers')) {
if (typeof xhr.getResponseHeader === 'function' && availableHeaders?.indexOf('access-control-expose-headers') >= 0) {
const responseHeaders = xhr.getResponseHeader('access-control-expose-headers').split(', ');
responseHeaders.forEach(header => {
headers[header] = xhr.getResponseHeader(header.toLowerCase());
if (availableHeaders.indexOf(header.toLowerCase()) >= 0) {
headers[header] = xhr.getResponseHeader(header.toLowerCase());
}
});
}
} catch (e) {
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ describe('RESTController', () => {
getResponseHeader: function (header) {
return headers[header];
},
getAllResponseHeaders: function() {
return Object.keys(headers).map(key => `${key}: ${headers[key]}`).join('\n');
},
send: function () {
this.status = 200;
this.responseText = '{}';
Expand All @@ -241,6 +244,9 @@ describe('RESTController', () => {
getResponseHeader: function (header) {
return headers[header];
},
getAllResponseHeaders: function() {
return Object.keys(headers).map(key => `${key}: ${headers[key]}`).join('\n');
},
send: function () {
this.status = 200;
this.responseText = '{}';
Expand Down
Loading