Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changes/fix-macos11-stop-task-uaf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

On macOS 11, fix use-after-free crash in custom protocol `stop_task` during WKWebView dealloc by using raw pointers instead of objc2 references and enforcing correct drop ordering in the async response handler.
16 changes: 11 additions & 5 deletions src/wkwebview/class/url_scheme_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,18 @@ extern "C" fn start_task(
}))
.map_err(|_e| crate::Error::CustomProtocolTaskInvalid)?;

if WEBVIEW_STATE.read().unwrap().contains_key(webview_id) {
let result = if WEBVIEW_STATE.read().unwrap().contains_key(webview_id) {
webview.remove_custom_task_key(task_key);
Ok(())
} else {
Err(crate::Error::CustomProtocolTaskInvalid)
}
};

// webview must drop before task: if webview drop triggers dealloc →
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this alone fix the problem?

Copy link
Copy Markdown
Contributor

@sftse sftse May 27, 2026

Choose a reason for hiding this comment

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

Is this change still necessary once we figure out what to do with stop_task?

I'm reading the explanation as, dropping webview is necessary because we call stop_task on the freed task, which the other hunk is supposed to address.

// stopAllTasksForPage → platformStopTask, the task must still be alive.
drop(webview);
drop(task);
result
}

#[cfg(feature = "tracing")]
Expand Down Expand Up @@ -334,8 +340,8 @@ extern "C" fn start_task(
extern "C" fn stop_task(
_this: &ProtocolObject<dyn WKURLSchemeHandler>,
_sel: objc2::runtime::Sel,
webview: &WryWebView,
task: &ProtocolObject<dyn WKURLSchemeTask>,
_webview: *mut AnyObject,
_task: *mut AnyObject,
) {
webview.remove_custom_task_key(task.hash());
// no-op: avoid accessing task/webview — macOS 11 may pass freed pointers here
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we can simply removing this?

}
Loading