Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Custom NSURLSession configuration #27701

Closed

Conversation

hakonk
Copy link
Contributor

@hakonk hakonk commented Jan 7, 2020

Summary

While it is possible in the React Native implementation for Android to provide a custom configuration for HTTP requests, the iOS implementation does not allow for the same customization. As the NSURLSession used for HTTP requests on iOS is configured internally, one may for instance not supply an ephemeral configuration for HTTP requests. Other concerns related to the given problem have been addressed in the community: react-native-community/discussions-and-proposals#166. I did make a PR with an RFC in the community repo, but after some discussion in the said repo, I figured I might as well make a PR with a suggestion :)

Changelog

[iOS] [Added] - Allow for configuring the NSURLSessionConfiguration

Implement a C function RCTSetCustomNSURLSessionConfigurationProvider which gives the app programmer the ability to provide a block which provides an NSURLSessionConfiguration that will be used for all HTTP requests instead of the default configuration. The provided block will be called when the session configuration is needed.

Test Plan

Unsure if this can be tested in any other way than uncommenting the example code in RNTester/RNTester/AppDelegate.mm.

…tHandler to allow for configuring the NSURLSession. Add code that is commented out in AppDelegate for testing different configurations
@facebook-github-bot
Copy link
Contributor

Hi hakonk! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file.In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@react-native-bot react-native-bot added Platform: iOS iOS applications. Type: Enhancement A new feature or enhancement of an existing feature. labels Jan 7, 2020
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 7, 2020
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@akshetpandey
Copy link

+1 On this, this will greatly simplify implementations that need to use their own NSURLSessionConfiguration, ex. RCTCronetHTTPRequestHandler.mm

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@zsiegel
Copy link

zsiegel commented Jul 16, 2020

Any movement on this? I am looking for the ability to customize the iOS networking client like you do on Android.

@analysis-bot
Copy link

analysis-bot commented Jul 23, 2020

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 9,240,042 +50
android hermes armeabi-v7a 8,749,734 +50
android hermes x86 9,702,640 +38
android hermes x86_64 9,667,522 +43
android jsc arm64-v8a 10,885,299 +2
android jsc armeabi-v7a 9,786,267 +4
android jsc x86 10,943,318 +13
android jsc x86_64 11,549,717 +12

Base commit: 286fac5

@analysis-bot
Copy link

analysis-bot commented Jul 23, 2020

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 286fac5

@hakonk
Copy link
Contributor Author

hakonk commented Jul 24, 2020

@zsiegel I haven't heard from the review team. I assume they've had their hands full. Not sure if I should tag someone in the discussion, and in that case, who.

@safaiyeh
Copy link
Contributor

Hi @hakonk thanks for the PR, I'll see if I can ping someone for this.

@hakonk
Copy link
Contributor Author

hakonk commented Jul 24, 2020

Thanks, @safaiyeh 😊

@hakonk
Copy link
Contributor Author

hakonk commented Aug 10, 2020

Hey @safaiyeh, did you have any luck finding anyone?

@safaiyeh
Copy link
Contributor

Hi @TheSavior, could someone check this out?

@namchuai
Copy link

namchuai commented Dec 2, 2020

Nearly a year already..

@hakonk
Copy link
Contributor Author

hakonk commented Jan 13, 2021

Happy new year!

I guess there are more important PRs in the pipe still. Any news @safaiyeh ?

@pke
Copy link

pke commented May 19, 2021

