Skip to content

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

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

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
63 changes: 63 additions & 0 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@
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 @@
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 @@ -253,6 +259,63 @@
expect(response._headers['X-Parse-Push-Status-Id']).toBe('5678');
});

it('does not call getRequestHeader with no headers or no getAllResponseHeaders', async () => {
const XHR = function () {};
XHR.prototype = {
open: function () {},
setRequestHeader: function () {},
getResponseHeader: jest.fn(),
send: function () {
this.status = 200;
this.responseText = '{"result":"hello"}';
this.readyState = 4;
this.onreadystatechange();
},
};
RESTController._setXHR(XHR);
await RESTController.request('GET', 'classes/MyObject', {}, {});
expect(XHR.prototype.getResponseHeader.mock.calls.length).toBe(0);

XHR.prototype.getAllResponseHeaders = jest.fn();
await RESTController.request('GET', 'classes/MyObject', {}, {});
expect(XHR.prototype.getAllResponseHeaders.mock.calls.length).toBe(1);
expect(XHR.prototype.getResponseHeader.mock.calls.length).toBe(0);
});

it('does not invoke Chrome browser console error on getResponseHeader', async () => {
const headers = {
'access-control-expose-headers': 'a, b, c',
'a' : 'value',
'b' : 'value',
'c' : 'value',
}
const XHR = function () {};
XHR.prototype = {
open: function () {},
setRequestHeader: function () {},
getResponseHeader: jest.fn(key => {
if (Object.keys(headers).includes(key)) {

Check failure on line 297 in src/__tests__/RESTController-test.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 10
return headers[key];

Check failure on line 298 in src/__tests__/RESTController-test.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 10 spaces but found 14
}

Check failure on line 299 in src/__tests__/RESTController-test.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 10
throw new Error("Chrome creates a console error here.");

Check failure on line 300 in src/__tests__/RESTController-test.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 10
}),
getAllResponseHeaders: jest.fn(() => {
return Object.keys(headers).map(key => `${key}: ${headers[key]}`).join('\r\n');

Check failure on line 303 in src/__tests__/RESTController-test.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 10
}),
send: function () {
this.status = 200;
this.responseText = '{"result":"hello"}';
this.readyState = 4;
this.onreadystatechange();
},
};
RESTController._setXHR(XHR);
await RESTController.request('GET', 'classes/MyObject', {}, {});
expect(XHR.prototype.getAllResponseHeaders.mock.calls.length).toBe(1);
expect(XHR.prototype.getResponseHeader.mock.calls.length).toBe(4);
});


it('handles invalid header', async () => {
const XHR = function () {};
XHR.prototype = {
Expand Down
Loading