Skip to content

Commit

Permalink
fix(smtp-server): fixes proxy protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Mar 13, 2024
1 parent 144af20 commit 9240612
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
11 changes: 6 additions & 5 deletions app/lib/smtp_server/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,19 @@ def logger
private

def proxy(data)
# inet-protocol, client-ip, proxy-ip, client-port, proxy-port
if m = data.match(/\APROXY (.+) (.+) (.+) (.+) (.+)\z/)
@ip_address = m[2]
check_ip_address
@state = :welcome
logger&.debug "\e[35mClient identified as #{@ip_address}\e[0m"
increment_command_count("PROXY")
"220 #{Postal::Config.postal.smtp_hostname} ESMTP Postal/#{id}"
else
@finished = true
increment_error_count("proxy-error")
"502 Proxy Error"
return "220 #{Postal::Config.postal.smtp_hostname} ESMTP Postal/#{trace_id}"
end

@finished = true
increment_error_count("proxy-error")
"502 Proxy Error"
end

def quit
Expand Down
2 changes: 1 addition & 1 deletion lib/postal/config_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ module Postal
end

boolean :proxy_protocol do
description "Enable proxy protocol for use behind some load balancers"
description "Enable proxy protocol for use behind some load balancers (supports proxy protocol v1 only)"
default false
end

Expand Down
28 changes: 28 additions & 0 deletions spec/lib/smtp_server/client/proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "rails_helper"

module SMTPServer

describe Client do
let(:ip_address) { nil }
subject(:client) { described_class.new(ip_address) }

describe "PROXY" do
context "when the proxy header is sent correctly" do
it "sets the IP address" do
expect(client.handle("PROXY TCP4 1.1.1.1 2.2.2.2 1111 2222")).to eq "220 #{Postal::Config.postal.smtp_hostname} ESMTP Postal/#{client.trace_id}"
expect(client.ip_address).to eq "1.1.1.1"
end
end

context "when the proxy header is not valid" do
it "returns an error" do
expect(client.handle("PROXY TCP4")).to eq "502 Proxy Error"
expect(client.finished?).to be true
end
end
end
end

end

0 comments on commit 9240612

Please sign in to comment.