Skip to content

Commit 382f088

Browse files
Dmitry Dushkinfacebook-github-bot
Dmitry Dushkin
authored andcommitted
Fix: respect "withCredentials: false" in RCTNetworking iOS (#24629)
Summary: Fixes #24080. Even with `withCredentials: false` network requests still sending cookies. Fix this behaviour according to https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials. [iOS] [Fixed] - Respect "withCredentials: false" in network requests Pull Request resolved: #24629 Differential Revision: D15120420 Pulled By: cpojer fbshipit-source-id: 78b9924436b02584c4fc1aa04763dff085eea78c
1 parent 6ab249f commit 382f088

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Libraries/Network/RCTNetworking.mm

+6-4
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,13 @@ - (RCTURLRequestCancellationBlock)buildRequest:(NSDictionary<NSString *, id> *)q
259259
NSURL *URL = [RCTConvert NSURL:query[@"url"]]; // this is marked as nullable in JS, but should not be null
260260
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
261261
request.HTTPMethod = [RCTConvert NSString:RCTNilIfNull(query[@"method"])].uppercaseString ?: @"GET";
262+
request.HTTPShouldHandleCookies = [RCTConvert BOOL:query[@"withCredentials"]];
262263

263-
// Load and set the cookie header.
264-
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:URL];
265-
request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
264+
if (request.HTTPShouldHandleCookies == YES) {
265+
// Load and set the cookie header.
266+
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:URL];
267+
request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
268+
}
266269

267270
// Set supplied headers.
268271
NSDictionary *headers = [RCTConvert NSDictionary:query[@"headers"]];
@@ -273,7 +276,6 @@ - (RCTURLRequestCancellationBlock)buildRequest:(NSDictionary<NSString *, id> *)q
273276
}];
274277

275278
request.timeoutInterval = [RCTConvert NSTimeInterval:query[@"timeout"]];
276-
request.HTTPShouldHandleCookies = [RCTConvert BOOL:query[@"withCredentials"]];
277279
NSDictionary<NSString *, id> *data = [RCTConvert NSDictionary:RCTNilIfNull(query[@"data"])];
278280
NSString *trackingName = data[@"trackingName"];
279281
if (trackingName) {

0 commit comments

Comments
 (0)