Giter Site home page Giter Site logo

Comments (18)

rymai avatar rymai commented on June 15, 2024

Hi,

This is a tricky use-case, but maybe by setting the strict option only on the second statement might work:

config.middleware.use Rack::SslEnforcer,
  only_hosts: %r{api.example.com}

config.middleware.use Rack::SslEnforcer,
  except_hosts: %r{api.example.com},
  only: %r{/users},
  ignore: %r{/assets},
  strict: true

So the first statement ensures that all calls on api.example.com will be redirected to HTTPS, and then the second statement ensures that all calls matching /users will be redirected to HTTPS, and every other calls will be redirected to HTTP (ignoring calls matching /assets).

I haven't actually tried this case, I'll try to add this test cases of the projects to see if that actually works.

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

nope it works the same way. /users paths works as https enabled but api.example.com ended in a redirect loop.

from rack-ssl-enforcer.

rymai avatar rymai commented on June 15, 2024

Ok, is the strict option mandatory in your use-case then?

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

yes

from rack-ssl-enforcer.

rymai avatar rymai commented on June 15, 2024

Ok, I've added your use-case to our test suite and it works as expected...

What do you think?

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

why is except_hosts set to api.example.com and only_hosts set to api.example.org?

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

ok I forked and checked with api.example.org at both place and tests are passing fine. But in Rails app it's not working. Is there a way to define the both statements in one array (like in test case) rather than 2 different statements (like above). Something like

config.middleware.use Rack::SslEnforcer, [
{ :only_hosts => %r{api.example.org} },
{ :except_hosts => %r{api.example.org}, :only => %r{^/users}, :ignore => %r{^/assets}, :strict => true }
]

Just a silly thought.

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

@rymai I guess above isn't silly thought :), if i define 2 statements in test case, rather than one array.

setup {
        mock_app :only_hosts => %r{api.example.org}
        mock_app :except_hosts => %r{api.example.org}, :only => %r{^/users}, :ignore => %r{^/assets}, :strict => true
}

One Test fails (api test). And that's exactly the same thing happening in rails app. Any suggestions?

from rack-ssl-enforcer.

rymai avatar rymai commented on June 15, 2024

Sorry about the typo, but actually the :except_hosts is actually not needed at all (I've fixed the test)!

Re. the test following setup:

setup {
  mock_app :only_hosts => %r{api.example.org}
  mock_app :except_hosts => %r{api.example.org}, :only => %r{^/users}, :ignore => %r{^/assets}, :strict => true
}

some tests fail because the first statement is not part of the app that's used for the tests. In fact the last call to mock_app replaces the app you created with the first call. That's why I passed 2 hashes to mock_app in order to define two use Rack::SslEnforcer statements.

That all said, could you share your Rails config file where you use use Rack::SslEnforcer? Thanks!

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

Here's my rack_ssl_config.rb from /config/initializers directory

Website::Application.configure do

  if AppConfig.ssl.enabled
    config.middleware.use Rack::SslEnforcer,
                          only_hosts: AppConfig.api.host

    config.middleware.use Rack::SslEnforcer,
                          except_hosts: AppConfig.api.host,
                          only: %r{/users},
                          ignore: %r{/assets},
                          strict: true
  end

end

I've no clue to pass both statements in one hash. what will be the code for it?

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

oh now i studied the mock_app method. My bad. Sorry for above. But there is one problem with tests, you've to add one more test that when someone access the https://api.example.org it shouldn't redirect and that's failing

should 'stay on HTTPS for https://api.example.org' do
      get 'https://api.example.org'
      assert_equal 200, last_response.status
      assert_equal 'https://api.example.org/', last_response.location
end

and that's creating a redirect loop.

from rack-ssl-enforcer.

akashkamboj avatar akashkamboj commented on June 15, 2024

ok this hasn't solved from long time. So I guess if there's another option where I can pass a Proc or a property called ignore_hosts so that I can ignore api.example.org and manage it manually. Is that possible. I was also thinking to override the call method for middleware, but thought if ignore_hosts is there, that will be better instead overriding the call. What do you think?

from rack-ssl-enforcer.

tobmatth avatar tobmatth commented on June 15, 2024

@rymai, @thibaudgg would you take a look at branch issue62? 1c985f6 should fix this issue, but two other tests are broken now (both a comination of :strict and :except*). I believe the :except option should except specific paths, hosts or whatever from any modification - any other opinions on that?

from rack-ssl-enforcer.

thibaudgg avatar thibaudgg commented on June 15, 2024

@rymai could you look at it, I'm in the middle of my move, thanks!

from rack-ssl-enforcer.

rymai avatar rymai commented on June 15, 2024

I'll have a look this evening!

from rack-ssl-enforcer.

rymai avatar rymai commented on June 15, 2024

Ok, I've had a chance to review this issue. I think :except* constraints + :strict option make sense. Why should it be different than for :only* constraints?

from rack-ssl-enforcer.

samnang avatar samnang commented on June 15, 2024

I face the same situation with @akashkamboj. I got redirect loop when I go http://api.example.com

config.middleware.use Rack::SslEnforcer, only_hosts: 'api.example.com'
config.middleware.use Rack::SslEnforcer,
  only: ['/admin/login', %r{^/user/password}],
  ignore: %r{^/assets},
  strict: true

Even it still get redirect loop when I try with except_hosts

config.middleware.use Rack::SslEnforcer, only_hosts: 'api.example.com'
config.middleware.use Rack::SslEnforcer,
  except_hosts: 'api.example.com',
  only: ['/admin/login', %r{^/user/password}],
  ignore: %r{^/assets},
  strict: true

from rack-ssl-enforcer.

tobmatth avatar tobmatth commented on June 15, 2024

I'll try to take a closer look before the weekend...

from rack-ssl-enforcer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.