Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class RNCWebView extends WebView implements LifecycleEventListener {
protected boolean nestedScrollEnabled = false;
protected ProgressChangedFilter progressChangedFilter;
protected boolean mActive = true;
protected @Nullable String mLastCommittedUrl;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Protect mLastCommittedUrl with cross-thread visibility guarantees.

mLastCommittedUrl is shared mutable state across WebView callback flow and @JavascriptInterface bridge flow. As currently implemented, stale reads are possible; this can still produce incorrect topFrameUrl under timing races.

Proposed fix
-    protected `@Nullable` String mLastCommittedUrl;
+    protected volatile `@Nullable` String mLastCommittedUrl;
According to Android WebView documentation, are methods annotated with `@JavascriptInterface` invoked on the UI thread or on a background thread, and what synchronization/visibility guarantees are required when sharing fields with UI-thread callbacks?

Also applies to: 260-266, 497-502

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java` at
line 82, mLastCommittedUrl is shared between WebView callbacks and
`@JavascriptInterface` bridge code and needs cross-thread visibility; make
mLastCommittedUrl volatile (and apply the same volatile protection to the other
shared mutable fields noted at lines 260-266 and 497-502) so updates on the UI
thread are immediately visible to background/bridge threads, and ensure any
compound read-modify-write sequences use a synchronized block or explicit lock
to preserve atomicity where used (search for mLastCommittedUrl and the other
listed fields to locate usages).


/**
* WebView must be created with an context of the current activity
Expand Down Expand Up @@ -256,6 +257,14 @@ public boolean getMessagingEnabled() {
return this.messagingEnabled;
}

public void setLastCommittedUrl(@Nullable String url) {
this.mLastCommittedUrl = url;
}

public @Nullable String getLastCommittedUrl() {
return this.mLastCommittedUrl;
}

@SuppressLint("RestrictedApi")
protected void createRNCWebViewBridge(RNCWebView webView) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)){
Expand Down Expand Up @@ -485,8 +494,11 @@ protected class RNCWebViewBridge {
@JavascriptInterface
public void postMessage(String message) {
if (mWebView.getMessagingEnabled()) {
// Capture top frame URL eagerly to avoid race condition if a navigation
// occurs between now and when the runnable executes on the main thread.
String topFrameUrl = mWebView.getLastCommittedUrl();
// Post to main thread because `mWebView.getUrl()` requires to be executed on main.
mWebView.post(() -> mWebView.onMessage(message, mWebView.getUrl(), null, mWebView.getUrl()));
mWebView.post(() -> mWebView.onMessage(message, mWebView.getUrl(), null, topFrameUrl));
} else {
FLog.w(TAG, "ReactNativeWebView.postMessage method was called but messaging is disabled. Pass an onMessage handler to the WebView.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void onPageStarted(WebView webView, String url, Bitmap favicon) {
mLastLoadFailed = false;

RNCWebView reactWebView = (RNCWebView) webView;
reactWebView.setLastCommittedUrl(url);
reactWebView.callInjectedJavaScriptBeforeContentLoaded();
}

Expand Down
Loading