Hmbown/CodeWhale · error · anyhow::Error

failed to parse config TOML while removing plaintext API key

Error message

failed to parse config TOML while removing plaintext API keys; file contents were omitted

What it means

Before writing a credential-free config backup, Codewhale re-parses the raw config with toml_edit to recursively strip plaintext api_key entries. If the file is not valid TOML, redaction is refused with this message; contents are omitted so keys never reach logs.

Source

Thrown at crates/config/src/lib.rs:5298

            "failed to scrub plaintext API keys while creating config backup {}",
            backup.display()
        )
    })?;
    persistence::atomic_write(&backup, scrubbed.as_bytes()).with_context(|| {
        format!(
            "failed to create credential-free config backup {} from {}",
            backup.display(),
            path.display()
        )
    })?;
    Ok(())
}

fn config_toml_without_plaintext_api_keys(raw: &str) -> Result<String> {
    let mut document = raw
        .parse::<toml_edit::DocumentMut>()
        .map_err(|_| {
            anyhow::anyhow!(
                "failed to parse config TOML while removing plaintext API keys; file contents were omitted"
            )
        })?;
    remove_plaintext_api_keys_recursive(document.as_table_mut());
    Ok(document.to_string())
}

fn remove_plaintext_api_keys_recursive(table: &mut dyn toml_edit::TableLike) {
    table.remove("api_key");
    for (_, item) in table.iter_mut() {
        if let toml_edit::Item::ArrayOfTables(tables) = item {
            for nested in tables.iter_mut() {
                remove_plaintext_api_keys_recursive(nested);
            }
        } else if let Some(nested) = item.as_table_like_mut() {
            remove_plaintext_api_keys_recursive(nested);
        }
    }

View on GitHub (pinned to 8880682c63)

Solutions

  1. Fix the TOML syntax first (validator or TOML-aware editor), then re-trigger the operation
  2. If you need the rewrite regardless, temporarily remove the plaintext api_key lines yourself and retry
  3. Restore from the pre-edit backup if one exists
Defensive patterns

Strategy: validation

Validate before calling

// Before triggering the credential-free backup, prove the raw config parses:
fn raw_config_parses(raw: &str) -> bool {
    raw.parse::<toml_edit::DocumentMut>().is_ok()
}

Try / catch

On Err, keep the original file untouched (the code already refuses to write) and direct the user to fix the TOML; redaction cannot be forced, so the catch flow is repair-then-retry once.

Prevention

When it happens

Trigger: The backup/redaction flow (config_toml_without_plaintext_api_keys) runs when plaintext API keys exist in the config and the file has a TOML syntax error or duplicate keys, so a safe sanitized copy cannot be produced.

Common situations: A user hand-edited the config and broke syntax while an api_key was present; an interrupted write truncated the file; the redaction then blocks until the file parses.

Understand the failure class

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/8bd88fed36c826b3. Report an issue: GitHub.