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

Conversation

piejanssens
Copy link

In case the session has timed out and a new GET request comes in, the @sap/approuter component responds with status 200 and HTML content to store session and redirect the browser. By indicating the request originates from an AJAX call, the @sap/approuter is informed that it should return a 401 in case the session has timed out.

In case the session has timed out and a new GET request comes in, the @sap/approuter component responds with status 200 and HTML content to store session and redirect the browser. By indicating the request originates from an AJAX call, the @sap/approuter is informed that it should return a 401 in case the session has timed out.
@CLAassistant
Copy link

CLAassistant commented Aug 23, 2021

CLA assistant check
All committers have signed the CLA.

@Shtilianova
Copy link
Contributor

Hello @piejanssens ,
Thank you for your pull request. I've created an internal record 2170172149. The status of the issue will be updated here in GitHub.
Regards, Diana

@tobiasso85
Copy link
Contributor

Hi @piejanssens ,

thanks for this contribution. The code looks good, but the tests need to be adjusted such that it runs through.
We require unit as well as integration tests.

Regards
Tobias

Copy link
Contributor

@tobiasso85 tobiasso85 left a comment

Choose a reason for hiding this comment

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

@piejanssens
Copy link
Author

Hi @tobiasso85,

Got fixes ready for 3/4, but could you elaborate on the first item?
I'm not sure what exactly you would like to have tested in the constructor (and why that is different from the other headers).

ODataModel (v2) constructor Unit test
https://github.com/SAP/openui5/blob/master/src/sap.ui.core/test/sap/ui/core/qunit/odata/v2/ODataModelNoFakeService.qunit.js#L110
--> check oHeaders

@tobiasso85
Copy link
Contributor

Since there is already a test for the constructor, and the constructor code is modified,
I would have expected something like:

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"
});

https://github.com/SAP/openui5/blob/master/src/sap.ui.core/test/sap/ui/core/qunit/odata/v2/ODataModelNoFakeService.qunit.js#L110
(at the end of this test)

Copy link
Author

@piejanssens piejanssens left a comment

Choose a reason for hiding this comment

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

fixed

Copy link
Contributor

@tobiasso85 tobiasso85 left a comment

Choose a reason for hiding this comment

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

looks good now 👍

Copy link
Member

@ThomasChadzelek ThomasChadzelek left a comment

Choose a reason for hiding this comment

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

What is the background of this change, especially concerning OData V4?

Copy link
Member

@ThomasChadzelek ThomasChadzelek left a comment

Choose a reason for hiding this comment

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

Sorry, I cannot yet approve this (regarding OData V4).

@@ -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

@tobiasso85
Copy link
Contributor

Hi @piejanssens ,

after quite some discussions we decided not against this change, but instead only take parts of it; this means we will align the behaviour of OData v2 and v4 such that the X-Requested-With Header only gets sent for non-cross origin requests.
This way we follow the jQuery way, because we do not know the server landscape. When using this header in a cross origin scenario unnecessary pre-flight requests could be the result and these we want to avoid.

The header can still be set manually though as part of the ODataModel configuration (e.g. in the manifest):

Regards
Tobias

@piejanssens
Copy link
Author

Hi @tobiasso85,

Since the default $.ajax behavior is only valid for same-origin requests the Plunkr was a bad example of course. I am perfectly OK with having to set custom headers for CORS requests. My real-world issue was raised from a same-origin scenario.

So if I get this right, this means at the moment UI5 strips away (or initializes the full headers array) the default present header from the XHR in a same-origin scenario and this is what will be reinstated?

Regards,

Pieter

@tobiasso85
Copy link
Contributor

Hi @tobiasso85,

Since the default $.ajax behavior is only valid for same-origin requests the Plunkr was a bad example of course. I am perfectly OK with having to set custom headers for CORS requests. My real-world issue was raised from a same-origin scenario.

So if I get this right, this means at the moment UI5 strips away (or initializes the full headers array) the default present header from the XHR in a same-origin scenario and this is what will be reinstated?

Regards,

Pieter

Hi Pieter,

I don't know if I get your question right;
in OData V2, it will set this header (if not already set) in the same-origin scenario (Behaviour like OData V4).

Regards
Tobias

@openui5bot openui5bot closed this in 15acac7 Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants