Skip to content

Security hardening: sanitize MQTT topics, fix bounds check, reduce lo… - #14

Merged
mplogas merged 3 commits into
mainfrom
fix/security-hardening
May 3, 2026
Merged

mplogas merged 3 commits into
mainfrom
fix/security-hardening

Conversation

@mplogas

@mplogas mplogas commented Mar 13, 2026

Copy link
Copy Markdown
Owner

…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

…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
Copilot AI review requested due to automatic review settings March 13, 2026 22:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/Ecowitt.Controller/Service/Mqtt/MqttService.Events.cs Outdated
Comment thread src/Ecowitt.Controller/Service/Mqtt/MqttPathBuilder.cs
Comment thread src/Ecowitt.Controller/Service/Mqtt/MqttPathBuilder.cs Outdated
mplogas added 2 commits March 19, 2026 19:15
…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
Copilot AI review requested due to automatic review settings April 27, 2026 20:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.SanitizeSegment and 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 subdevices segment 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}"
@mplogas
mplogas merged commit d3583d7 into main May 3, 2026
5 checks passed
@mplogas
mplogas deleted the fix/security-hardening branch May 3, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants