MEDIUM: Add input validation and allowlist to /config/set API endpoint #43

Open
opened 2026-02-20 14:52:17 +02:00 by Koko210 · 0 comments
Owner

Problem

The /config/set API endpoint in bot/api.py accepts arbitrary key paths and values without any validation or restrictions. A caller can overwrite ANY configuration key with ANY value, including:

  • Overwriting security-sensitive settings (tokens, secrets)
  • Setting invalid types (string where int expected, etc.)
  • Creating non-existent keys that pollute config_runtime.yaml
  • Potentially breaking the bot by setting invalid service URLs, model names, etc.

Since the dashboard is accessible on the local network (port 3939), any device on the network can modify arbitrary configuration.

Current Behavior

POST /config/set
{'key': 'any.arbitrary.path', 'value': 'anything'}
-> Always succeeds, no validation

Proposed Solution

1. Allowlist of modifiable keys

Define which configuration keys are safe to modify at runtime:

MODIFIABLE_KEYS = {
    'autonomous.enabled': bool,
    'autonomous.interval_minutes': int,
    'voice.enabled': bool,
    'mood.current': str,
    'mood.rotation_enabled': bool,
    'services.prefer_amd_gpu': bool,
    # ... explicitly list all allowed keys
}

@app.post('/config/set')
async def set_config(key: str, value: Any):
    if key not in MODIFIABLE_KEYS:
        raise HTTPException(400, f'Key {key} is not modifiable at runtime')
    expected_type = MODIFIABLE_KEYS[key]
    if not isinstance(value, expected_type):
        raise HTTPException(400, f'Expected {expected_type.__name__}, got {type(value).__name__}')
    # proceed with set

2. Type validation

Validate that the provided value matches the expected type for that key (leveraging Pydantic models if the config unification issue is completed first).

3. Value range validation

For numeric values, add min/max constraints:

  • interval_minutes: 1-1440
  • temperature: 0.0-2.0
  • etc.

Impact

  • Risk: Very Low (adds restrictions, doesn't remove functionality)
  • Effort: Low-Medium (define the allowlist, add validation logic)
  • Benefit: Prevents accidental or malicious misconfiguration via the API

Files Affected

  • bot/api.py (or bot/api/config_router.py if split is done first)
  • bot/config_manager.py (add validation layer)
## Problem The /config/set API endpoint in bot/api.py accepts arbitrary key paths and values without any validation or restrictions. A caller can overwrite ANY configuration key with ANY value, including: - Overwriting security-sensitive settings (tokens, secrets) - Setting invalid types (string where int expected, etc.) - Creating non-existent keys that pollute config_runtime.yaml - Potentially breaking the bot by setting invalid service URLs, model names, etc. Since the dashboard is accessible on the local network (port 3939), any device on the network can modify arbitrary configuration. ## Current Behavior POST /config/set {'key': 'any.arbitrary.path', 'value': 'anything'} -> Always succeeds, no validation ## Proposed Solution ### 1. Allowlist of modifiable keys Define which configuration keys are safe to modify at runtime: MODIFIABLE_KEYS = { 'autonomous.enabled': bool, 'autonomous.interval_minutes': int, 'voice.enabled': bool, 'mood.current': str, 'mood.rotation_enabled': bool, 'services.prefer_amd_gpu': bool, # ... explicitly list all allowed keys } @app.post('/config/set') async def set_config(key: str, value: Any): if key not in MODIFIABLE_KEYS: raise HTTPException(400, f'Key {key} is not modifiable at runtime') expected_type = MODIFIABLE_KEYS[key] if not isinstance(value, expected_type): raise HTTPException(400, f'Expected {expected_type.__name__}, got {type(value).__name__}') # proceed with set ### 2. Type validation Validate that the provided value matches the expected type for that key (leveraging Pydantic models if the config unification issue is completed first). ### 3. Value range validation For numeric values, add min/max constraints: - interval_minutes: 1-1440 - temperature: 0.0-2.0 - etc. ## Impact - Risk: Very Low (adds restrictions, doesn't remove functionality) - Effort: Low-Medium (define the allowlist, add validation logic) - Benefit: Prevents accidental or malicious misconfiguration via the API ## Files Affected - bot/api.py (or bot/api/config_router.py if split is done first) - bot/config_manager.py (add validation layer)
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Koko210/miku-discord#43