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]},
);

/**
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 @@ -16,8 +16,8 @@ import {
} from '@angular/core';
import {Observable} from 'rxjs';

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 @@ -264,6 +264,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 @@ -314,7 +327,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