benpickles / parklife

Render a Rack app (Rails/Sinatra/etc) to a static build so it can be served by Netlify, Now, GitHub Pages, S3, or any other web server.
https://parklife.dev
MIT License
193 stars 7 forks source link

403 error #91

Closed alienxp03 closed 1 year ago

alienxp03 commented 1 year ago

I'm getting 403 error when I'm trying to test this project. Is there any steps that I'm missing here?

1. Clone repo from GitHub
  > git clone git@github.com:benpickles/parklife.git 

2. Cd into Rails example
  > cd parklife/examples/rails      

3. Run server
  > bundle exec rails s

4. Run routes command (no output. Is this expected?)
  > bundle exec parklife routes                            
  /  crawl=true

5. Trying to build
  > bundle exec parklife build         
  bundler: failed to load command: parklife (/home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/bin/parklife)
  /home/user/Workspace/GitHub/parklife/lib/parklife/crawler.rb:64:in `process_route': 403 response from path "/" (Parklife::HTTPError)
    from /home/user/Workspace/GitHub/parklife/lib/parklife/crawler.rb:27:in `start'
    from /home/user/Workspace/GitHub/parklife/lib/parklife/application.rb:25:in `build'
    from /home/user/Workspace/GitHub/parklife/lib/parklife/cli.rb:15:in `build'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/base.rb:485:in `start'
    from /home/user/Workspace/GitHub/parklife/exe/parklife:5:in `<top (required)>'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/bin/parklife:25:in `load'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/bin/parklife:25:in `<top (required)>'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/cli/exec.rb:58:in `load'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/cli/exec.rb:58:in `kernel_load'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/cli/exec.rb:23:in `run'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/cli.rb:492:in `exec'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/cli.rb:34:in `dispatch'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/cli.rb:28:in `start'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/exe/bundle:45:in `block in <top (required)>'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
    from /home/user/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.8/exe/bundle:33:in `<top (required)>'
    from /home/user/.rbenv/versions/3.2.0/bin/bundle:25:in `load'
    from /home/user/.rbenv/versions/3.2.0/bin/bundle:25:in `<main>'
benpickles commented 1 year ago

Ah that's annoying. That's Rails (6+) host authorisation https://guides.rubyonrails.org/configuring.html#actiondispatch-hostauthorization rejecting the Parklife request because it's pretending to come from another host (example.com by default). That particular Rails middleware is only enabled in development which is why the error isn't present in CI - which runs Rails in production mode:

https://github.com/benpickles/parklife/blob/db2be1f6e91ec56f78099d54391778809aba2a43/examples/rails/bin/static-build#L7

You can allow the host or completely disable host authorisation in config/environments/development.rb but note that it's a security feature so I would only do it temporarily and not commit the change:

# This must match the host configured in Parklife's `base` (which defaults to example.com).
Rails.application.config.hosts << "example.com"

# Or completely disable host authorisation.
Rails.application.config.hosts = nil

Whilst I develop my Rails app locally in development mode I guess I don't encounter this issue because I don't run the actual Parklife build locally. It would be nice for Parklife to be able to disable this middleware itself when it needs to but by the time Parklife gets involved the Rails app has already booted and been configured and the middleware stack is frozen. But perhaps there's a way.

alienxp03 commented 1 year ago

Ah, cool. Will give it a try again once I read more about the Rails update. Thanks for the pointers!

benpickles commented 1 year ago

I've just merged #96 which prevents the 403 response issue - it'll be in the next release.