foreman icon indicating copy to clipboard operation
foreman copied to clipboard

Fixes #36919 - Add URL validation to HTTP Proxy URL field.

Open chris1984 opened this issue 2 years ago • 7 comments

Before PR: prproxybefore

After PR: prproxy

To Test:

  • Apply PR
  • Create an HTTP Proxy with a nonfqdn value
  • Hit Test Connection

chris1984 avatar Nov 14 '23 16:11 chris1984

Could we have a clear error message, something like: Please enter a valid proxy URL.

lfu avatar Nov 16 '23 15:11 lfu

@lfu PR updated, here is the new screenshot prproxynew

chris1984 avatar Nov 16 '23 16:11 chris1984

To a bit more guiding: I suspect that you can change this bit: https://github.com/theforeman/foreman/blob/74933559fafc3f52494845bf82a5a67d83b2d584/app/controllers/http_proxies_controller.rb#L26-L33

To

  def test_connection
    http_proxy = HttpProxy.new(http_proxy_params)
    if http_proxy.invalid?
      # TODO: convert http_proxy.errors to some exception somehow
    end
    http_proxy.test_connection(params[:test_url])

    render :json => {:status => 'success', :message => _("HTTP Proxy connection successful.")}, :status => :ok
  rescue => e
    render :json => {:status => 'failure', :message => e.message}, :status => :unprocessable_entity
  end

See https://guides.rubyonrails.org/active_record_validations.html#valid-questionmark-and-invalid-questionmark & https://guides.rubyonrails.org/active_record_validations.html#validations-overview-errors as well

The main benefit is that you reuse the already existing validations. It may error out if the name is not specified though, which not be what you want.

ekohl avatar Nov 17 '23 16:11 ekohl

To a bit more guiding: I suspect that you can change this bit:

https://github.com/theforeman/foreman/blob/74933559fafc3f52494845bf82a5a67d83b2d584/app/controllers/http_proxies_controller.rb#L26-L33

To

  def test_connection
    http_proxy = HttpProxy.new(http_proxy_params)
    if http_proxy.invalid?
      # TODO: convert http_proxy.errors to some exception somehow
    end
    http_proxy.test_connection(params[:test_url])

    render :json => {:status => 'success', :message => _("HTTP Proxy connection successful.")}, :status => :ok
  rescue => e
    render :json => {:status => 'failure', :message => e.message}, :status => :unprocessable_entity
  end

See https://guides.rubyonrails.org/active_record_validations.html#valid-questionmark-and-invalid-questionmark & https://guides.rubyonrails.org/active_record_validations.html#validations-overview-errors as well

The main benefit is that you reuse the already existing validations. It may error out if the name is not specified though, which not be what you want.

Ok will go with this approach

chris1984 avatar Nov 17 '23 16:11 chris1984

@lfu updated, @ekohl how does this look?

chris1984 avatar Nov 21 '23 16:11 chris1984

@ekohl when you get a chance can this one get a review, I have added tests.

chris1984 avatar Jan 09 '24 15:01 chris1984

@ekohl can this one get back on your radar to review? It should be a quick one and I did tests as you asked.

chris1984 avatar Apr 08 '24 17:04 chris1984

@ekohl I'd like to merge this, but your previous change request is blocking it.

ianballou avatar Jun 12 '24 20:06 ianballou