10 ms·
Bypassing GitHub's OAuth Flow with a Head Request
- purephase 7y ago* 2019-06-19 23:28:56 UTC Issue reported to GitHub on HackerOne * 2019-06-19 23:36:50 UTC Issue confirmed by GitHub security team * 2019-06-20 02:44:29 UTC Issue patched on github.com, GitHub replies on HackerOne to double-check that the patch fully resolves the issue * 2019-06-26 16:19:20 UTC GitHub Enterprise 2.17.3, 2.16.12, 2.15.17, and 2.14.24 released with the patch (see GitHub’s announcement). * 2019-06-26 22:30:45 UTC GitHub awards $25000 bounty So used to these disclosure articles where the timelines look very different, communication is lacking and there's pushback on the bounties etc. Refreshing to see that sometimes the process works as everyone hopes it will.
- zaroth 7y agoI mean, it’s a total auth bypass in GitHub by simply sending a HEAD request, with a one line fix. The turnaround time shows that they have the ability to evaluate a serious report through HackerOne quickly, they have a dedicated engineer who could get in and make the one line fix, and they have the ability to deploy new code quickly. All good things! I am mostly impressed that they paid out $25k in under a week. That’s a sign of not just a good engineering setup, but some good bureaucracy as well.
- _jal 7y agoPerhaps it does merely reduce to "being competent". But the contrast with nearly every other vendor out there is stark. Been managing GHE for years, and I have been consistently impressed with Github's code quality, competence, and even product management (nearly every time I think, "there should be a way to...", there already is). From the outside, at least, they look like the model to emulate for this sort of application.
- jrochkind1 7y agoYou're both right, competency is in fact really damn impressive. (I have come to think, as a software engineer, that we've comuterized so much of social life such that our economy literally could not afford it if it wasn't mostly crap).
- Tijdreiziger 7y agoIt doesn't say they paid out yet, just that they 'awarded' the bounty, which could just be GitHub telling HackerOne they intend to pay without actually having moved any money yet.
- mtnGoat 7y agoRegardless, the fact that someone at Github can approve such a large payment so quickly, is still impressive.
- NightlyDev 7y agoI don't think $25k is a large payment in githubs eyes. There are much smaller companies that doesn't even blink spending ten times as much. And this was definitely approved before they went out and told people what they would pay for different bugs.
- spullara 7y agoHow long until someone is coding up a minivan? https://dilbert.com/strip/1995-11-13 https://dilbert.com/strip/1995-11-13
- notacoward 7y agoExactly what I thought too. Just for once, a bug bounty program worked as intended. Bug was fixed in a timely fashion instead of being denied or ignored, bounty was awarded, disclosure was made, life gets better for everyone. Nice to see.
- throwaway5752 7y ago"Just for once, a bug bounty program worked as intended" I would just say, you don't generally hear about it when it works as intended (as it usually does). Your sentiment, though, is why it's nice when it is publicized that the system is working as intended, like it did in this case.
- t34543 7y agoI run a bounty program and I like to think my company does a good job, too. I received a report of a RCE in a desktop app and got a fix released in 72h. We have a bounty committee that reviews payouts and paid in one week.
- FactolSarin 7y agoIt's good PR too. This really makes me appreciate GitHub that much more
- diafygi 7y agoIn the proof of concept[1]: 1. Send an ajax HEAD request, which grants the permissions. 2. However, since the HEAD response doesn't have valid CORS headers, you can't read the 'code' parameter from the HEAD response. 3. To get around CORS, the proof-of-concept redirects the user to the same OAuth url again. 4. Since the user has already authorized the scope (from the previous HEAD request), Github automatically redirects back to the redirect_uri (the proof-of-concept page) with a new 'code' that the author can then use. So I have two questions: a) If I have 3rd party cookies disabled, will the "credentials: 'include'," part of the ajax HEAD request still work? b) Why does Github automatically redirect back the 2nd time without asking the user to authorize again? I thought it was best practice to ask the user to click authorize every time even if they're authorizing the same scope they previously authorized? [1]: https://not-an-aardvark.github.io/oauth-bypass-poc-fbdf56605489c74b2951/ https://not-an-aardvark.github.io/oauth-bypass-poc-fbdf56605...
- homakov 7y agoOnly step 1 is important, that's where the bug is. The rest is routine to get a code for your already authorized app.
- zaroth 7y agoWhy would you want to ask the user to authorize an app they already authorized? IMO that would be a bug and definitely unexpected behavior. It would give the end-user the impression the app was not authorized when in fact it was, which could create its own issues. So now you’re designing a whole different page which shows the app is authorized with a POST form just to do the redirect? And if the entire purpose is to try to avoid any place that you can GET a redirect to the app, you better hope that’s the only place where it could happen.
- diafygi 7y agoHmmm, as a user, I guess I would expect the opposite behavior, and I'd think the question should be why wouldn't you want to ask the user to authorize and app they already authorized? A client should usually only redirect the user to the OAuth authorization endpoint when: (a) they don't have an access_token (e.g. they haven't asked for authorization yet) (b) their current access_token or refresh_token got rejected (e.g. the resource owner revoked access, so they need to reauthorize) (c) they want to change scope of access So the situation where you redirect the user to the OAuth authorization endpoint when they've already granted access and with the same scope is usually when something strange is going on (like when the client loses your access_token or this exploit happens). So why would you have different behavior (not asking to click Authorize) just to streamline the atypical scenario? Especially when it leaves open the possibility to be exploited[1], create confusing situations[2], or be mis-categorized as an opt-in event when it isn't[3]. As a user, I'd like to think that I'd always get the same behavior every time I'm redirected to an OAuth authorization endpoint, because the OAuth provider doesn't have any idea what the situation is on the client side and assuming it's okay to auto-redirect without asking for my consent again is dangerous. [1]: This HEAD bug would have not been able to be exploited in the wild if the auto-redirect wasn't in place. [2]: In clients that allow users to have multiple accounts (where each can hook-up a github connection), auto-redirects creates confusing revocations across the multiple client accounts. For example, Bob authorizes a github connection to account A, then when he switches to account B wants to authorize github again. If auto-redirect is in place, then Bob wouldn't see anything (since it is a transparent redirect), but account B's connection would start working and account A's connection would silently stop working. If there were an authorize click, Bob would see the same behavior for account A and account B, and probably be able to put together that if he clicks authorize on one account, the other stops working. Auto-redirects makes Bob figuring out what's going on really hard. [3]: Privacy regulations are requiring opt-in more and more, and OAuth is seen as a fairly good mechanism for proving someone "opt'd in". If a regulation requires a "consent" to have an expiration (e.g. an accounting app must require the user to re-authorize bank access on a yearly basis), then when the users is asked to "consent" again, if they get auto-redirected, they can later claim they were never asked to consent because they never clicked authorize again after a year.
- homakov 7y agoWow, why would the browser allow HEAD request in the first place w/o explicit confirmation from github server with CORS headers. This is very strange. It also reminded me of why "match" was removed in the first place - http://homakov.blogspot.com/2012/04/whitelist-your-routes-match-is-evil.html http://homakov.blogspot.com/2012/04/whitelist-your-routes-ma... - you could route any POST request via GET therefore bypassing CSRF checks still getting inside the controller.
- lol768 7y agoWhy's it strange? In a properly designed application, it shouldn't do anything any different to the no-cors requests you can already achieve with e.g. an <img> element. HEAD, GET et al are all supposed to be "safe methods" per the spec.
- homakov 7y agoYou can't achieve a HEAD request with <img> elements, can you? In a rails env the only way to upgrade to non-standard methods (PATCH, PUT etc) were always to supply _method and pass CSRF protection first. This trick is clearly a bypass because it instructs browser to make a HEAD request, something it never did before. If you try to make other non-standard request you will get this error: Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': 'PATCH' is unsupported in no-cors mode.
- lol768 7y agoNo, but you can achieve a GET which is also a "safe request method" per RFC7231. All of these requests should be idempotent. I'm not a fan of the hacks that exist to allow HTML forms to emulate other request methods. They're off by default in ASP.NET Core MVC, which is good IMO.
- homakov 7y ago"should". Real apps are much more complicated than what they are in theory. In theory all CSRF protections must be based on an auth token, in practice they routinely rely on the method or referrer. For this reason browser upgrades must be extremely careful to not break older protections. That's why you cannot set your own referer (which is supposed to mean nothing), you cannot supply Content-type: application/json unless instructed by CORS preflight, etc etc. Here, the backwards compatibility was clearly broken. It was never possible to send HEAD few years ago with regular XHR or <img>s. Web standards owe $25k back to github. Yes, github's routing was flawed but it wasn't exploitable until browsers allowed HEAD in fetch() no-cors mode.
- lol768 7y agoPresumably a SameSite=Lax cookie wouldn't have helped here, because > "Safe" HTTP methods include "GET", "HEAD", "OPTIONS", and "TRACE", as defined in Section 4.2.1 of [RFC7231]. But I wonder if GitHub could issue a separate SameSite=Strict cookie here as a defense in depth measure to cause the initial part of the PoC (HEAD request with credentials: include) to fail?
- fanf2 7y agoI found a very similar bug in my code, though not in such a dangerous place! So thank you Teddy Katz and GitHub for helping me to fix a mistake in a less stressful way. This is why sharing detailed vulnerability reports is such a good thing.
- zaroth 7y agoWhat is it about OAuth’s design that provides such an incredibly diverse number of failure modes? I feel like it’s the most common critical vulnerability that crops up on every major site in one form or another.
- lightedman 7y ago"What is it about OAuth’s design that provides such an incredibly diverse number of failure modes?" A solid lack of understanding at all layers of the OSI model, especially the first three.
- SgtBastard 7y agoDid you mean the top 3 (Session-Application) not first 3 (Physical-Network)? This is deliciously ironic if not.
- est 7y agoOhhh, so that's how my github account was stolen. One day I received an email saying that a third party app was added to my github account, they in an instant it says my password has been changed, key was stripped, etc.
- lol768 7y agoIf the folks at GitHub are half-decent engineers (and everything I've seen suggests they know what they're doing), they'll have request logs that would make it very very obvious someone had done this. Would advise sending a support request in, if you think this happened to you.
- couchand 7y agoI don't believe it is possible to change your password with the GitHub API, and that action also requires reauthentication.
- danso 7y ago> So Rails (along with some other web frameworks) implements a clever hack: it tries to route HEAD requests to the same place as it would route GET requests. Then it runs the controller code, and just omits the response body. I wonder if Rails will fix the default implementation of handling HEAD requests, similar to how they overhauled the default handling of mass assignment of parameters in 2012 (coincidentally, a flaw also exposed via Github) [0]? Obviously, the latter issue posed a much bigger risk to the average Rails app. [0] https://news.ycombinator.com/item?id=3666564 https://news.ycombinator.com/item?id=3666564 edit: fixed URL
- 333c 7y agoYour link doesn't work.
- homakov 7y ago> I wonder if Rails will fix the default implementation of handling HEAD requests It absolutely should. Nobody expected Rails to covertly turn HEAD into GET. It must be explicit "get_or_head" method in routes.rb
- michaelmior 7y agoHEAD isn't turned into GET, it's routed the same as a GET request. If it were actually turned into a GET request, then `request.get?` would have been true, and this exploit wouldn't have happened. But I do agree with your point that routing of HEAD requests should be explicit.
- homakov 7y ago> HEAD isn't turned into GET, it's routed the same as a GET request Pardon my poor wording indeed, I intended to mean this too.
- ptoomey3 7y agoRails actually did turn the HEAD into a GET back when this code was written - https://github.com/rails/rails/blob/e17e25cd23e8abd45b1706463dd57c90fa6dcb7c/actionpack/lib/action_dispatch/middleware/head.rb#L8-L9 https://github.com/rails/rails/blob/e17e25cd23e8abd45b170646...
- kerng 7y agoIt took all of 8 minutes from the bug bounty hunter reporting, to Github confirming the finding. That's amazing.
- greenie_beans 7y agovery cool, thanks for sharing. i'm a junior dev who recently implemented oauth in a rails app and this is instructional. thanks
- oefrha 7y agoThis is why I hate it when people think there are N possible cases, and in a cascading if/else construct they only check for N-1 conditions and assume the remaining all fall within the Nth case. Thing is application logic in the real world is often fuzzy, it is entirely possible to overlook certain possibilities (which may or may not arise from bugs elsewhere); and even if you have considered all cases at present, future changes to other parts of the code may inadvertently introduce new possibilities. Personally, I always check for all N conditions, and raise an exception when "the impossible" happens; this way, the application would fail noisily instead of silently doing something unintended. However, sometimes other people would "optimize" away my last condition, which is very frustrating. Edit: Checking for all N cases helps with readability too. Otherwise one has to figure out what exactly is the remaining case by exclusion, which is sometimes not at all obvious.
- femto113 7y agoIn general I’d say testing the method in a controller is a smell,and in this particular case I’d suggest GET and POST should never have been handled by the same function. If GET did nothing but render the view sending a HEAD would be harmless.
- diminoten 7y agoYeah the "if GET" code raised my eyebrows a bit, I know rails can handle separate GET and POST funcs.
- icebraining 7y agoWhile you're not wrong, if I explicitly tell the framework to only accept GET and POST, I consider it a bug if any other method "gets through", particularly since the docs don't say that can happen.
- bobbyi_settv 7y agoA common reason people don't do it the way you're suggesting is dogma about code coverage. If the code has branches for each possible case, and then an "else" to cover anything else (which raises an exception indicating something impossible has happened), there is often no good way to write a test that covers the "else" logic, since the coder doesn't believe there's a way to end up going there. Or if there is a way to force it to happen, it involves jumping through hoops that are too much effort.
- nickjj 7y agoInteresting bug. It makes me appreciate working with languages that support pattern matching like Elixir. I think this type of bug would have been preventable with pattern matching because that single controller function would have likely been split out into 2 functions that each matched against a specific HTTP method, and when the unmatched pattern came into play (the HEAD request) it would have failed to find a match and then threw an error instead of processing it as something else.
- griffinmb 7y agoThis class of bug (CSRF bypass via route confusion) is probably more common in Phoenix apps. I’ve found a handful of apps vulnerable to this issue with Sobelow. People create (for example) a get ‘/profile’ and a post ‘/profile’, and the action intended to correspond with post requests really just pattern matches against params. I’ve also seen at least one app implement this properly, matching against the HTTP method as you described.
- nickjj 7y agoTo be safe from this in Phoenix that would look like this right? def profile(conn = %{method: "GET"}, params) do # ... end def profile(conn = %{method: "POST"}, %{"user" => user_params) do # ... end This would be in a case where your router looks like: get "/profile", UserController, :profile, as: :user post "/profile", UserController, :profile, as: :user That's what I'm doing in my code base at the moment. Mainly thanks to Changelog open sourcing their platform, and you can see that pattern being used here: https://github.com/thechangelog/changelog.com/blob/f9b0a758746f2cec10781b77eb0541cce765e054/lib/changelog_web/controllers/person_controller.ex#L90 https://github.com/thechangelog/changelog.com/blob/f9b0a7587... The above seems like the natural way to do it with Phoenix once you get a hang of pattern matching.
- griffinmb 7y agoYep, that’s the way!
- tptacek 7y agoWhat a fantastic bug. Rails routes HEAD to GET, but .get? in a HEAD handler is false. Github uses a postback to /authorize to complete OAuth, distinguishing between the original GET and the completing POST with .get?. Rails sees the HEAD request and assumes it's a POST (because it's not .get?) --- and also assumes that, because it's a POST, it's CSRF protected, which it is not, because it is not actually a POST.
- eganist 7y agoIt's bad assumptions all the way down. Also easy to bake into basic static analysis going forward. Knowing GitHub, I'd think a bug like this probably won't show up again in the near future.
- jacquesm 7y ago> It's bad assumptions all the way down. It always is. The moment you can shine light between your model of what the code does and what it actually does you have at least a bug and maybe much worse.
- ljm 7y agoIt also looks like a Rails bug, unless Github routed the HEAD requests themselves. In that case, it’s a shortcut that bit them in the ass.
- ryanisnan 7y agoRails is not assuming that it's a POST. The conditional logic in the controller explicitly checks for a get, and then else's otherwise. This is an unfortunate consequence of not having routing go to specific methods, e.g. `def post():`.
- tptacek 7y agoRails routing does go to specific methods ordinarily. What's funny here is, apparently, .get? used to return true for HEAD requests.
- londons_explore 7y agoAnd this folks is why you should always use separate sessions for different websites, and remember to log out of sites you aren't using.
- kureikain 7y agoThis is the reason I don't use else much and use explicitly matching. `if/else` is pretty much only use for boolean with exact 2 states. Not like this issue but the use of `if/else` can lead to thing like: loop do # do thing and increase i break if i == 10 end If i go from 9 to 11, we never exit the loop. In language like Erlang, when you write `if/else` it force you to handle all the cases with pattern matching which I think may help reduce amount of unhandled state check error.
- jacquesm 7y agoDefensive code would have you write 'break if i >= 10'.