Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port 80 being used even when using a proper host or SERVER_PORT #118

Open
caiofct opened this issue Apr 20, 2023 · 1 comment
Open

Port 80 being used even when using a proper host or SERVER_PORT #118

caiofct opened this issue Apr 20, 2023 · 1 comment

Comments

@caiofct
Copy link

caiofct commented Apr 20, 2023

We were using this gem for a while and never had problems with it, however, after we upgraded rack from 2.1.4.3 to 2.2.6.4 we started observing some issues with the way this gem parses a Rack:Request. It's important to note that this only happens when running the app through docker.

In our Proxy we do:

class LocalProxy < Rack::Proxy
  def perform_request(env)
    request = Rack::Request.new(env)

    if request.path.starts_with?("/some_service")
      uri = URI.parse(ENV["PROXY_URL"])

      env["HTTP_HOST"] = "#{uri.host}:#{uri.port}"
      env["SERVER_PORT"] = uri.port
      env["PATH_INFO"] = request.path.gsub("/some_service", "")
      env["HTTP_COOKIE"] = ""
      env["HTTP_X_FORWARDED_PROTO"] = "http"
      env["rack.url_scheme"] = "http"
      @port = uri.port.to_i
      super(env)
    else
      @app.call(env)
    end
  end
end

The PROXY_URL env var has a value of some_service:3001. And when trying to access the http://localhost:3000/some_service path we get an error: Failed to open TCP connection to some_service:3001:80 (getaddrinfo: Name or service not known). After spending some time investigating this it seems the issue is with the way a Rack::Request is being parsed. Not sure if this is an issue with rack or on this gem's code. But wanted to point out something:

irb(main):005:0> r = Rack::Request.new({ "HTTP_HOST" => "http://some_service:3001" })
=> #<Rack::Request:0x0000aaaada21c0c0 @params=nil, @env={"HTTP_HOST"=>"http://some_service:3001"}>
irb(main):006:0> r.host
=> "http://some_service:3001"
irb(main):007:0> r.port
=> nil
# Using SERVER_PORT instead
irb(main):009:0> r = Rack::Request.new({ "HTTP_HOST" => "some_service", "SERVER_PORT" => 3001 })
=> #<Rack::Request:0x0000aaaad7a11fa8 @params=nil, @env={"HTTP_HOST"=>"some_service", "SERVER_PORT"=>3001}>
irb(main):010:0> r.host
=> "some_service"
irb(main):011:0> r.port
=> 3001

The code above is for rack 2. Rack 3 seems to handle the host parsing a lot better however setting a SERVER_PORT should have worked but actually didn't. So I'm clueless and would like to know if anyone here has any ideas or have experienced something similar.

Also I'm intrigued by the fact that we were using the same rack-proxy version of 0.7.6 with rack 2.1.4.3 for a while and didn't have any issues. Which leads me to believe this might be a regression from: rack/rack#1606 which was backported to 2.2.6.4.

Rails version: 7.0.4
Ruby: 2.7.8
rack-proxy: 0.7.6
rack: 2.2.6.4

@caiofct
Copy link
Author

caiofct commented Apr 20, 2023

I've run some tests locally and discovered that rack 2.1.4.3 parses the request correctly:

irb(main):001:0> gem 'rack', '=2.1.4.3'
irb(main):002:0> require 'rack'
irb(main):003:0> r = Rack::Request.new({"HTTP_HOST" => "some_service:3001"})
irb(main):004:0> r.port
=> 3001

So it seems rack 2.2.6.4 introduced some regressions here. I've created an issue there rack/rack#2070 so we have better tracking. Let me know if we should close this issue or if there's any workarounds on rack-proxy's side I could try.

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

No branches or pull requests

1 participant