Interesting that this important (for SSL Pinning that can't use sizzling) PR is still not merged.

@@ -82,6 +82,12 @@ @implementation AppDelegate
- (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RCTEnableTurboModule(YES);
// uncomment to configure NSURLSession
Copy link
Member

Choose a reason for hiding this comment

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

We probably don’t need to add commented code here as this is an uncommon use case. Perhaps this explanation of usage should just live in the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I can add a section about that. Would it be appropriate to add a section here for instance?

Copy link
Member

Choose a reason for hiding this comment

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

I think so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have created a PR that contains the needed documentation now: facebook/react-native-website#2629

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, the commented code has been removed in: 1634c87

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TheSavior Please see messages above 🙂

@facebook-github-bot
Copy link
Contributor

@sshic has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@JoshuaGross has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@JoshuaGross merged this pull request in 58444c7.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jun 3, 2021
Titozzz pushed a commit to Titozzz/react-native that referenced this pull request Jun 7, 2021
Summary:
While it is possible in the React Native implementation for Android to provide a custom configuration for HTTP requests, the iOS implementation does not allow for the same customization. As the NSURLSession used for HTTP requests on iOS is configured internally, one may for instance not supply an ephemeral configuration for HTTP requests. Other concerns related to the given problem have been addressed in the community: react-native-community/discussions-and-proposals#166. I did make a PR with an RFC in the community repo, but after some discussion in the said repo, I figured I might as well make a PR with a suggestion :)

## Changelog

[iOS] [Added] - Allow for configuring the NSURLSessionConfiguration

Implement a C function `RCTSetCustomNSURLSessionConfigurationProvider` which gives the app programmer the ability to provide a block which provides an NSURLSessionConfiguration that will be used for all HTTP requests instead of the default configuration. The provided block will be called when the session configuration is needed.

Pull Request resolved: facebook#27701

Test Plan: Unsure if this can be tested in any other way than uncommenting the example code in `RNTester/RNTester/AppDelegate.mm`.

Reviewed By: yungsters

Differential Revision: D28680384

Pulled By: JoshuaGross

fbshipit-source-id: ae24399955581a1cc9f4202f0f6f497bfe067a5c
tido64 pushed a commit that referenced this pull request Jun 8, 2021
Summary:
While it is possible in the React Native implementation for Android to provide a custom configuration for HTTP requests, the iOS implementation does not allow for the same customization. As the NSURLSession used for HTTP requests on iOS is configured internally, one may for instance not supply an ephemeral configuration for HTTP requests. Other concerns related to the given problem have been addressed in the community: react-native-community/discussions-and-proposals#166. I did make a PR with an RFC in the community repo, but after some discussion in the said repo, I figured I might as well make a PR with a suggestion :)

## Changelog

[iOS] [Added] - Allow for configuring the NSURLSessionConfiguration

Implement a C function `RCTSetCustomNSURLSessionConfigurationProvider` which gives the app programmer the ability to provide a block which provides an NSURLSessionConfiguration that will be used for all HTTP requests instead of the default configuration. The provided block will be called when the session configuration is needed.

Pull Request resolved: #27701

Test Plan: Unsure if this can be tested in any other way than uncommenting the example code in `RNTester/RNTester/AppDelegate.mm`.

Reviewed By: yungsters

Differential Revision: D28680384

Pulled By: JoshuaGross

fbshipit-source-id: ae24399955581a1cc9f4202f0f6f497bfe067a5c
Setito pushed a commit to Setito/react-native that referenced this pull request Jul 17, 2021
Summary:
While it is possible in the React Native implementation for Android to provide a custom configuration for HTTP requests, the iOS implementation does not allow for the same customization. As the NSURLSession used for HTTP requests on iOS is configured internally, one may for instance not supply an ephemeral configuration for HTTP requests. Other concerns related to the given problem have been addressed in the community: react-native-community/discussions-and-proposals#166. I did make a PR with an RFC in the community repo, but after some discussion in the said repo, I figured I might as well make a PR with a suggestion :)

## Changelog

[iOS] [Added] - Allow for configuring the NSURLSessionConfiguration

Implement a C function `RCTSetCustomNSURLSessionConfigurationProvider` which gives the app programmer the ability to provide a block which provides an NSURLSessionConfiguration that will be used for all HTTP requests instead of the default configuration. The provided block will be called when the session configuration is needed.

Pull Request resolved: facebook#27701

Test Plan: Unsure if this can be tested in any other way than uncommenting the example code in `RNTester/RNTester/AppDelegate.mm`.

Reviewed By: yungsters

Differential Revision: D28680384

Pulled By: JoshuaGross

fbshipit-source-id: ae24399955581a1cc9f4202f0f6f497bfe067a5c
@taylorkline
Copy link

taylorkline commented Aug 10, 2021

@pke you mentioned this is useful for SSL pinning.

Can you elaborate on how? From what I can tell, Apple's docs on performing manual server trust authentication state that we should provide an NSURLSessionDelegate that evaluates sever trust within urlSession(_:didReceive:completionHandler:) (or URLSession:didReceiveChallenge:completionHandler: in obj-c).

This PR does not allow us to set our own NSURLSessionDelegate, so how would this be useful in evaluating server trust?

@hakonk definitely open to your input as well. Thank you for this PR.

@hakonk
Copy link
Contributor Author

hakonk commented Aug 10, 2021

@taylorkline I believe you are right in that you would have to implement NSURLSessionDelegate as a part of implementing certificate or public key pinning, and this PR does not address that particular problem. In RCTHTTPRequestHandler.mm:99, you can see that self is assigned as the delegate for the NSURLSession instance. Thus, there is currently no way of supplying the delegate implementation from outside of React Native.

You can make use of swizzling when you do not have the means to implement NSURLSessionDelegate yourself. While I guess this would work most of the time, this mandates potential use of swizzling in other dependencies or your own code. Not really a problem in itself, it just isn't so transparent given that funky stuff happens at runtime. https://github.com/datatheorem/TrustKit does actually allow for implementing public key pinning with swizzling. That could possibly be useful if you want to achieve certificate or public key pinning in a React Native app.

@taylorkline
Copy link

@hakonk Thank you for confirming and thank you for your time.

@pke
Copy link

pke commented Aug 10, 2021

@taylorkline iirc one can only sizzle functions once. So if one react native module sizzles the NSURLSession (for certificate pinning) then no other module could sizzle the session. The ability to provide the pinning configuration for the sessions directly removes the need to use sizzling for it. So thanks for merging this!

@taylorkline
Copy link

Is this in v0.65.0? The commit does not appear to be tagged with v0.65.0.

@elicwhite
Copy link
Member

0.65 was cut on April 28th, before this PR was merged. This PR will be included in 0.66

@hakonk
Copy link
Contributor Author

hakonk commented Aug 18, 2021

@TheSavior I'm a bit confused. The changelog for 0.65.0 lists this PR.

@elicwhite
Copy link
Member

Ah, I was looking at the tags on the commit as @taylorkline mentioned. It looks like this commit was cherry picked and thus doesn't have the same hash in the release branch. This commit is in 0.65.0 here: c299694

Sorry for the confusion! 😅

@hakonk
Copy link
Contributor Author

hakonk commented Aug 18, 2021

Alright, thanks for answering so quickly! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Needs: React Native Team Attention Platform: iOS iOS applications. Type: Enhancement A new feature or enhancement of an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.