fix(evonet): add configurable protocol detection for binary downloads#74
Open
DeryFerd wants to merge 1 commit into
Open
fix(evonet): add configurable protocol detection for binary downloads#74DeryFerd wants to merge 1 commit into
DeryFerd wants to merge 1 commit into
Conversation
… Add EVONIC_PUBLIC_PROTOCOL environment variable to control protocol (http/https) embedded in downloaded Evonet connector binaries. Changes: - Add EVONIC_PUBLIC_PROTOCOL config with validation (auto/http/https) - Update protocol detection in _build_binary() with explicit override - Add debug/warning logs for protocol detection traceability - Document configuration in .env.example Motivation: When Evonic is deployed behind non-standard reverse proxies, automatic protocol detection from request headers may be unreliable. If the wrong protocol is embedded in the binary, the connector will: 1. Fail to connect (protocol mismatch) 2. Potentially expose connector_token over plaintext HTTP This fix allows explicit protocol configuration while maintaining backward-compatible auto-detection as the default. Protocol detection priority (auto mode): 1. X-Forwarded-Proto header 2. X-Real-Proto header 3. CF-Visitor header (Cloudflare) 4. request.scheme (ProxyFix-corrected) 5. Fallback to https with warning Closes issue identified in security audit: protocol misconfiguration could expose connector tokens in certain proxy configurations.
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.
Problem
When you deploy Evonic behind a reverse proxy, the system needs to figure out whether it's running on
http://orhttps://so it can embed the right URL in the downloaded Evonet connector binaries. Get it wrong and the connector fails to connect—or worse, tries to send theconnector_tokenover plaintext HTTP.The current implementation tries to auto-detect the protocol from request headers (
X-Forwarded-Proto,request.scheme, etc.), which works fine if your proxy setup is standard. But if you're running something custom, or if your proxy chain is weird, or if ProxyFix isn't configured exactly right, the detection can fail. When it does, the code silently falls back tohttpswithout logging anything, which makes debugging really annoying.I ran into this during a security audit when I realized that in some proxy configurations, the binary could end up with the wrong protocol embedded. If it guesses
httpwhen it should behttps, the connection fails. If it guesseshttpswhen it should behttp(rare but possible in dev setups), same problem. And if someone's proxy stripsX-Forwarded-Protoentirely? The fallback just pickshttpsand hopes for the best.The Fix
This PR adds a new environment variable,
EVONIC_PUBLIC_PROTOCOL, that lets you explicitly override the protocol. Three modes:auto(default): Auto-detect from headers (current behavior, but with better logging)https: Always use HTTPS (recommended for production)http: Always use HTTP (local dev only, do not use in production)When
autois set, the detection logic now checks headers in priority order:X-Forwarded-ProtoX-Real-ProtoCF-Visitor(Cloudflare-specific, parses the JSON)request.scheme(already corrected by ProxyFix if you have it)httpswith a warning logIf you set
EVONIC_PUBLIC_PROTOCOL=httpsorhttpexplicitly, it skips all the detection and just uses what you told it to use. Debug logs show which source was used, so you can actually see what's happening instead of guessing.Why
The
connector_tokenis sensitive. If the binary embedshttp://your-server.comwhen it should behttps://your-server.com, the connector tries to authenticate over plaintext. That's not great. This fix gives you control so you can lock it down in production while still keeping auto-detection for simpler deployments.Also, the warning log helps. If detection is uncertain, you'll see a message like:
That's way better than silently picking
httpsand hoping.What Changed
Config (
config.py)EVONIC_PUBLIC_PROTOCOLwith validationauto)Protocol Detection (
routes/workplaces.py)auto, run through the header detection in priority orderDocumentation (
.env.example)Testing
I wrote two test scripts to verify this works:
Config loading test (6 scenarios):
auto,http,https)HTTPS→https)auto)auto)Protocol detection test (8 scenarios):
X-Forwarded-Proto>X-Real-Proto>CF-Visitor)request.schemefallbackhttpswith warning)All tests passed. Syntax check passed. No regressions.
Backward Compatibility
The default is
auto, which matches the current behavior (auto-detect from headers). Existing deployments don't need to change anything. If you're already using ProxyFix and standard headers, everything keeps working.If you're in a weird proxy setup and want to lock it down, set
EVONIC_PUBLIC_PROTOCOL=httpsand you're done.When to Use Each Mode
auto(default) — Let Evonic figure it out from request headers. Works for most standard setups (Nginx, Caddy, Traefik with proper proxy headers).https— Force HTTPS in production when you're behind a custom proxy or when auto-detection is unreliable. Set this if you've seen connection failures or if you want to eliminate the guesswork entirely.http— Local development without TLS. Do not use this in production unless you really know what you're doing and have some other way of protecting the connector token in transit.What I Checked
python -m py_compile.env.exampleupdated with clear documentationWhat I Didn't Test
I did not test this against a live reverse proxy setup with real connectors. The logic is sound and the tests pass, but if you're deploying this in production, verify the embedded URL in a downloaded binary before rolling it out widely.
To check: download a binary, extract the embedded JSON config at the end (after the
\x00\x00EVONET_CFG\x00\x00marker), and confirm theserver_urlfield has the right protocol.