Search code examples
ruby-on-railsnginxrack

Where should URIs be decoded in a Nginx - Puma - Rails stack?


I've a Rails app that needs to accept some URIs with unencoded %2f (forward slashes). These characters need to be interpreted as forward slashes, even though I know it's not ideal.

What would be the best solution to do so? I'm thinking about a nginx rewrite rule or a custom rack middleware. Is there any other solution or suggested way to do so?


Solution

  • We ended up doing it with the following middleware:

    class SlashDecoder
      def initialize(app)
        @app = app
      end
    
      def call(env)
        env["PATH_INFO"] = env["PATH_INFO"].gsub(/%2[fF]/, "/") if env["REQUEST_METHOD"] == "GET"
        @app.call(env)
      end
    end