security: fix auth, input validation, MQTT hardening, and modernize stack - #2
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the webhook-to-MQTT bridge by adding optional webhook authentication, request-size limiting, and MQTT TLS/credentials support, while also modernizing the container/base-image and pinning Python dependencies for more reproducible builds.
Changes:
- Add optional
WEBHOOK_TOKENauth, 1MB request body cap, safer JSON handling, and improved MQTT publish robustness (TLS + user/pass + exception handling). - Modernize deployment: switch to
python:3.12-slim, run via explicitgunicornCMD, and add pinnedrequirements.txt. - Remove hardcoded personal hostnames from compose; add
.env.exampleand ignore.env.
Reviewed changes
Copilot reviewed 4 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| requirements.txt | Adds pinned Flask / paho-mqtt / gunicorn dependencies for reproducible installs. |
| Dockerfile | Switches to python:3.12-slim, installs via requirements, runs Gunicorn explicitly. |
| docker-compose.yml | Replaces hardcoded hostnames/envs with variable-driven configuration suitable for .env. |
| app/main.py | Implements webhook auth, request size limit, MQTT auth/TLS, error handling, and fixes exit/JSON handling. |
| .gitignore | Ignores .env to prevent committing secrets. |
| .env.example | Documents the expected environment variables and recommended auth usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+87
to
+90
| myparams = request.get_json() | ||
| if myparams is None: | ||
| logger.warning("Received non-JSON or empty request body") | ||
| return Response(status=400) |
Comment on lines
+82
to
+86
| if webhook_token: | ||
| auth_header = request.headers.get('Authorization', '') | ||
| if auth_header != 'Bearer {}'.format(webhook_token): | ||
| logger.warning("Unauthorized request from {}".format(request.remote_addr)) | ||
| return Response(status=401) |
Comment on lines
+75
to
+76
| except Exception as e: | ||
| logger.error("Failed to publish MQTT message: {}".format(e)) |
|
|
||
| COPY ./app /app | ||
|
|
||
| CMD ["gunicorn", "--bind", "0.0.0.0:80", "main:app"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Multiple security gaps and bugs in the webhook-to-MQTT bridge: unauthenticated endpoint, anonymous plaintext MQTT, no request body limit, silent crash on missing
MQTT_SERVER, and a Pythonos.exit()that doesn't exist.Bug fixes
os.exit()→sys.exit(1)on missingMQTT_SERVERrespond()returns400when body is not valid JSON (previously passedNoneto worker thread, causing silentTypeError)Security
WEBHOOK_TOKENenv var; when set, requiresAuthorization: ****** on everyPOST /, returns401` otherwise — with a startup warning if unsetMAX_CONTENT_LENGTH = 1 MBto prevent oversized payloads reaching MQTTMQTT_USER/MQTT_PASSWORDenv vars →client.username_pw_set()MQTT_TLS=trueenv var →client.tls_set()docker-compose.yml; replaced with${MQTT_SERVER}/${WEBHOOK_HOST}variables..env.exampleadded;.envin.gitignoreReliability
workit()thread wrapped intry/except— MQTT failures now logged instead of silently droppedpaho-mqttmigrated fromCallbackAPIVersion.VERSION1→VERSION2Dependencies & image
tiangolo/meinheld-gunicorn-flask:latest(unmaintained) →python:3.12-slimwith explicitgunicornCMDrequirements.txtadded with pinned versions:Flask==3.1.3,paho-mqtt==2.1.0,gunicorn==26.0.0pip install -r requirements.txtversion: "3.6"key fromdocker-compose.yml