Skip to content

TLS config crashes in mqtt_api.py and evcc_api.py - tls field used as both bool and dict #309

@MaStr

Description

@MaStr

Both mqtt_api.py and evcc_api.py have identical TLS configuration bugs where config['tls'] is checked as a boolean but then accessed as a dictionary.
Location

src/batcontrol/mqtt_api.py lines 92-100
src/batcontrol/evcc_api.py lines 115-123

The Problem

TLS , not tested yet

if config['tls'] is True:
self.client.tls_set(
config['tls']['ca_certs'], # crashes: True['ca_certs']
config['tls']['certfile'],
config['tls']['keyfile'],
cert_reqs=config['tls']['cert_reqs'],
tls_version=config['tls']['tls_version'],
ciphers=config['tls']['ciphers']
)

The code checks config['tls'] is True (a boolean), then immediately tries to subscript it as a dict (config['tls']['ca_certs']). This will always crash with TypeError: 'bool' object is not subscriptable when TLS is enabled.
Expected Behavior

The TLS fields are defined as sibling keys in the config, not nested under tls:

mqtt:
tls: false
cafile: /etc/ssl/certs/ca-certificates.crt
certfile: /etc/ssl/certs/client.crt
keyfile: /etc/ssl/certs/client.key
tls_version: tlsv1.2

How to Reproduce

Set tls: true in the mqtt or evcc config section. Batcontrol crashes on startup.
Suggested Fix

if config['tls'] is True:
self.client.tls_set(
config.get('cafile'),
config.get('certfile'),
config.get('keyfile'),
tls_version=config.get('tls_version'),
)

Found during Pydantic config model audit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions