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 @@ -110,11 +110,14 @@ const RESTController = {
let response;
try {
response = JSON.parse(xhr.responseText);
const availableHeaders = 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) {
mortenmo marked this conversation as resolved.
Show resolved Hide resolved
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
20 changes: 20 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,7 @@ describe('ParseObject', () => {
it('can make changes while in the process of a save', async () => {
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -1717,6 +1718,7 @@ describe('ParseObject', () => {
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn(),
getAllResponseHeaders: jest.fn(),
};
xhrs.push(xhr);
return xhr;
Expand Down Expand Up @@ -1827,6 +1829,7 @@ describe('ParseObject', () => {
RESTController._setXHR(function () {
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -1957,6 +1960,7 @@ describe('ParseObject', () => {
const xhrs = [];
RESTController._setXHR(function () {
const xhr = {
getAllResponseHeaders: jest.fn(),
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn(),
Expand Down Expand Up @@ -2156,6 +2160,7 @@ describe('ParseObject', () => {
send: jest.fn(),
status: 200,
readyState: 4,
getAllResponseHeaders: jest.fn(),
};
xhrs.push(xhr);
return xhr;
Expand Down Expand Up @@ -2294,6 +2299,7 @@ describe('ParseObject', () => {
it('can destroy an object', async () => {
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2358,6 +2364,7 @@ describe('ParseObject', () => {
it('can save an array of objects', (done) => {
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2398,6 +2405,7 @@ describe('ParseObject', () => {
for (let i = 0; i < 2; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -2459,6 +2467,7 @@ describe('ParseObject', () => {
for (let i = 0; i < 2; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -2520,6 +2529,7 @@ describe('ParseObject', () => {
for (let i = 0; i < 2; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -2580,6 +2590,7 @@ describe('ObjectController', () => {
const objectController = CoreManager.getObjectController();
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2651,6 +2662,7 @@ describe('ObjectController', () => {
const objectController = CoreManager.getObjectController();
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2697,6 +2709,7 @@ describe('ObjectController', () => {
const objectController = CoreManager.getObjectController();
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2751,6 +2764,7 @@ describe('ObjectController', () => {
for (let i = 0; i < 3; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2833,6 +2847,7 @@ describe('ObjectController', () => {
for (let i = 0; i < 3; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2940,6 +2955,7 @@ describe('ObjectController', () => {
const objectController = CoreManager.getObjectController();
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
Expand Down Expand Up @@ -2982,6 +2998,7 @@ describe('ObjectController', () => {
for (let i = 0; i < 4; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -3023,6 +3040,7 @@ describe('ObjectController', () => {
for (let i = 0; i < 3; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -3108,6 +3126,7 @@ describe('ObjectController', () => {
for (let i = 0; i < 2; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -3277,6 +3296,7 @@ describe('ParseObject (unique instance mode)', () => {
it('can save an array of objects', async () => {
const xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
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
3 changes: 3 additions & 0 deletions src/__tests__/test_helpers/mockXHR.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function mockXHR(results, options = {}) {
getRequestHeader: function (key) {
return headers[key];
},
getAllResponseHeaders: function() {
mortenmo marked this conversation as resolved.
Show resolved Hide resolved
return Object.keys(headers).map(key => `${key}: ${headers[key]}`).join('\n');
},
upload: function () {},
send: function () {
this.status = results[attempts].status;
Expand Down
Loading