tauri-apps/tauri · error

failed to serialize scope

Error message

failed to serialize scope

What it means

While building a runtime capability (CapabilityBuilder), each allowed-scope entry is serialized to a serde_json::Value. The expect fires only when Serialize fails: the value cannot be represented as JSON, e.g. a HashMap with non-string keys, or a custom Serialize impl that returns an error.

Source

Thrown at crates/tauri/src/ipc/capability_builder.rs:117

  /// Add a new scoped permission to this capability.
  pub fn permission_scoped<T: Serialize>(
    mut self,
    permission: impl Into<String>,
    allowed: Vec<T>,
    denied: Vec<T>,
  ) -> Self {
    let permission = permission.into();
    let identifier = permission
      .clone()
      .try_into()
      .unwrap_or_else(|_| panic!("invalid permission identifier '{permission}'"));

    let allowed_scope = allowed
      .into_iter()
      .map(|a| {
        serde_json::to_value(a)
          .expect("failed to serialize scope")
          .into()
      })
      .collect();
    let denied_scope = denied
      .into_iter()
      .map(|a| {
        serde_json::to_value(a)
          .expect("failed to serialize scope")
          .into()
      })
      .collect();
    let scope = Scopes {
      allow: Some(allowed_scope),
      deny: Some(denied_scope),
    };

    self
      .0

View on GitHub (pinned to 52e4b6e71d)

Solutions

  1. Use string-keyed maps, plain strings, or structs of strings/vecs for scope entries.
  2. Add a unit test asserting serde_json::to_value(&scope).is_ok() for custom scope types.

Example fix

// before
let scope: HashMap<u32, String> = ...; // non-string keys

// after
let scope: HashMap<String, String> = ...;
Defensive patterns

Strategy: validation

Validate before calling

#[test]
fn allowed_scope_serializes_to_json() {
    assert!(serde_json::to_value(&my_scope()).is_ok(), "scope type is not JSON-representable");
}

Prevention

When it happens

Trigger: Passing an allowed scope value that is a map with non-string keys (HashMap<u32, _>) or a type whose serde serialization can error. Plain strings and string-structs always serialize.

Common situations: Custom scope types using numeric-keyed maps; exotic hand-written Serialize impls; almost never triggered with normal string scopes.

Related errors


AI-assisted analysis of tauri-apps/tauri@52e4b6e71d (2026-08-20). Data as JSON: /api/errors/520cec1e7d19835a. Report an issue: GitHub.