Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(settings): reject control characters in keys (#7062)

LearningCircuit03efa77ff7bb
diff --git a/changelog.d/+settings-key-controls.bugfix.md b/changelog.d/+settings-key-controls.bugfix.md
new file mode 100644
index 0000000000..eae4f8296a
--- /dev/null
+++ b/changelog.d/+settings-key-controls.bugfix.md
@@ -0,0 +1 @@
+Settings writes now reject NUL and other ASCII control characters in keys before storing them, preserving valid printable and Unicode names.
diff --git a/src/local_deep_research/settings/manager.py b/src/local_deep_research/settings/manager.py
index 04da74d7a8..03f501494c 100644
--- a/src/local_deep_research/settings/manager.py
+++ b/src/local_deep_research/settings/manager.py
@@ -408,7 +408,8 @@ def is_valid_setting_key(key: Any) -> bool:
"""Return True if *key* is a well-formed setting key.
A valid key is a non-empty string of dot-separated segments where each
- segment is non-empty and contains no whitespace. This rejects the
+ segment is non-empty and contains no whitespace or ASCII control characters.
+ This rejects the
malformed keys that corrupt prefix/namespace lookups (see #4840) — a
trailing dot ``"foo."``, a leading dot ``".foo"``, an empty segment
``"foo..bar"``, a blank key — as well as keys carrying whitespace
@@ -426,7 +427,9 @@ def is_valid_setting_key(key: Any) -> bool:
if not isinstance(key, str) or not key:
return False
return all(
- seg and not any(c.isspace() for c in seg) for seg in key.split(".")
+ seg
+ and not any(c.isspace() or ord(c) < 0x20 or ord(c) == 0x7F for c in seg)
+ for seg in key.split(".")
)
diff --git a/tests/security/test_hostile_input_matrix.py b/tests/security/test_hostile_input_matrix.py
index 8afaf15e3e..e2a707b078 100644
--- a/tests/security/test_hostile_input_matrix.py
+++ b/tests/security/test_hostile_input_matrix.py
@@ -650,26 +650,6 @@ def test_settings_uncoercible_number_is_rejected_not_silently_nulled(client):
assert resp.status_code == 400, f"{resp.status_code} {resp.text[:200]}"
- strict=True,
- reason=(
- "DEFECT (pre-existing on origin/main — shared helper "
- "settings/manager.py::is_valid_setting_key). The validator rejects "
- "whitespace in a key segment BECAUSE (its own docstring) "
- "whitespace 'would break the key.split('.') -> LDR_... env-var "
- "mapping'. It tests `c.isspace()`, which is False for every C0 "
- "control character except \\t\\n\\v\\f\\r -- so NUL (\\x00) and BEL "
- "(\\x07) sail through. PUT /settings/api/llm.model%00x therefore "
- "returns 201 and persists a row whose key can never be reached by "
- "an LDR_* environment override (os.environ raises ValueError on "
- "embedded NUL), i.e. the exact corruption class the validator was "
- "written to prevent, plus a settings row the operator cannot lock. "
- "FIX: replace the `c.isspace()` predicate with a positive "
- "allow-list, e.g. `seg.isascii() and all(c.isalnum() or c in '_-' "
- "for c in seg)`, or at minimum reject any `c` with `ord(c) < 0x20 "
- "or ord(c) == 0x7f` alongside the existing isspace() check."
- ),
-)
def test_settings_key_control_characters_are_rejected(client):
# CONTROL 1 (validator unit level): the characters the validator DOES
# know about are refused, so the predicate is live.
@@ -688,10 +668,26 @@ def test_settings_key_control_characters_are_rejected(client):
assert resp.status_code == 400, f"{key!r} -> {resp.status_code}"
assert "malformed" in resp.json()["error"], f"{key!r} -> {resp.text}"
- # The defect, at the route: a NUL-bearing key is CREATED.
- resp = client.put("/settings/api/llm.model%00x", json={"value": "x"})
- assert resp.status_code == 400, (
- f"NUL key -> {resp.status_code} {resp.text[:200]}"
+ # Every ASCII control character is refused at the shared validator.
+ for codepoint in [*range(0x20), 0x7F]:
+ assert not is_valid_setting_key(f"llm.model{chr(codepoint)}x")
+
+ for encoded in ("%00", "%07", "%1b", "%7f"):
+ key = f"llm.model{encoded}x"
+ resp = client.put(f"/settings/api/{key}", json={"value": "x"})
+ assert resp.status_code == 400, f"{encoded} -> {resp.text[:200]}"
+ assert "malformed" in resp.json()["error"]
+ # Rejection happens before creating a setting row.
+ assert client.get(f"/settings/api/{key}").status_code == 404
+
+ # The HTTP guard accepts an ordinary new key and preserves its value.
+ resp = client.put(
+ "/settings/api/llm.release_review_key_2", json={"value": "retained"}
+ )
+ assert resp.status_code == 201, resp.text[:200]
+ assert (
+ client.get("/settings/api/llm.release_review_key_2").json()["value"]
+ == "retained"
)
@@ -1088,3 +1084,11 @@ def test_news_subscribe_type_confusion_is_rejected_not_500(client):
assert resp.status_code == 400, (
f"{field}={value!r} -> {resp.status_code} {resp.text[:220]}"
)
+
+
+ "key",
+ ["llm.model", "custom.key-with_dash_2", "custom.模型", "custom.modèle"],
+)
+def test_setting_keys_preserve_printable_names(key):
+ assert is_valid_setting_key(key)