Skip to content

Commit 715e0c3

Browse files
x93008lixingxin
authored andcommitted
fix(macOS 11): prevent UAF crash in WKURLSchemeHandler stop_task
macOS 11 WebKit bug: during WKWebView dealloc, stopAllTasksForPage calls stop_task with already-freed task pointers. Any access (including the implicit objc_release from objc2 reference types) causes SIGSEGV. Fix: - stop_task: use raw pointers (*mut AnyObject) instead of objc2 references to skip automatic retain/release. Body is no-op since task is invalid. - start_task response handler: explicit drop(webview) before drop(task) to ensure correct deallocation order.
1 parent 5bdda32 commit 715e0c3

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/wkwebview/class/url_scheme_handler.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,18 @@ extern "C" fn start_task(
286286
}))
287287
.map_err(|_e| crate::Error::CustomProtocolTaskInvalid)?;
288288

289-
if WEBVIEW_STATE.read().unwrap().contains_key(webview_id) {
289+
let result = if WEBVIEW_STATE.read().unwrap().contains_key(webview_id) {
290290
webview.remove_custom_task_key(task_key);
291291
Ok(())
292292
} else {
293293
Err(crate::Error::CustomProtocolTaskInvalid)
294-
}
294+
};
295+
296+
// webview must drop before task: if webview drop triggers dealloc →
297+
// stopAllTasksForPage → platformStopTask, the task must still be alive.
298+
drop(webview);
299+
drop(task);
300+
result
295301
}
296302

297303
#[cfg(feature = "tracing")]
@@ -334,8 +340,8 @@ extern "C" fn start_task(
334340
extern "C" fn stop_task(
335341
_this: &ProtocolObject<dyn WKURLSchemeHandler>,
336342
_sel: objc2::runtime::Sel,
337-
webview: &WryWebView,
338-
task: &ProtocolObject<dyn WKURLSchemeTask>,
343+
_webview: *mut AnyObject,
344+
_task: *mut AnyObject,
339345
) {
340-
webview.remove_custom_task_key(task.hash());
346+
// no-op: avoid accessing task/webview — macOS 11 may pass freed pointers here
341347
}

0 commit comments

Comments
 (0)