Skip to content

Commit 33e61d5

Browse files
authored
Merge pull request #11 from CoolBitX-Technology/fix/show_alert_to_ssl_error
🐛 [CW-25785] fix: add SSL error handling with user confirmation in WebViewClient
2 parents bea71c4 + 9bd3e35 commit 33e61d5

File tree

3 files changed

+77
-68
lines changed

3 files changed

+77
-68
lines changed

README.md

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,65 @@
11
# React Native WebView
22

3-
![star this repo](https://img.shields.io/github/stars/react-native-webview/react-native-webview?style=flat-square)
4-
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
5-
[![NPM Version](https://img.shields.io/npm/v/react-native-webview.svg?style=flat-square)](https://www.npmjs.com/package/react-native-webview)
6-
![Npm Downloads](https://img.shields.io/npm/dm/react-native-webview.svg)
3+
A forked version of React Native WebView customized for CoolBitX Technology projects.
74

8-
**React Native WebView** is a community-maintained WebView component for React Native. It is intended to be a replacement for the built-in WebView (which was [removed from core](https://github.com/react-native-community/discussions-and-proposals/pull/3)).
5+
## 🚀 Getting Started
96

10-
### Maintainers
7+
### Clone and Install
118

12-
**Many thanks to these companies** for providing us with time to work on open source.
13-
Please note that maintainers spend a lot of free time working on this too so feel free to sponsor them, **it really makes a difference.**
9+
```bash
10+
# Clone the repository
11+
git clone https://github.com/CoolBitX-Technology/react-native-webview.git
1412

15-
- [Thibault Malbranche](https://github.com/Titozzz) ([Twitter @titozzz](https://twitter.com/titozzz)) from [Brigad](https://www.brigad.co/en-gb/about-us)
16-
[*Sponsor me* ❤️ !](https://github.com/sponsors/Titozzz)
13+
# Navigate to project directory
14+
cd react-native-webview
1715

16+
# Install dependencies
17+
yarn install
18+
```
1819

19-
Windows and macOS are managed by Microsoft, notably:
20-
- [Alexander Sklar](https://github.com/asklar) ([Twitter @alexsklar](https://twitter.com/alexsklar)) from [React Native for Windows](https://microsoft.github.io/react-native-windows/)
21-
- [Chiara Mooney](https://github.com/chiaramooney) from [React Native for Windows @ Microsoft](https://microsoft.github.io/react-native-windows/)
22-
23-
Shout-out to [Jamon Holmgren](https://github.com/jamonholmgren) from [Infinite Red](https://infinite.red) for helping a lot with the repo when he had more available time.
24-
25-
### Disclaimer
26-
27-
Maintaining WebView is very complex because it is often used for many different use cases (rendering SVGs, PDFs, login flows, and much more). We also support many platforms and both architectures of react-native.
28-
29-
Since WebView was extracted from the React Native core, nearly 500 pull requests have been merged.
30-
Considering that we have limited time, issues will mostly serve as a discussion place for the community, while **we will prioritize reviewing and merging pull requests.**
31-
32-
### Platform compatibility
33-
34-
This project is compatible with **iOS**, **Android**, **Windows** and **macOS**.
35-
This project supports both **the old** (paper) **and the new architecture** (fabric).
36-
This project is compatible with [expo](https://docs.expo.dev/versions/latest/sdk/webview/).
37-
38-
### Getting Started
39-
40-
Read our [Getting Started Guide](docs/Getting-Started.md). If any step seems unclear, please create a pull request.
20+
### Build for Android
4121

42-
### Versioning
22+
```bash
23+
# Run Android example app
24+
yarn run android
25+
```
4326

44-
This project follows [semantic versioning](https://semver.org/). We do not hesitate to release breaking changes but they will be in a major version.
27+
> **Note**: Once the build is complete, the example app will automatically open on your connected Android device or emulator.
4528
46-
### Usage
29+
## 📦 Publishing
4730

48-
Import the `WebView` component from `react-native-webview` and use it like so:
31+
### 1. Update Version
4932

50-
```tsx
51-
import React, { Component } from 'react';
52-
import { StyleSheet, Text, View } from 'react-native';
53-
import { WebView } from 'react-native-webview';
33+
Update the version in `package.json`:
5434

55-
// ...
56-
const MyWebComponent = () => {
57-
return <WebView source={{ uri: 'https://reactnative.dev/' }} style={{ flex: 1 }} />;
35+
```json
36+
{
37+
"version": "13.13.4-cbx.6"
5838
}
5939
```
6040

61-
For more, read the [API Reference](./docs/Reference.md) and [Guide](./docs/Guide.md). If you're interested in contributing, check out the [Contributing Guide](./docs/Contributing.md).
41+
**Example**: `13.13.4-cbx.5``13.13.4-cbx.6`
42+
43+
### 2. Publish to NPM
6244

63-
### Common issues
45+
```bash
46+
yarn run publish:npm
47+
```
6448

65-
- If you're getting `Invariant Violation: Native component for "RNCWebView does not exist"` it likely means you forgot to run `react-native link` or there was some error with the linking process
66-
- If you encounter a build error during the task `:app:mergeDexRelease`, you need to enable multidex support in `android/app/build.gradle` as discussed in [this issue](https://github.com/react-native-webview/react-native-webview/issues/1344#issuecomment-650544648)
49+
When prompted, enter the new version (e.g., `13.13.4-cbx.6`).
6750

68-
#### Contributing
51+
> **Note**: Two-factor authentication is required for NPM publishing.
6952
70-
Contributions are welcome, see [Contributing.md](https://github.com/react-native-webview/react-native-webview/blob/master/docs/Contributing.md)
53+
### 3. Verify Publication
7154

72-
### License
55+
After successful publication, verify the new version at:
56+
📍 [https://www.npmjs.com/package/@coolwallet-app/react-native-webview](https://www.npmjs.com/package/@coolwallet-app/react-native-webview)
7357

74-
MIT
58+
## 🔗 Links
7559

76-
### Translations
60+
- **NPM Package**: [@coolwallet-app/react-native-webview](https://www.npmjs.com/package/@coolwallet-app/react-native-webview)
61+
- **Original Repository**: [react-native-webview/react-native-webview](https://github.com/react-native-webview/react-native-webview)
7762

78-
This readme is available in:
63+
## 📄 License
7964

80-
- [Brazilian portuguese](docs/README.portuguese.md)
81-
- [French](docs/README.french.md)
82-
- [Italian](docs/README.italian.md)
65+
This project maintains the same license as the original react-native-webview project.

android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.webkit.WebResourceResponse;
1515
import android.webkit.WebView;
1616
import android.webkit.WebViewClient;
17+
import android.app.AlertDialog;
1718

1819
import androidx.annotation.Nullable;
1920
import androidx.annotation.RequiresApi;
@@ -38,6 +39,8 @@
3839
import java.util.Objects;
3940
import java.util.concurrent.atomic.AtomicReference;
4041

42+
import java.net.URL;
43+
4144
public class RNCWebViewClient extends WebViewClient {
4245
private static String TAG = "RNCWebViewClient";
4346
protected static final int SHOULD_OVERRIDE_URL_LOADING_TIMEOUT = 250;
@@ -181,12 +184,12 @@ public void onReceivedSslError(final WebView webView, final SslErrorHandler hand
181184
String topWindowUrl = webView.getUrl();
182185
String failingUrl = error.getUrl();
183186

184-
// Cancel request after obtaining top-level URL.
185-
// If request is cancelled before obtaining top-level URL, undesired behavior may occur.
186-
// Undesired behavior: Return value of WebView.getUrl() may be the current URL instead of the failing URL.
187-
handler.cancel();
188-
189187
if (!topWindowUrl.equalsIgnoreCase(failingUrl)) {
188+
// Cancel request after obtaining top-level URL.
189+
// If request is cancelled before obtaining top-level URL, undesired behavior may occur.
190+
// Undesired behavior: Return value of WebView.getUrl() may be the current URL instead of the failing URL.
191+
handler.cancel();
192+
190193
// If error is not due to top-level navigation, then do not call onReceivedError()
191194
Log.w(TAG, "Resource blocked from loading due to SSL error. Blocked URL: "+failingUrl);
192195
return;
@@ -223,12 +226,25 @@ public void onReceivedSslError(final WebView webView, final SslErrorHandler hand
223226

224227
description = descriptionPrefix + description;
225228

226-
this.onReceivedError(
227-
webView,
228-
code,
229-
description,
230-
failingUrl
231-
);
229+
// --- Show an AlertDialog to let the user choose whether to proceed ---
230+
final int finalCode = code;
231+
final String finalDescription = description;
232+
final String finalMessage = "Attackers may attempt to steal your information from " + getDomainFromUrl(failingUrl) + " (e.g., passwords, messages, or credit card details)."
233+
+ "\n\nThis could be due to a misconfiguration or an intercepted connection. Continuing is unsafe."
234+
+ "\n\n" + finalDescription;
235+
AlertDialog.Builder builder = new AlertDialog.Builder(webView.getContext());
236+
builder.setTitle("⚠️ Your connection is not private");
237+
builder.setMessage(finalMessage);
238+
builder.setCancelable(false);
239+
builder.setPositiveButton("Cancel", (dialog, which) -> {
240+
handler.cancel(); // Cancel the request
241+
// Only call onReceivedError here since this represents an actual load failure
242+
this.onReceivedError(webView, finalCode, finalDescription, failingUrl);
243+
});
244+
builder.setNegativeButton("Continue", (dialog, which) -> {
245+
handler.proceed(); // Proceed with loading
246+
});
247+
builder.show();
232248
}
233249

234250
@Override
@@ -338,4 +354,14 @@ protected WritableMap createWebViewEvent(WebView webView, String url) {
338354
public void setProgressChangedFilter(RNCWebView.ProgressChangedFilter filter) {
339355
progressChangedFilter = filter;
340356
}
357+
358+
private String getDomainFromUrl(String urlString) {
359+
try {
360+
URL url = new URL(urlString);
361+
return url.getHost(); // 取得 domain,例如 "example.com"
362+
} catch (Exception e) {
363+
e.printStackTrace();
364+
return urlString; // 如果解析失敗就回原 URL
365+
}
366+
}
341367
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"Thibault Malbranche <[email protected]>"
1111
],
1212
"license": "MIT",
13-
"version": "13.13.4-cbx.5",
13+
"version": "13.13.4-cbx.7",
1414
"publishConfig": {
1515
"access": "public",
1616
"registry": "https://registry.npmjs.org/"

0 commit comments

Comments
 (0)