Skip to content

Personal AI, On Personal Devices

Python

fix: keep generated config locally safe (#891)

Elliot Slusky8ad8cb857bd9
diff --git a/configs/openjarvis/examples/chat-simple.toml b/configs/openjarvis/examples/chat-simple.toml
index 4b4c373e..fb9a0e42 100644
--- a/configs/openjarvis/examples/chat-simple.toml
+++ b/configs/openjarvis/examples/chat-simple.toml
@@ -20,5 +20,5 @@ default_model = "qwen3.5:4b" # Fast and lightweight
default_agent = "simple" # Single-turn, no tools
[server]
-host = "0.0.0.0"
+host = "127.0.0.1"
port = 8000
diff --git a/docs/getting-started/configuration.md b/docs/getting-started/configuration.md
index 13a9dfd8..2d580714 100644
--- a/docs/getting-started/configuration.md
+++ b/docs/getting-started/configuration.md
@@ -412,7 +412,7 @@ Controls the OpenAI-compatible API server started by `jarvis serve`.
```toml
[server]
-host = "0.0.0.0"
+host = "127.0.0.1"
port = 8000
agent = "orchestrator"
model = ""
@@ -421,7 +421,7 @@ workers = 1
| Field | Type | Default | Description |
|-------|------|---------|-------------|
-| `host` | string | `"0.0.0.0"` | Bind address for the server. Use `"127.0.0.1"` to restrict to localhost. |
+| `host` | string | `"127.0.0.1"` | Bind address for the server. Configure API authentication before using `"0.0.0.0"` for LAN access. |
| `port` | int | `8000` | Port number for the server. |
| `agent` | string | `"orchestrator"` | Agent to use for chat completion requests. |
| `model` | string | `""` | Default model for the server. When empty, uses `intelligence.default_model` or the first available model. |
diff --git a/src/openjarvis/core/config.py b/src/openjarvis/core/config.py
index 6159a25a..6363495e 100644
--- a/src/openjarvis/core/config.py
+++ b/src/openjarvis/core/config.py
@@ -1806,6 +1806,11 @@ def validate_config_key(dotted_key: str) -> type:
"""
from dataclasses import fields as dc_fields
+ def contains_dataclass(annotation: Any) -> bool:
+ if is_dataclass(annotation):
+ return True
+ return any(contains_dataclass(arg) for arg in get_args(annotation))
+
parts = dotted_key.split(".")
if len(parts) < 2:
raise ValueError(
@@ -1840,7 +1845,18 @@ def validate_config_key(dotted_key: str) -> type:
fld_type = eval(fld_type, vars(_cfg_mod)) # noqa: S307
if i == len(parts) - 1:
- # Leaf — return the primitive type
+ if contains_dataclass(fld_type):
+ suggestions = ""
+ if is_dataclass(fld_type):
+ child_keys = [
+ f"{dotted_key}.{child.name}" for child in dc_fields(fld_type)
+ ]
+ suggestions = f"; set one of: {', '.join(child_keys)}"
+ raise ValueError(
+ f"Config key {dotted_key!r} names a section, not a settable value"
+ f"{suggestions}"
+ )
+ # Leaf — return the primitive/container type
return fld_type
else:
# Must be a nested dataclass
@@ -2236,7 +2252,10 @@ enabled = true
# viewport_height = 720
[server]
-host = "0.0.0.0"
+# Loopback is safe for local use and works without API authentication.
+host = "127.0.0.1"
+# To serve other machines, configure an API key before using:
+# host = "0.0.0.0"
port = 8000
agent = "orchestrator"
diff --git a/tests/cli/test_config_set.py b/tests/cli/test_config_set.py
index 7fbe47d2..86453197 100644
--- a/tests/cli/test_config_set.py
+++ b/tests/cli/test_config_set.py
@@ -108,6 +108,24 @@ class TestConfigSet:
assert result.exit_code != 0
assert "Unknown config key" in result.output
+ def test_set_section_rejected_without_changing_config(self, tmp_path: Path) -> None:
+ config_file = tmp_path / "config.toml"
+ original = '[engine]\ndefault = "ollama"\n'
+ config_file.write_text(original, encoding="utf-8")
+
+ with mock.patch.dict(
+ os.environ,
+ {"OPENJARVIS_CONFIG": str(config_file)},
+ ):
+ result = CliRunner().invoke(
+ cli,
+ ["config", "set", "engine.ollama", "http://box:11434"],
+ )
+
+ assert result.exit_code != 0
+ assert "names a section" in result.output
+ assert config_file.read_text(encoding="utf-8") == original
+
def test_set_engine_host_probes_reachable(self, tmp_path: Path) -> None:
"""config set probes engine host and reports reachability."""
config_file = tmp_path / "config.toml"
diff --git a/tests/cli/test_preset_bind.py b/tests/cli/test_preset_bind.py
new file mode 100644
index 00000000..6015bfff
--- /dev/null
+++ b/tests/cli/test_preset_bind.py
@@ -0,0 +1,28 @@
+"""Preset installation must preserve safe local server defaults (#770)."""
+
+from pathlib import Path
+from unittest import mock
+
+from click.testing import CliRunner
+
+from openjarvis.cli import cli
+from openjarvis.core.config import load_config
+from openjarvis.server.auth_middleware import check_bind_safety
+
+
+def test_chat_simple_preset_can_serve_without_an_api_key(tmp_path: Path) -> None:
+ config_dir = tmp_path / ".openjarvis"
+ config_path = config_dir / "config.toml"
+ with (
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_DIR", config_dir),
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_PATH", config_path),
+ ):
+ result = CliRunner().invoke(
+ cli,
+ ["init", "--preset", "chat-simple", "--no-download", "--no-scan"],
+ )
+
+ assert result.exit_code == 0, result.output
+ config = load_config(config_path)
+ assert config.server.host == "127.0.0.1"
+ check_bind_safety(config.server.host, api_key="")
diff --git a/tests/core/test_config.py b/tests/core/test_config.py
index 8aa972ff..ae4941e1 100644
--- a/tests/core/test_config.py
+++ b/tests/core/test_config.py
@@ -4,6 +4,8 @@ from __future__ import annotations
from pathlib import Path
+import tomllib
+
from openjarvis.core.config import (
AgentConfig,
ChannelConfig,
@@ -140,6 +142,13 @@ class TestGenerateToml:
assert 'default = "vllm"' in toml
assert "H100" in toml
+ def test_default_server_bind_is_loopback(self) -> None:
+ toml = generate_default_toml(HardwareInfo())
+
+ assert 'host = "127.0.0.1"' in toml
+ assert '# host = "0.0.0.0"' in toml
+ assert tomllib.loads(toml)["server"]["host"] == "127.0.0.1"
+
class TestSecurityConfig:
def test_security_config_defaults(self) -> None:
diff --git a/tests/core/test_config_key_validation.py b/tests/core/test_config_key_validation.py
index badc706e..7449c834 100644
--- a/tests/core/test_config_key_validation.py
+++ b/tests/core/test_config_key_validation.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+from typing import get_args, get_origin
+
import pytest
from openjarvis.core.config import validate_config_key
@@ -52,3 +54,19 @@ class TestValidateConfigKey:
"""Hardware is auto-detected, not user-settable."""
with pytest.raises(ValueError, match="Unknown config key"):
validate_config_key("hardware.cpu_count")
+
+ def test_nested_dataclass_section_is_not_settable(self):
+ with pytest.raises(ValueError, match="names a section") as exc_info:
+ validate_config_key("engine.ollama")
+
+ assert "engine.ollama.host" in str(exc_info.value)
+
+ def test_list_of_dataclasses_is_not_settable(self):
+ with pytest.raises(ValueError, match="names a section"):
+ validate_config_key("skills.sources")
+
+ def test_plain_list_leaf_remains_settable(self):
+ field_type = validate_config_key("digest.sections")
+
+ assert get_origin(field_type) is list
+ assert get_args(field_type) == (str,)