-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
XHR Request fail with CORS Access-Control-Allow-Origin on Cordova android 10 #1354
Comments
Some background knowledge...
In cordova-android@10, we implemented something called a WebAssetLoader, which proxies requests through Cordova android 9.x uses the plain old file system (
But let's assume you don't want to use this workaroundCORS is a security mechanism for CORS-enabled browsers that are controlled by the backend server. So in this case, There is no API available in the webview to disable CORS. Assuming you don't have access to Another approach is to configure a proxy server that is configured to use the CORS protocol in which your app can make request to, which will be redirected to Now that we got all that information out there... May I ask more details on your use case? |
First of all thank you for the quick and detailled answer. We are trying to update cordova 9 existing apps which embend some xhr calls to an api and some other resources. These apps are internal apps not delivered on Google Play store then i think that we are going to use the workaround with cordova preferences you have explained in your first point. I've tested it with my hellocdv10 demo app and this configuration do the job. |
This maybe the most detailed answer I have ever read , thank you! |
hello after so much banging on this problem finally one that makes me understand everything. How do I understand it makes you emulate the device like it's a version 9? Go to the config.xml file and at what point? Why did I do this:
But it still doesn't work in me the state has always been 0 |
Is there a way to change what it sends as the source? We normally use appname.companyname.com in our URLs. |
CORS is a browser feature, so the feature isn't tied to Android versions. It depends on the Android Webview version that happens to be running. However I'm not sure when exactly CORS started being enforced in the Android Webview. iOS is slightly different in that their system webview is tied to the OS, and in particular they started enforcing CORS with their WKWebView available in iOS 9. Android Webview is an upgradeable package, independent from the OS so you can still have an Android 9 device (or an app running cordova-android@9) and still encounter CORS issues.
If by source, you mean the origin value, you have limited control over it. The origin is the scheme and domain of the document, or Starting on cordova-android@10 we have support for scheme handlers allowing you to change the scheme of the app, instead of using the For android, the default scheme if enabled is <preference name="scheme" value="https" /> <!-- This requires cordova-android@10.1 or later -->
<preference name="hostname" value="localhost" /> Note that for Android, the scheme must be either So by changing the scheme settings, you can influence the origin value. Don't forget that web storage features like local storage are tied to origins, each origin have their own database so by changing the scheme settings/origin, you will lose access to previously stored web storage data. |
Awesome, thank you very much |
I am commenting here due to Android 12 Behaviour changes. Can you give me an example? I am getting the below error when I give |
On Android, I'm not sure what happens if you're real server is |
Initially, I had the default config Note: I cannot change the cookie response on the server side. It's an ionic mobile app using Cordova. To overcome the above issue, I tried to configure a custom |
Wish I could help but I started having this problem a year or so ago and had to switch to the plugin cordova-plugin-advanced-http to bypass cookie security while we worked to remove all cookie authentication. All our problems are gone now that we switched to token auth. Cookies are a dead tech for use as logins. You can do the same, use that plugin to keep things going while you replace your cookies. https://learn.g2.com/cookieless-future |
Cookie policies, like the Access-Control CORS headers, are set by the server. If yo do not have access to the server side to allow cross origin cookies, then you effectively don't have permission to communicate with that server and that webserver only supports standard browsers that connect to the webserver directly.
This is wrong, because by setting your scheme scheme/hostname you won't actually hit your real server. The request gets intercepted by something called WebViewAssetLoader to load local files. It sounds like you're using a Cookies are intended to be set to it's own origin. It's a potential security risk to expose cookies cross origin, but with server configuration access, you can set the This thread is getting off topic so in order to respect the OP's inbox, I'm going to lock it here. If you have further questions on this matter, I'd suggest asking our Slack community. If you believe you've found a bug, then feel free to create a new issue. Edit: For those who have access to the server to edit cookie policies, you might be able to explicitly set your Then have your app scheme be set to a subdomain of that domain, e.g. MDN states
And they also state:
So this leads me to believe that it is possible to configure the server and the app in a way that the app will treat cookies as first-party cookies, but this needs to be tested and this still requires server side configurations. |
Bug Report
Problem
Simple GET xhr request (https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests) in cordova-android@^10.0.0 trigger CORS
What is expected to happen?
Simple xhr GET request should not trigger CORS
What does actually happen?
Simple xhr GET request should trigger CORS
Example:
Access to XMLHttpRequest at 'https://www.google.com/' from origin 'https://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Information
I have tested with two Cordova applications out of the box :
XHR request save as CURL from Chrome network
Result in Chrome console :
XHR request save as CURL from Chrome network
Result in Chrome console :
Command or Code
I've created two Cordova applications
Environment, Platform, Device
Device
Version information
First app
Second App
Cordova Cli: 10.0.0
Checklist
The text was updated successfully, but these errors were encountered: