oauth2: undefined method `bytesize' (Incompatible with latest Faraday version)

Hi ✋🏻

With the last update: https://github.com/oauth-xx/oauth2/compare/v1.4.7...v1.4.8

NoMethodError at /auth/google_oauth2/callback

undefined method `bytesize' for {"client_id"=>"**********.apps.googleusercontent.com", "client_secret"=>"*********", "grant_type"=>"authorization_code", "code"=>"********", :redirect_uri=>"http://localhost:3000/auth/google_oauth2/callback"}:Hash
    self.content_length = body.bytesize
Screenshot 2022-02-18 at 13 28 52 Screenshot 2022-02-18 at 13 29 44

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 13
  • Comments: 20 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Version 1.4.9 is released. Changelog:

## [1.4.9] - 2022-02-20

- Fixes compatibility with Faraday v2 [572](https://github.com/oauth-xx/oauth2/issues/572)
- Includes supported versions of Faraday in test matrix:
  - Faraday ~> 2.2.0 with Ruby >= 2.6
  - Faraday ~> 1.10 with Ruby >= 2.4
  - Faraday ~> 0.17.3 with Ruby >= 1.9
- Add Windows and MacOS to test matrix

Just started seeing this same problem with the omniauth-auth0 gem too when switch from 1.4.7 to 1.4.8.

The fix is now in 1-4-stable. Please try out the branch before I release 1.4.8 if you have time.

With gem "oauth2", github: "oauth-xx/oauth2", branch: "1-4-stable" it works 🥇

The fix is now in 1-4-stable. Please try out the branch before I release 1.4.8 if you have time.

Before I release, I am going to:

  • Try to repro the actual error
    • if anyone can help by porting over their failing specs to this repo in a PR, that would be amazing.
  • Setup Appraisals to test multiple versions of Faraday
  • Increasse branched spec coverage from 86% to > 90%

The fix for this issue has been in master branch for 4.3 years. It is only a few lines of change. I was hoping to get version 2 of oauth2 out without needing to keep churning on the 1.4.x series, but haven’t had time to finish v2. 😦. It’s 97% complete! I was also hoping that we would not need to allow Faraday 2 upgrades in the 1.4.x series, but it creates a dependency tree snafu that is of unusual size for people who are blocked on the faraday upgrade.

I am working on backporting the fix now. https://github.com/oauth-xx/oauth2/pull/331/files

The primary change between 1.4.7 and 1.4.8 was to allow users to upgrade to faraday versions >= 2 (actually < 3)… Anyone experiencing this error in a test suite, and assuming you also pulled in a faraday update, please try locking faraday to the previous version and keep oauth2 at 1.4.8. If that resolves the error it will help diagnose a fix.

Alternatively, if you did not pull in a faraday update when you also pulled in oauth2 1.4.8, see if the error still happens if you do pull in a faraday update.

I am working on adding Appraisals to the build so the discrete versions of Faraday can be tested independently.

Thank you @pboling! Just wanted to note here that I upgraded today to oauth2 2.0.6 and also that version works in our application without problems. The omniauth-gitlab version stayed the same (4.0.0).

I now upgraded omniauth-gitlab to the recently released version 4.0.0 and that works with oauth2 version 1.4.9. I think this commit fixed the issue on the side of omniauth-gitlab. Additionally I needed to change the configuration setting from gitlab_url: 'https://our_gitlab_hostname/api/v4' to gitlab_url: 'https://our_gitlab_hostname'.

I guess the issue can be closed now.

For users of omniauth-gitlab it may still cause confusion that the configuration setting must include /api/v4 in the oauth2 versions up to 1.4.7 and must not include it starting from oauth2 1.4.9 which also requires omniauth-gitlab 4.0.0. @pboling Do you think this is specific to omniauth-gitlab and the necessary change of configuration should be clarified in omniauth-gitlab documentation or does it also affect other gems depending on oauth2?

(Just for completeness: The combination of omniauth-gitlab 4.0.0 and oauth2 1.4.8 produced the undefined method 'bytesize' error but that is not very relevant as there’s no reason for using version 1.4.8 instead 1.4.9 which works.)

@pboling Thank you very much for the answer!

I tried changing the configuration setting to gitlab_url: 'https://our_gitlab_hostname/'. In that case the authentication goes further, the GitLab login screen asking for username and password is shown, but the callback fails with Undefined method '[]' for nil:nilclass in the Rails application. Maybe the omniauth-gitlab gem (or the devise gem or our own app) has been relying on the old behavior and needs some corresponding small change after the bug fix.

(I’m on vacation this week and don’t have access to the system right now but if I’ll be happy to get back to it next week,)

I maintain a small in-house developed Rails application which authenticates using OAuth against our self-hosted GitLab instance (currently running GitLab version 14.8.). I’m using Debian 11, Ruby 2.7.4 and Rails 6.0.4.4.

The versions of the relevant gems in a working configuration are:

oauth2 (1.4.7)
devise (4.8.1)
faraday (1.10.0)
faraday-net_http (1.0.1)
omniauth (2.0.4)
omniauth-gitlab (3.0.0)
omniauth-oauth2 (1.7.2)
omniauth-rails_csrf_protection (1.0.1)

If I upgrade oauth2 to version 1.4.8, it selects faraday version 2.2.0 and faraday-net_http version 2.0.1 and breaks with the undefined method 'bytesize' error in the Rails application. Downgrading faraday back to 1.10.0 doesn’t help.

If I upgrade oauth2 to version 1.4.9, it selects faraday version 2.2.0 and faraday-net_http version 2.0.1 and breaks with a {"error":"404 Not Found"} message on the GitLab instance already before I get to the login screen. Downgrading faraday doesn’t help here either.

Looking at the nginx access log on the GitLab server gives some more information. In the working configuration with oauth2 version 1.4.7 (and also with oauth2 version 1.4.8) the first request coming in looks like: GET /oauth/authorize?client_id=XXXXX&redirect_uri=https%3A%2F%2Four_rails_app_hostname%2Fusers%2Fauth%2Fgitlab%2Fcallback&response_type=code&scope=api&state=XXXXX HTTP/2.0

When using oauth2 version 1.4.9, the request looks like this: GET /api/v4/oauth/authorize?client_id=XXXXX&redirect_uri=https%3A%2F%2Four_rails_app_hostname%2Fusers%2Fauth%2Fgitlab%2Fcallback&response_type=code&scope=api&state=XXXXX HTTP/2.0

In config/config.yml I have defined gitlab_url: 'https://our_gitlab_hostname/api/v4' as instructed in the documentation of the gem omniauth-gitlab (https://github.com/linchus/omniauth-gitlab/blob/v3.0.0/README.md).

It seems that with oauth2 version 1.4.9 and faraday version 2.2.0 this url gets processed differently and the api/v4 part is included in the GET request - which actually sounds logical but doesn’t work. I tried taking it out from the url defined in config/config.yml - then the authentication goes further, the login screen asking for username and password is shown, but the callback fails with Undefined method '[]' for nil:nilclass in the Rails application.

Please tell if you need any further information or have suggestions what I should try next.

@sue445 @cratown @jerryclinesmith @dambrisco @benoittgt @tfader @scottsherwood @RSO It should work with release 1.4.9. Please let me know if you find otherwise!

Keeping oauth2 1.4.8, but dropping faraday back to < 2 (1.9.3 specifically) fixed the problem for me.