Repository navigation
Security hardening: sanitize MQTT topics, fix bounds check, reduce lo… - #14
Conversation
…g exposure
- Strip MQTT wildcard characters (#, +, /) from user-provided topic
segments via new SanitizeSegment method, preventing topic traversal
from subdevice nicknames sourced from gateway API
- Add bounds check on topic.Split('/') before array indexing in
MQTT command handler to prevent IndexOutOfRangeException
- Remove full message payloads from Warning-level log lines on
publish failure (topic-only logging now)
- Delete stale Ecowitt.Controller/Dockerfile targeting .NET 8 / Debug
- Remove unused FluentValidation, Swashbuckle package references
There was a problem hiding this comment.
Pull request overview
Security hardening for MQTT topic handling and logging in the controller’s MQTT integration, plus some cleanup of build artifacts and unused dependencies.
Changes:
- Introduces segment-level MQTT topic sanitization to prevent wildcard/topic traversal injection via gateway/subdevice names.
- Adds a defensive bounds check when parsing Home Assistant command topics to avoid
IndexOutOfRangeException. - Reduces log exposure by removing full payloads from Warning-level publish-failure logs; removes unused package references and deletes a stale Dockerfile.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/Ecowitt.Controller/Service/Mqtt/MqttService.Publisher.cs |
Removes message payload from Warning logs on publish failures. |
src/Ecowitt.Controller/Service/Mqtt/MqttService.Events.cs |
Adds Split('/') bounds check before indexing when handling HA command topics. |
src/Ecowitt.Controller/Service/Mqtt/MqttService.DiscoveryPublisher.cs |
Switches discovery topic building to sanitize only user-provided segments. |
src/Ecowitt.Controller/Service/Mqtt/MqttService.Consumer.cs |
Aligns discovery removal topic generation with new segment sanitization. |
src/Ecowitt.Controller/Service/Mqtt/MqttPathBuilder.cs |
Adds SanitizeSegment and updates topic builders to sanitize each segment. |
src/Ecowitt.Controller/Ecowitt.Controller.csproj |
Removes unused FluentValidation/Swashbuckle-related package references. |
src/Ecowitt.Controller/Dockerfile |
Deletes stale .NET 8 Debug Dockerfile. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
…d tests - Parse subdevice ID relative to the "subdevices" segment instead of a fixed index, so multi-segment base topics work correctly - Remove unused Sanitize method (all callers migrated to SanitizeSegment) - Replace old Sanitize tests with SanitizeSegment tests covering slash, hash, plus, and combined wildcard stripping
There was a problem hiding this comment.
Pull request overview
This PR hardens the MQTT integration in Ecowitt.Controller by preventing unsafe user-provided strings from affecting MQTT topic structure, improving robustness of topic parsing, and reducing potentially sensitive logging.
Changes:
- Introduce
MqttPathBuilder.SanitizeSegmentand migrate topic-building code to sanitize user-provided segments (e.g., gateway names, nicknames, sensor names) by stripping MQTT wildcards/separators. - Fix Home Assistant command-topic parsing by locating the
subdevicessegment dynamically and adding bounds checks before indexing. - Reduce log exposure on publish failures (topic/reason only), and clean up project artifacts (remove stale Dockerfile + unused package references).
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Ecowitt.Controller/Service/Mqtt/MqttService.Publisher.cs | Stops logging full payloads at Warning level on publish failures. |
| src/Ecowitt.Controller/Service/Mqtt/MqttService.Events.cs | Makes HA command parsing resilient to multi-segment base topics and prevents out-of-range indexing. |
| src/Ecowitt.Controller/Service/Mqtt/MqttService.DiscoveryPublisher.cs | Uses segment-level sanitization when building Home Assistant discovery topics. |
| src/Ecowitt.Controller/Service/Mqtt/MqttService.Consumer.cs | Aligns discovery-removal topic construction with the new segment-level sanitization approach. |
| src/Ecowitt.Controller/Service/Mqtt/MqttPathBuilder.cs | Replaces the old Sanitize approach with SanitizeSegment, and preserves wildcard topic filters without sanitization. |
| src/Ecowitt.Controller/Ecowitt.Controller.csproj | Removes unused FluentValidation/Swashbuckle-related package references. |
| src/Ecowitt.Controller/Dockerfile | Deletes a stale debug-oriented Dockerfile. |
| src/Ecowitt.Controller.sln | Reorganizes workflow solution items into a dedicated solution folder. |
| src/EcoWitt.Controller.Tests/MqttPathBuilderTest.cs | Adds/updates unit tests covering the new SanitizeSegment behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ..\docs\architecture.md = ..\docs\architecture.md | ||
| EndProjectSection | ||
| EndProject | ||
| Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Github", "Github", "{BEB106CA-DDE3-4277-934E-A7E2910187D7}" |
…g exposure