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] sap.ui.model.odata: add request header "X-Requested-With" #3324

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/sap.ui.core/src/sap/ui/model/odata/v2/ODataModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ sap.ui.define([
this.oHeaders["MaxDataServiceVersion"] = this.sMaxDataServiceVersion;
}

// indicate these requests are originating from an AJAX request
// with this header present @sap/approuter responds with a 401 on a GET request
this.oHeaders["X-Requested-With"] = "XMLHttpRequest";

},
metadata : {
publicMethods : ["read", "create", "update", "remove", "submitChanges", "getServiceMetadata", "metadataLoaded",
Expand Down
3 changes: 2 additions & 1 deletion src/sap.ui.core/src/sap/ui/model/odata/v4/lib/_Requestor.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ sap.ui.define([
"Accept" : "application/json;odata.metadata=minimal;IEEE754Compatible=true",
"OData-MaxVersion" : "4.0",
"OData-Version" : "4.0",
"X-CSRF-Token" : "Fetch"
"X-CSRF-Token" : "Fetch",
"X-Requested-With" : "XMLHttpRequest"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should WE do this? Shouldn't it be already done automaticall by jQuery.ajax()?

Their code says the following:

			// X-Requested-With header
			// For cross-domain requests, seeing as conditions for a preflight are
			// akin to a jigsaw puzzle, we simply never set it to be sure.
			// (it can always be set on a per-request basis or even using ajaxSetup)
			// For same-domain requests, won't change header if already provided.
			if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) {
				headers[ "X-Requested-With" ] = "XMLHttpRequest";
			}

Who am I to know this better? This sound dubious to me. I am not convinced, sorry.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make sense that this was indeed taken care of by jQuery.ajax(), but I didn't assume that they should. When I performed some tests I quickly noticed these headers were missing using the v2/v4 OData models.
I just did another test and it shows that these headers are present in bare $.ajax() calls, but not in ajax requests originating from UI5 v2/v4 models.

See network log: https://plnkr.co/edit/16J1TFICxbqETCzaxuZ0?preview
To whitelist your IP for the V2 CORS, click the button over at https://cors-anywhere.herokuapp.com/corsdemo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you shared the correct Plunker ID? It is not doing any OData calls in my case and in the code I also cannot see why it should.
With one of our test applications I can see the header X-Requested-With: XMLHttpRequest for the batch requests.

Copy link
Member

@codeworrior codeworrior Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uhlmannm The Plunker issues a same-origin request for its JSONModel data. I guess that's what @piejanssens was referring to.

As @ThomasChadzelek already pointed out with the code snippet, jQuery sends the X-Request-With=XMLHttpRequest header automatically, if and only if the outgoing request is a same-origin request and when the caller of $.ajax did not add the header already.

The reason for the same-origin condition is that X-Requested-With is not a CORS-safelisted header. Adding it to a cross-origin requests will therefore trigger an additional preflight request, which often is unwanted in the general case (e.g. for performance reasons or when the server is not prepared to handle it). Several frameworks therefore removed the X-Requested-With header again from their defaults (1) or limited it to the same-origin case (jQuery).

When there are already other non-safelisted headers (e.g. X-CSRF-Token) and when the server is known to allow the X-Requested-With header with CORS or when the server is in the same origin as the current page, then adding the header should be safe.

I just wonder how the ODataModel could know about the server capabilities.

[1] ReactiveX/rxjs#3273 , angular/angular.js#1004

};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ sap.ui.define([
"Accept" : "application/json",
"MaxDataServiceVersion" : "2.0",
"DataServiceVersion" : "2.0",
"X-CSRF-Token" : "Fetch"
"X-CSRF-Token" : "Fetch",
"X-Requested-With" : "XMLHttpRequest"
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ sap.ui.define([
delete mHeaders["MaxDataServiceVersion"];
delete mHeaders["sap-cancel-on-close"];
delete mHeaders["sap-contextid-accept"];
delete mHeaders["X-Requested-With"];
delete oActualRequest["_handle"];
delete oActualRequest["adjustDeepPath"];
delete oActualRequest["async"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ sap.ui.define([

assert.strictEqual(oModel.mCodeListModelParams, "~codeListModelParameters");
assert.strictEqual(oModel.sMetadataUrl, "~metadataUrl");
assert.deepEqual(oModel.oHeaders, {
"Accept": "application/json",
"Accept-Language": sap.ui.getCore().getConfiguration().getLanguageTag(),
"DataServiceVersion": "2.0",
"MaxDataServiceVersion": "2.0",
"sap-contextid-accept": "header",
"X-Requested-With": "XMLHttpRequest"
});
});

//*********************************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ sap.ui.define([
"Accept" : "application/json;odata.metadata=minimal;IEEE754Compatible=true",
"OData-MaxVersion" : "4.0",
"OData-Version" : "4.0",
"X-CSRF-Token" : "Fetch"
"X-CSRF-Token" : "Fetch",
"X-Requested-With": "XMLHttpRequest"
}
}, {
sODataVersion : "2.0",
Expand All @@ -251,7 +252,8 @@ sap.ui.define([
"Accept" : "application/json",
"MaxDataServiceVersion" : "2.0",
"DataServiceVersion" : "2.0",
"X-CSRF-Token" : "Fetch"
"X-CSRF-Token" : "Fetch",
"X-Requested-With": "XMLHttpRequest"
}
}].forEach(function (oFixture) {
var sTest = "factory function: check members for OData version = " + oFixture.sODataVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ sap.ui.define([
"Accept" : "foo",
"MaxDataServiceVersion" : "foo",
"DataServiceVersion" : "foo",
"X-CSRF-Token" : "foo"
"X-CSRF-Token" : "foo",
"X-Requested-With": "foo"
}
}].forEach(function (oRequestor) {
QUnit.test("check headers (V2): ", function (assert) {
Expand All @@ -63,7 +64,8 @@ sap.ui.define([
"Accept" : "application/json",
"MaxDataServiceVersion" : "2.0",
"DataServiceVersion" : "2.0",
"X-CSRF-Token" : "Fetch"
"X-CSRF-Token" : "Fetch",
"X-Requested-With": "XMLHttpRequest"
});
});
});
Expand Down