Skip to content

fix(api): graceful 400 for non-table [memory]/[proactive_memory] in PATCH /api/memory/config#6301

Merged
houko merged 1 commit into
mainfrom
fix/memory-config-patch-panic
Jun 24, 2026
Merged

fix(api): graceful 400 for non-table [memory]/[proactive_memory] in PATCH /api/memory/config#6301
houko merged 1 commit into
mainfrom
fix/memory-config-patch-panic

Conversation

@houko

@houko houko commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary

memory_config_patch (PATCH /api/memory/config) read config.toml, parsed it as a toml::Value, then did:

let memory_tbl = root
    .entry("memory")
    .or_insert_with(|| toml::Value::Table(toml::map::Map::new()))
    .as_table_mut()
    .unwrap();                       // <- panics

and the same for proactive_memory. entry(..).or_insert_with only inserts the default table when the key is absent. If config.toml already holds a scalar at that key — e.g. an operator hand-edited memory = 5 or proactive_memory = "x"entry() returns the existing non-table value, as_table_mut() returns None, and the unwrap() panics.

axum isolates the panic to the request task (the daemon survives and returns 500), so the blast radius is one failed admin PATCH, and the trigger requires a pre-existing malformed config on disk (operator-controlled, not attacker-controlled) — hence low severity. But the operator gets an opaque 500 + a panic in the logs instead of a useful message.

The top-level table.as_table_mut().unwrap() (the document root) is left as-is: a valid TOML document root is always a table.

Fix

Both as_table_mut() sites now return a clear 400 Bad Request (config.toml has a non-table [memory] entry; expected a table) instead of panicking.

Tests

Adds two #[tokio::test] cases to memory_routes_integration.rs:

  • patch_memory_config_with_non_table_memory_entry_is_graceful — seeds memory = 5, asserts 400 + error body.
  • patch_memory_config_with_non_table_proactive_memory_entry_is_graceful — seeds proactive_memory = "oops", asserts 400 + error body.

Verification

This host has no native Rust toolchain and no working container runtime, so local cargo verification was not possible — CI is the gate. Reviewers / CI: cargo test -p librefang-api.

Scope

One of several independent findings from a repo audit; filed as its own PR per the one-PR-one-domain policy.

@github-actions github-actions Bot added size/M 50-249 lines changed area/docs Documentation and guides labels Jun 24, 2026
@houko
houko force-pushed the fix/memory-config-patch-panic branch 2 times, most recently from 77ec986 to 7b46ce7 Compare June 24, 2026 01:09
@houko
houko enabled auto-merge (squash) June 24, 2026 01:22
@github-actions github-actions Bot added the has-conflicts PR has merge conflicts that need resolution label Jun 24, 2026
@houko
houko force-pushed the fix/memory-config-patch-panic branch from 7b46ce7 to cb5d93d Compare June 24, 2026 01:26
@github-actions github-actions Bot added ready-for-review PR is ready for maintainer review has-conflicts PR has merge conflicts that need resolution and removed has-conflicts PR has merge conflicts that need resolution ready-for-review PR is ready for maintainer review labels Jun 24, 2026
…ATCH /api/memory/config

memory_config_patch did root.entry("memory").or_insert_with(..).as_table_mut().unwrap() (and the same for proactive_memory).
entry() only inserts the default table when the key is absent, so a pre-existing scalar at that key (e.g. an operator hand-edited `memory = 5`) made as_table_mut() return None and the unwrap panicked into a 500.

Both sites now return a clear 400 instead of panicking. Adds integration tests seeding a non-table [memory] and a non-table [proactive_memory] entry.
@houko
houko force-pushed the fix/memory-config-patch-panic branch from cb5d93d to 5c5b25b Compare June 24, 2026 03:16
@github-actions github-actions Bot added ready-for-review PR is ready for maintainer review and removed has-conflicts PR has merge conflicts that need resolution labels Jun 24, 2026
@houko
houko merged commit cec6bea into main Jun 24, 2026
31 checks passed
@houko
houko deleted the fix/memory-config-patch-panic branch June 24, 2026 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs Documentation and guides ready-for-review PR is ready for maintainer review size/M 50-249 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant