Skip to content

Escape ADMIN_PASSWORD in the AMQP URL and ignore blank RABBITMQ_URL / CLOUDAMQP_URL values - #2529

Open
tencur wants to merge 1 commit into
FarmBot:stagingfrom
tencur:fix/amqp-url-escape-password
Open

tencur wants to merge 1 commit into
FarmBot:stagingfrom
tencur:fix/amqp-url-escape-password

Conversation

@tencur

@tencur tencur commented Nov 17, 2026

Copy link
Copy Markdown

What goes wrong. On a docker-compose self-host, the API's AMQP connection to the bundled broker can fail on every attempt while everything else looks healthy. The browser and the bot still connect over MQTT, but:

  • edits made in the Web App never auto-sync to the bot (AutoSyncJob fails its 4 attempts and is dropped), so the bot keeps running the old sequences and farm events until someone syncs by hand;
  • FBOS logs and telemetry are never saved (rabbit_workers retries forever);
  • Device#tell messages are lost.

The only sign is a Connecting to broker in 3 seconds (#<URI::InvalidURIError ...>) line repeating in the rabbit_jobs container's log.

There are two ways to get there:

  1. ADMIN_PASSWORD contains a character that is not allowed in the userinfo part of a URL, e.g. @, #, /, ?, %, {, } or a space. example.env asks for "something SECURE", and password-manager passwords often contain these.
  2. The user keeps the rest of example.env (it says to do so if you want email) including the blank lines WHERE_IS_CLOUDAMQP_URL=, CLOUDAMQP_URL= and RABBITMQ_URL=. Unlike the neighbouring lines, these carry no "delete this" note.

Cause. app/models/transport.rb:

CLOUDAMQP_ENV_KEY = ENV.fetch("WHERE_IS_CLOUDAMQP_URL", "CLOUDAMQP_URL")

def self.amqp_url
  @amqp_url ||= ENV[CLOUDAMQP_ENV_KEY] ||
                ENV["RABBITMQ_URL"] ||
                "amqp://admin:#{ENV.fetch("ADMIN_PASSWORD")}@mqtt:5672"
end
  • The password is interpolated without escaping, so e.g. p@ssw0rd99 or abc#def123 give a URL that URI.parse (and therefore Bunny) rejects.
  • docker's env_file turns RABBITMQ_URL= into an empty string. ENV.fetch("WHERE_IS_CLOUDAMQP_URL", ...) returns "", ENV[""] is nil, and ENV["RABBITMQ_URL"] is "", which is truthy in Ruby. So amqp_url is "" and Bunny raises "Connection URI must use amqp or amqps schema".

MQTT logins are not affected because RabbitMQ checks them over HTTP against the plain password, so nothing in the UI shows the problem.

What this change does.

  • app/models/transport.rb:
    • CLOUDAMQP_ENV_KEY, CLOUDAMQP_URL and RABBITMQ_URL use .presence, so blank values fall through to the default.
    • The default URL moves into Transport.default_amqp_url, which escapes the password with URI.encode_www_form_component. amq-protocol unescapes the userinfo when it parses the URL, so Bunny receives the original password.
    • Transport::Mgmt.username / .password decode the userinfo they read from the URL so the management API client gets the real credentials, and a blank RABBIT_MGMT_URL falls back to the computed URL.
  • example.env: comments out the three AMQP lines and says that self-hosters using the bundled broker should leave them that way.
  • spec/lib/transport_spec.rb: blank URL variables give the default mqtt URL, RABBITMQ_URL is used when set, and a password containing @ # / ? % { } and a space produces a URL that parses and decodes back to the original password.

How this helps a server without the cloud: the default amqp://admin:...@mqtt:5672 path is exactly what a LAN-only docker-compose install uses, and with this change it works with passwords like the ones above and with example.env as shipped.

Testing. I wasn't able to run rspec locally (no Ruby toolchain on the machine I used), so I'd appreciate CI confirmation. The existing transport_mgmt_spec expectation (Transport::Mgmt.password == ADMIN_PASSWORD) should still hold, since decoding a password that needed no escaping returns it unchanged. I haven't tested against a real broker.

🤖 Generated with Claude Code

… CLOUDAMQP_URL values

**What goes wrong.** On a docker-compose self-host, the API's AMQP connection to the bundled broker can fail on every attempt while everything else looks healthy. The browser and the bot still connect over MQTT, but:

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant