Skip to content

Omniauth impact on non omniauth routes #927

Closed

Description

@SamSaffron

Omniauth seems to spend time (and allocate lots of objects) in its internal middleware stack on every request.

https://gist.github.com/SamSaffron/2c7f2ce4089abf0375c5be9cfbbfefb1

This is clearly totally unneeded in non omniauth routes. I created a simple middleware in Discourse to bypass per:

discourse/discourse@442a17b

I wonder

  1. How safe is this, does omniauth need access to non /auth routes?
  2. Is a similar pattern doable out-of-the-box?

Also: memory profiler report:

allocated objects by location
-----------------------------------
       522  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/hashie-3.5.5/lib/hashie/mash.rb:353
       382  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/hashie-3.5.5/lib/hashie/mash.rb:316
       252  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/hashie-3.5.5/lib/hashie/mash.rb:311
        67  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/hashie-3.5.5/lib/hashie/mash.rb:189
        32  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/hashie-3.5.5/lib/hashie/mash.rb:331
        24  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/omniauth-1.6.1/lib/omniauth/strategy.rb:138
        21  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rack-2.0.3/lib/rack/builder.rb:86
        16  /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/hashie-3.5.5/lib/hashie/mash.rb:329

^^^ All allocated by omniauth on a non omniauth related route


allocated memory by class
-----------------------------------
     45006  String
     38320  Array
     18176  OmniAuth::Strategy::Options
     13128  Hash

^^^ mostly omniauth

Activity

  1. swrobel commented on Apr 2, 2018

    @swrobel

    @schneems been a long time since you sent in your Hashie-memoization PR, but maybe you have some thoughts about this...?

  2. BobbyMcWho commented on Dec 8, 2020

    @BobbyMcWho
    Member

    I've been exploring this today, and I've not come up with a simple drop-in solution unfortunately. The wrapper discourse uses works well, as long as you're guaranteed that the paths begin with '/auth'. I'll have to noodle on it a bit more when I get some more time.

  3. rainerborene commented on Oct 9, 2023

    @rainerborene

    Thanks @SamSaffron for bringing this up. I came up with a simpler solution using an initializer and monkey patch.

    module OmniAuth::Strategy
      def call(env)
        if env["PATH_INFO"].start_with?("/users/auth")
          dup.call!(env)
        else
          @app.call(env)
        end
      end
    end
  4. arpu commented on Nov 11, 2023

    @arpu

    @rainerborene how do i add this?

  5. arpu commented on Nov 11, 2023

    @arpu

    ok looks like i can overwrite this in the config/initializers/omniauth.rb

    any idea how i can enable the session only for the omniauth routes?

  6. added theissue type on Oct 17, 2026
  7. sferik commented on Oct 20, 2026

    @sferik
    Contributor

    OmniAuth::Builder was rebuilding its whole middleware stack, including every strategy, on every request, even ones that have nothing to do with authentication (#1083). That's fixed in c29ad9b: the stack is now built once and reused. It'll ship in the next release. If you still see a noticeable cost after upgrading, please open a new issue with a benchmark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions