DioxusLabs / DioxusLabs/dioxus

WASM client crash on keyboard event being undefined

Open
#4,486 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**

Fullstack web app client side crashes with a panicked at `already borrowed: BorrowMutError` when a user triggers browser autofill on a form.

I think the cause is that the browser autofill can dispatch synthetic keyboard events where modifier keys (altKey, ctrlKey, etc.) have a value of undefined. The Dioxus WASM bindings expect these properties to always be `booleans`. When the code attempts to access these properties (e.g., via `event.data.modifiers()`), the wasm-bindgen glue code fails with an `expected a boolean argument, found undefined` error.

**Steps To Reproduce**

Steps to reproduce the behavior:

- Create a Dioxus web application with a form containing `` fields
- Add a keyboard event handler (e.g., `onkeydown`) that accesses the event's modifier keys using `event.data.modifiers()`
- Use the browser's autofill feature to complete the form fields
- The application crashes with a `BorrowMutError` when autofill triggers synthetic keyboard events

Minimal reproduction example:
```
rsx! {
div {
// This handler will be triggered by autofill's synthetic events
onkeydown: move |event| {
// This line panics because autofill events have undefined modifier keys
let modifiers = event.data.modifiers();
println!("Modifiers: {:?}", modifiers);
},
input { name: "username", placeholder: "Username" }
input { name: "password", r#type: "password", placeholder: "Password" }
}
}
```

I know that many browsers all handle autofill in different ways, this error happened on microsoft edge (chromium based), although I think it would be a good thing to handle regardless.

**Expected behavior**

The application should handle synthetic keyboard events from browser autofill gracefully without crashing. When modifier key values are undefined, they should be safely coerced to false, allowing the event handler to execute without error.

**Environment:**

- Dioxus version: main
- Rust version: 1.88
- OS info: Windows (but I think this applies to any)
- App platform: web (fullstack)

**Questionnaire**

I would like to fix and I have a solution.

Add `#[serde(default)]` attributes to the modifier key fields in `SerializedKeyboardData` struct (`packages/html/src/events/keyboard.rs`):

```#[serde(default)]
alt_key: bool,
#[serde(default)]
ctrl_key: bool,
#[serde(default)]
meta_key: bool,
#[serde(default)]
shift_key: bool,```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.