Repository navigation
Conversation
0948886 to
4538fa6
Compare
| api(libs.yoga.proguard.annotations) | ||
|
|
||
| api(libs.jsr305) | ||
| api(libs.okhttp3.urlconnection) |
There was a problem hiding this comment.
I believe this can be made non-breaking as apps can declare in their .gradle files that they don't intend to consume this api dependency from react-native.
There was a problem hiding this comment.
ReactAndroid now keeps okhttp-urlconnection as an api dependency, so the change is no longer breaking, and the [BREAKING] changelog entry is gone.
An app on OkHttp 5 needs to exclude it only if its code or a keep rule retains JavaNetCookieJar.
There was a problem hiding this comment.
An app on OkHttp 5 needs to exclude it only if its code or a keep rule retains
JavaNetCookieJar.
We probably need a page on the website explaining how to do this
There was a problem hiding this comment.
Added a section to the Networking page: react/react-native-website#5363
f378dfa to
13c1611
Compare
3e99c93 to
ca8d6fc
Compare
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D135940203. |
Why === react/react-native#60385 lets Android apps use OkHttp 5, starting with React Native 0.89. The Networking page doesn't say how to opt in, or what to do about the OkHttp 4 version of `okhttp-urlconnection` that `react-android` brings in. How === Adds a "Using OkHttp 5 on Android" section to the Networking page, before "Configuring NSURLSession on iOS". It shows how to add OkHttp 5, when to also add `okhttp-urlconnection` at the same version, and how to exclude the OkHttp 4 `okhttp-urlconnection` from `react-android`. Test Plan ========= `npx prettier --check docs/network.md` and `npx eslint docs/network.md` pass. Signed-off-by: James Ide <ide@expo.dev>
An app that depends on OkHttp 5 crashes when React Native loads cookies (react#52945). Gradle resolves okhttp to the app's 5.x, but okhttp-urlconnection stays at the 4.9.2 that ReactAndroid declares, and its JavaNetCookieJar calls okhttp3.internal.Util, which OkHttp 5 removed. No okhttp-urlconnection release works with OkHttp 3, 4 and 5, because each calls internals of its own major version. Add CookieHandlerCookieJar, a port of OkHttp 5's JavaNetCookieJar onto API that OkHttp 3, 4 and 5 share, and use it in NetworkingModule and FrescoModule. React Native no longer uses okhttp-urlconnection. ReactAndroid still declares it as an api dependency, so code that uses JavaNetCookieJar or JavaNetAuthenticator through React Native keeps compiling, and the tests use it to compare the port with JavaNetCookieJar from the OkHttp version under test. An app on OkHttp 5 needs no change unless its code or a keep rule retains JavaNetCookieJar: R8's shrinking removes the unused 4.9.2 classes, so a minified build doesn't fail on the OkHttp 4 internals they call. An app that uses JavaNetCookieJar on OkHttp 5 declares okhttp-urlconnection at its OkHttp version, and an app that wants the dependency gone can exclude it from react-android and any other dependency that brings it in. Like OkHttp 5.5.1's, the port accepts a value that is a lone quote and a value with whitespace just inside its quotes; earlier versions throw on both. Unlike JavaNetCookieJar, it logs and skips a cookie that Cookie.Builder rejects, such as one whose name starts with a vertical tab. Uncaught, that exception fails the request, and OkHttp rethrows it on its dispatcher thread, which crashes the app. Two other things differ on OkHttp 5: - Response.body() is non-null, so seven null checks that OkHttp 3 and 4 need become warnings, which -PenableWarningsAsErrors=true makes errors. Each check now reads the body into an explicitly nullable value first. - MultipartBody reports itself as one-shot when any part is, so getRequestBodyPreview showed a form upload with a file as only "[Binary data, N bytes]" in DevTools. It now checks the parts first. Run the OkHttp compatibility job as a matrix over OkHttp 3.14.9 and 5.5.1, with warnings as errors. The init script takes the version as -PokhttpCompatVersion and logs the Okio version it resolves. Add the OkHttp 5 rules to AGENTS.md. Signed-off-by: James Ide <ide@expo.dev>
ca8d6fc to
f79e78a
Compare
|
Some AI feedback :) Suggested follow-up for React Native PR #60385Keep the existing OkHttp 5 compatibility approach and keep Cookie validation
In For example: private fun isValidHeaderCharacter(c: Char): Boolean = c == '\t' || c in ' '..'~'require(name.all(::isValidHeaderCharacter) && value.all(::isValidHeaderCharacter)) {
"Invalid request header characters"
}Use the existing Regression coverageExtend
Validation to retainThe rejection regression failed against the original decoder and passed after the correction. With the logging regression included, the cookie and multipart-preview suites passed together: 31 tests, zero failures, using OkHttp 4.12.0. The public Android API snapshot is unchanged. Run these from the GitHub repository, preserving the existing warnings-as-errors checks: ./gradlew --init-script .github/workflow-scripts/okhttp-compat.gradle \
-PokhttpCompatVersion=3.14.9 -PenableWarningsAsErrors=true \
:packages:react-native:ReactAndroid:verifyOkHttpCompatibility
./gradlew --init-script .github/workflow-scripts/okhttp-compat.gradle \
-PokhttpCompatVersion=5.5.1 -PenableWarningsAsErrors=true \
:packages:react-native:ReactAndroid:verifyOkHttpCompatibility
./gradlew -PenableWarningsAsErrors=true \
:packages:react-native:ReactAndroid:testDebugUnitTest
yarn formatThose three Gradle configurations have not been rerun for this follow-up. The compatibility matrix verifies source compatibility by recompiling; it does not prove that a catalog-built AAR runs with OkHttp 5 and Okio 3. That mixed-version runtime smoke test also remains to be run for this follow-up. No public API snapshot update is needed because the adapter and helper remain internal. |
Validate decoded cookie names and values against OkHttp's request-header character rules (tab and printable ASCII) before building the Cookie, use url.redact() in all warning logs, and omit cookie details from rejected-cookie warnings. Signed-off-by: James Ide <ide@expo.dev>
|
Thanks, applied in 79eefcb:
Verified with
|
Summary:
This makes OkHttp 5 usable in React Native apps on Android. Today an app that depends on OkHttp 5 crashes when React Native loads cookies (#52945):
okhttp-urlconnectionstays at 4.9.2, and itsJavaNetCookieJarcalls an OkHttp internal that 5 removed.With this change, an app opts in by depending on OkHttp 5:
Apps whose dependencies don't select OkHttp 5 keep OkHttp 4.9.2, and React Native stays source-compatible with OkHttp 3.14.9.
CookieHandlerCookieJarreplacesJavaNetCookieJarand uses only API that OkHttp 3, 4 and 5 share. React Native no longer usesokhttp-urlconnection, but ReactAndroid still declares it, so code that usesJavaNetCookieJarorJavaNetAuthenticatorthrough React Native keeps compiling. An app on OkHttp 5 needs no change unless its code or a keep rule retainsJavaNetCookieJar: R8's shrinking removes the unused 4.9.2 classes, so a minified build doesn't fail on the OkHttp 4 internals they call. An app that usesJavaNetCookieJaron OkHttp 5 declaresokhttp-urlconnectionat its OkHttp version. An app that wants the dependency gone can exclude it from itsreact-androiddependency, and from any other dependency that brings it in:DevTools previews multipart uploads part by part on OkHttp 5 too, whose
MultipartBodyreports itself as one-shot when any part is.Response.body()null checks no longer warn on OkHttp 5, where the body is non-null.The
test_okhttpCI job intest-android.yml, from Add OkHttp 3 compatibility CI for ReactAndroid #60342, runs against OkHttp 3.14.9 and 5.5.1.Changelog:
[ANDROID] [FIXED] - Fix a crash loading cookies in apps that depend on OkHttp 5
Test Plan:
./gradlew --init-script .github/workflow-scripts/okhttp-compat.gradle -PokhttpCompatVersion=5.5.1 -PenableWarningsAsErrors=true :packages:react-native:ReactAndroid:verifyOkHttpCompatibilitypasses, and so does the same command with3.14.9../gradlew -PenableWarningsAsErrors=true :packages:react-native:ReactAndroid:testDebugUnitTestpasses on the catalog's 4.9.2.CookieHandlerCookieJarTestchecks the port againstJavaNetCookieJarfrom the OkHttp version under test.okhttp5.5.1 andokhttp-urlconnection4.9.2,JavaNetCookieJar.loadForRequestthrowsNoClassDefFoundError: okhttp3/internal/Util, and the port works.okhttp-android5.5.1 and Okio 3.18.1 withokhttp-urlconnection4.9.2 among its inputs. When the app keepsJavaNetCookieJar, R8 fails withMissing class okhttp3.internal.Util.implementation("com.squareup.okhttp3:okhttp:5.5.1"). ReactAndroid is still compiled against OkHttp 4.9.2, like the published AAR, and the app runs withokhttp-android5.5.1, Okio 3.18.1 andokhttp-urlconnection4.9.2. A test script checks that cookies are saved and sent back, and tries JSON and multipart requests, upload progress, Fresco image loading and a WebSocket. Cookies with non-ASCII characters inCookieManagerare skipped, and the rest are still sent. Without this change, the same app crashes on its first request withNoClassDefFoundError: okhttp3/internal/UtilfromJavaNetCookieJar.loadForRequest.