Skip to content

Commit af04e26

Browse files
alan-agius4atscott
authored andcommitted
refactor(http): deprecate jsonp support
JSONP is deprecated because it is prone to Cross-Site Scripting (XSS) attacks. Since JSONP works by executing arbitrary scripts in the global context, it bypasses modern Content Security Policies (CSP) and can lead to severe security vulnerabilities if the server or endpoint is compromised. DEPRECATED: `HttpClient.jsonp`, `HttpClientJsonpModule`, and related JSONP classes/functions are deprecated. Use standard HTTP requests instead.
1 parent df77e42 commit af04e26

7 files changed

Lines changed: 36 additions & 19 deletions

File tree

goldens/public-api/common/http/errors.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export const enum RuntimeErrorCode {
2626
INTEGRITY_NOT_SUPPORTED_WITH_XHR = 2820,
2727
// (undocumented)
2828
INVALID_TIMEOUT_VALUE = 2822,
29-
// (undocumented)
29+
// @deprecated (undocumented)
3030
JSONP_HEADERS_NOT_SUPPORTED = 2812,
31-
// (undocumented)
31+
// @deprecated (undocumented)
3232
JSONP_WRONG_METHOD = 2810,
33-
// (undocumented)
33+
// @deprecated (undocumented)
3434
JSONP_WRONG_RESPONSE_TYPE = 2811,
3535
// (undocumented)
3636
KEEPALIVE_NOT_SUPPORTED_WITH_XHR = 2813,

goldens/public-api/common/http/index.api.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ export class HttpClient {
236236
} & HttpClientCommonOptions): Observable<HttpResponse<T>>;
237237
head(url: string, options?: HttpClientCommonOptions): Observable<Object>;
238238
head<T>(url: string, options?: HttpClientCommonOptions): Observable<T>;
239+
// @deprecated
239240
jsonp(url: string, callbackParam: string): Observable<Object>;
241+
// @deprecated
240242
jsonp<T>(url: string, callbackParam: string): Observable<T>;
241243
options(url: string, options: {
242244
observe?: 'body';
@@ -1208,7 +1210,7 @@ export abstract class HttpXsrfTokenExtractor {
12081210
static ɵprov: i0.ɵɵInjectableDeclaration<HttpXsrfTokenExtractor>;
12091211
}
12101212

1211-
// @public
1213+
// @public @deprecated
12121214
export class JsonpClientBackend implements HttpBackend {
12131215
constructor(callbackMap: JsonpCallbackContext, document: any);
12141216
handle(req: HttpRequest<never>): Observable<HttpEvent<any>>;
@@ -1218,7 +1220,7 @@ export class JsonpClientBackend implements HttpBackend {
12181220
static ɵprov: i0.ɵɵInjectableDeclaration<JsonpClientBackend>;
12191221
}
12201222

1221-
// @public
1223+
// @public @deprecated
12221224
export class JsonpInterceptor {
12231225
constructor(injector: EnvironmentInjector);
12241226
intercept(initialRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
@@ -1240,7 +1242,7 @@ export function withInterceptors(interceptorFns: HttpInterceptorFn[]): HttpFeatu
12401242
// @public
12411243
export function withInterceptorsFromDi(): HttpFeature<HttpFeatureKind.LegacyInterceptors>;
12421244

1243-
// @public
1245+
// @public @deprecated
12441246
export function withJsonpSupport(): HttpFeature<HttpFeatureKind.JsonpSupport>;
12451247

12461248
// @public

packages/common/http/src/client.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,6 @@ function addBody<T>(options: HttpClientCommonOptions, body: T | null): any {
9494
* this.httpClient.request('GET', this.heroesUrl + '?' + 'name=term', {responseType:'json'});
9595
* ```
9696
*
97-
*
98-
* ### JSONP Example
99-
* ```ts
100-
* requestJsonp(url, callback = 'callback') {
101-
* return this.httpClient.jsonp(this.heroesURL, callback);
102-
* }
103-
* ```
104-
*
10597
* ### PATCH Example
10698
* ```ts
10799
* // PATCH one of the heroes' name
@@ -1478,6 +1470,7 @@ export class HttpClient {
14781470
* @param callbackParam The callback function name.
14791471
*
14801472
* @return An `Observable` of the response object, with response body as an object.
1473+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
14811474
*/
14821475
jsonp(url: string, callbackParam: string): Observable<Object>;
14831476

@@ -1492,6 +1485,7 @@ export class HttpClient {
14921485
* then the `JSONP` request can be rejected by the configured backend.
14931486
*
14941487
* @return An `Observable` of the response object, with response body in the requested type.
1488+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
14951489
*/
14961490
jsonp<T>(url: string, callbackParam: string): Observable<T>;
14971491

@@ -1511,7 +1505,7 @@ export class HttpClient {
15111505
*
15121506
* @param url The resource URL.
15131507
* @param callbackParam The callback function name.
1514-
*
1508+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
15151509
*/
15161510
jsonp<T>(url: string, callbackParam: string): Observable<T> {
15171511
return this.request<any>('JSONP', url, {

packages/common/http/src/errors.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ export const enum RuntimeErrorCode {
2121
RESPONSE_IS_NOT_A_BLOB = 2807,
2222
RESPONSE_IS_NOT_A_STRING = 2808,
2323
UNHANDLED_OBSERVE_TYPE = 2809,
24+
/**
25+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
26+
*/
2427
JSONP_WRONG_METHOD = 2810,
28+
/**
29+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
30+
*/
2531
JSONP_WRONG_RESPONSE_TYPE = 2811,
32+
/**
33+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
34+
*/
2635
JSONP_HEADERS_NOT_SUPPORTED = 2812,
2736
KEEPALIVE_NOT_SUPPORTED_WITH_XHR = 2813,
2837
CACHE_NOT_SUPPORTED_WITH_XHR = 2814,

packages/common/http/src/jsonp.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const JSONP_ERR_HEADERS_NOT_SUPPORTED = 'JSONP requests do not support he
6060
*
6161
* In the browser, this should always be the `window` object.
6262
*
63-
*
63+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
6464
*/
6565
export abstract class JsonpCallbackContext {
6666
[key: string]: (data: any) => void;
@@ -72,7 +72,7 @@ export abstract class JsonpCallbackContext {
7272
* Ordinarily JSONP callbacks are stored on the `window` object, but this may not exist
7373
* in test environments. In that case, callbacks are stored on an anonymous object instead.
7474
*
75-
*
75+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
7676
*/
7777
export function jsonpCallbackContext(): Object {
7878
if (typeof window === 'object') {
@@ -88,6 +88,7 @@ export function jsonpCallbackContext(): Object {
8888
* @see {@link HttpXhrBackend}
8989
*
9090
* @publicApi
91+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
9192
*/
9293
@Injectable()
9394
export class JsonpClientBackend implements HttpBackend {
@@ -100,7 +101,14 @@ export class JsonpClientBackend implements HttpBackend {
100101
constructor(
101102
private callbackMap: JsonpCallbackContext,
102103
@Inject(DOCUMENT) private document: any,
103-
) {}
104+
) {
105+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
106+
console.warn(
107+
'JSONP support is deprecated as it can cause XSS vulnerabilities, and will be removed ' +
108+
'in a future version of Angular. Please use standard HTTP requests instead.',
109+
);
110+
}
111+
}
104112

105113
/**
106114
* Get the name of the next callback method, by incrementing the global `nextRequestId`.
@@ -286,6 +294,8 @@ export class JsonpClientBackend implements HttpBackend {
286294

287295
/**
288296
* Identifies requests with the method JSONP and shifts them to the `JsonpClientBackend`.
297+
*
298+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
289299
*/
290300
export function jsonpInterceptorFn(
291301
req: HttpRequest<unknown>,
@@ -306,6 +316,7 @@ export function jsonpInterceptorFn(
306316
* @see {@link HttpInterceptor}
307317
*
308318
* @publicApi
319+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
309320
*/
310321
@Injectable()
311322
export class JsonpInterceptor {

packages/common/http/src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class HttpClientModule {}
113113
* with method JSONP, where they are rejected.
114114
*
115115
* @publicApi
116-
* @deprecated `withJsonpSupport()` as providers instead
116+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Intent to remove in future versions of Angular.
117117
*/
118118
@NgModule({
119119
providers: [withJsonpSupport().ɵproviders],

packages/common/http/src/provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export function withNoXsrfProtection(): HttpFeature<HttpFeatureKind.NoXsrfProtec
240240
* Add JSONP support to the configuration of the current `HttpClient` instance.
241241
*
242242
* @see {@link provideHttpClient}
243+
* @deprecated 22.1 JSONP is deprecated as it can cause XSS vulnerabilities. Use standard HTTP requests instead. Intent to remove in future versions of Angular.
243244
*/
244245
export function withJsonpSupport(): HttpFeature<HttpFeatureKind.JsonpSupport> {
245246
return makeHttpFeature(HttpFeatureKind.JsonpSupport, [

0 commit comments

Comments
 (0)