Custom URI Scheme Origins fail CORS policy check when registered on in memory clients
Earlier today my team was working on setting up an IdentityServer client for a native application with a custom uri scheme: ionic://localhost. After running an initial test we noticed that the server was logging the message "CorsPolicyService did not allow origin: ionic://localhost", even though we had declared it in the AllowedCorsOrigins of the client.
After digging int the source we found the issue. When registering clients with the AddInMemoryClients builder method, the DefaultCorsPolicyService is overridden with an InMemoryCorsPolicyService. This service eventually runs the AllowedCorsOrigins from the registered clients through the following extension method:
public static string GetOrigin(this string url)
{
if (url != null)
{
Uri uri;
try
{
uri = new Uri(url);
}
catch (Exception)
{
return null;
}
if (uri.Scheme == "http" || uri.Scheme == "https")
{
return $"{uri.Scheme}://{uri.Authority}";
}
}
return null;
}
This causes declared origins for custom uri schemes to fail the IsOriginAllowedAsync in the InMemoryCorsPolicyService. We believe this is a bug, due to the fact that custom schemes should be allowed for native app flows, and would be happy to put a pull request in to remove the http/https scheme checks.
From what you can tell, is this actually preventing the authorize request from completing properly?
CORS is only pertinent from JS clients, so a native app is a different animal.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Questions are community supported only and the authors/maintainers may or may not have time to reply. If you or your company would like commercial support, please see here for more information.
This is an issue for apps built with frameworks like Cordova that wrap web apps into native clients: it produces JS clients that consider their origin to be 'capacitor://localhost' or some custom URL scheme. Also, the limitation doesn't seem to be documented anywhere: I had to read the source code to find out why our app wasn't able to authenticate. Please consider removing this restriction.
Do those embedded browsers actually enforce the same origin policy?
Yes, they do, or at least Cordova does - we have an app which is blocked (or, I suppose, is blocking itself) from authenticating due to this.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.