Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/common/http/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {HttpHandler} from './backend';

import {HttpRequest} from './request';
import {HttpEvent} from './response';
import {xsrfInterceptorFn} from './xsrf';

/**
* Intercepts and handles an `HttpRequest` or `HttpResponse`.
Expand Down Expand Up @@ -200,7 +201,7 @@ export const HTTP_INTERCEPTORS = new InjectionToken<readonly HttpInterceptor[]>(
*/
export const HTTP_INTERCEPTOR_FNS = new InjectionToken<readonly HttpInterceptorFn[]>(
typeof ngDevMode !== 'undefined' && ngDevMode ? 'HTTP_INTERCEPTOR_FNS' : '',
{factory: () => []},
{factory: () => [xsrfInterceptorFn]},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems that when it became the default, it was omitted, or perhaps intentionally; if so, the documentation wasn't updated.

https://next.angular.dev/best-practices/security#httpclient-xsrf-csrf-security

);

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/common/http/src/xsrf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
import {Observable} from 'rxjs';
import {DOCUMENT, ɵparseCookieValue as parseCookieValue, PlatformLocation} from '../../index';

import {HttpHandler} from './backend';
import {HttpHandlerFn, HttpInterceptor} from './interceptor';
import type {HttpHandler} from './backend';
import type {HttpHandlerFn, HttpInterceptor} from './interceptor';
import {HttpRequest} from './request';
import {HttpEvent} from './response';

Expand Down
15 changes: 14 additions & 1 deletion packages/common/http/test/provider_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,19 @@ describe('provideHttpClient', () => {
});

describe('xsrf protection', () => {
it('should enable xsrf protection for the root-provided HttpClient', () => {
TestBed.configureTestingModule({
providers: [provideHttpClientTesting(), {provide: PLATFORM_ID, useValue: 'test'}],
});

setXsrfToken('abcdefg');

TestBed.inject(HttpClient).post('/test', '', {responseType: 'text'}).subscribe();
const req = TestBed.inject(HttpTestingController).expectOne('/test');
expect(req.request.headers.get('X-XSRF-TOKEN')).toEqual('abcdefg');
req.flush('');
});

it('should enable xsrf protection by default', () => {
TestBed.configureTestingModule({
providers: [
Expand Down Expand Up @@ -317,7 +330,7 @@ describe('provideHttpClient', () => {

TestBed.inject(HttpClient).post('/test', '', {responseType: 'text'}).subscribe();
const req = TestBed.inject(HttpTestingController).expectOne('/test');
expect(req.request.headers.has('X-Custom-Xsrf-Header')).toBeFalse();
expect(req.request.headers.has('X-XSRF-TOKEN')).toBeFalse();
req.flush('');
});

Expand Down
Loading