angular: HttpClient HttpXsrfInterceptor does not set xsrf token for absolute urls?
I’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[X ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
In the HttpXsrfInterceptor xsrf.ts code, it is written in comments
// Skip both non-mutating requests and absolute URLs.
// Non-mutating requests don't require a token, and absolute URLs require special handling
// anyway as the cookie set
// on our origin is not the same as the token expected by another origin.
if (req.method === 'GET' || req.method === 'HEAD' || lcUrl.startsWith('http://') ||
lcUrl.startsWith('https://')) {
return next.handle(req);
}
So if the req.url starts with http or https, it does not set the header. What is the reason behind this? Both the wikipedia article on csrf or the owasp guide do not mention any special handling of absolute urls
Expected behavior
I think expected behavior should be the token should be added for all requests (except non-mutating ones) irrespective of being absolute urls
Environment
Angular version: 4.X
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 9
- Comments: 20 (2 by maintainers)
@jrhite until this is fixed you can write your own simple XSRFInterceptor as I did. As long as you are sure to call only one API there are no security implications through this approach.
With CORS you actually have to implement the TokenExtractor yourself as well…
A more sophisticated check for absolute URLs would be nice… Instead of checking for http:// or https:// it would be amazing to check if the absolute URL matches the URL the application is loaded from. In this case I guess it would be ok to automatically attach a XSRF header. This would resolve this issue for people using absolute URLs that point to the same server.
It seems to me that that it is necessary to improve the XSRF-support to become possible to use URLs starting with http(s)
Why do URLs starting with http(s) require special handling?
There are several possible situations:
ng serve
, and the backend is launched on another port/server, etc.)Thus, depending on the scenario, angular can support different strategies, for example:
or
or
From the user’s point of view, it can look something like this:
but in this case it is not possible to configure the cookie name for each domain individually.
It would be possible to give the user the ability to specify his own class that implements the HttpXsrfTokenExtractor interface (maybe this is possible?), But to work properly, it is necessary to delete the checks lcUrl.startsWith (‘http: //’) || lcUrl.startsWith (‘https: //’) in https://github.com/angular/angular/blob/54e02449549448ebab6f255f2da0b4396665c6f0/packages/common/http/src/xsrf.ts#L81-L82
@alxhub You can explain what the “and absolute URLs require special handling” comment means. Did I understand the problem correctly?
@haidelber Can you brief about tokenExtractor, I am getting token as null, Also i have tried to fetch token from cookie, code attached. I can see cookie in response of a first request. but while fetching i get document as empty.
@otello1971, I just checked CLI and if I’m not wrong this proxy only refers to
webpack dev server
, which wouldn’t help in production if client-side and API are not on the same domain.@netdeamon I suggest you add to the title that this is connected to HttpClient
“and absolute URLs require special handling” means that it’s up to the application to handle those URLs - the interceptor cannot know whether a given URL uses the same token, a different token, or no token at all. Providing an interceptor that can handle all of these cases is not in scope for
common/http
, but would be a great third-party library 😃This has two problems:
Also I found this issue #15052