This would work for preventing non-gist-github.com hosts, but I think we strayed away from this because it wouldn't prevent Bypass 2, where JS is injected via a raw gist link.
require'uri'defvalid?(link)uri=URI.parselinkreturnfalseifuri.scheme!='https'returnfalseifuri.userinforeturnfalseifuri.host!='gist.github.com'returnfalseifuri.port!=443# I think it has to be this if its https?returnfalseifuri.fragmentreturnfalseifuri.query# idk if old gist ids could be arbitrary chars,# but the 10 or so I looked at all seemed to be hexpath,gist=File.splituri.pathreturnfalseunlessgist&&gist.match?(/\A[0-9a-f]{32}\z/)path,user=File.splitpathreturnfalseunlessuser&&user.match?(/\A[a-zA-Z0-9\-]{1,39}\z/)path=='/'end
Plus, a bunch of test cases:
valid=[# suggested one"https://gist.github.com/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa",# max name size (based on the existing regex, I didn't verify that it matches Github's rules)"https://gist.github.com/aaaaaaaaaabbbbbbbbbbccccccccccddddddddd/4bb1682ce590dc42402b2edddbca7aaa",# min name size"https://gist.github.com/a/4bb1682ce590dc42402b2edddbca7aaa",# each of the gist id char values (seems to be hex)"https://gist.github.com/a/0123456789abcdef0123456789abcdef",# user can have a dash in their name"https://gist.github.com/quincy-larson/4bb1682ce590dc42402b2edddbca7aaa",# just for contrast with some of the tests below"https://gist.github.com/a/0000000000111111111122222222223a",]invalid=[# the same as the valid one, except it's http. I actually thought browsers# would refuse to make http requests from https sites, but either way, it# should be disallowed as it's MITMable"http://gist.github.com/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa",# host"https://evil.com/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa",# auth credentials"https://user:pass@gist.github.com/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa",# http port (not totally sure this matters)"https://gist.github.com:80/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa",# query"https://gist.github.com/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa?q",# fragment"https://gist.github.com/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa#f",# this one exceeds the max name size"https://gist.github.com/aaaaaaaaaabbbbbbbbbbccccccccccddddddddde/4bb1682ce590dc42402b2edddbca7aaa",# less than min name size"https://gist.github.com//4bb1682ce590dc42402b2edddbca7aaa",# invalid gist id chars (outside the hex range)"https://gist.github.com/a/0000000000111111111122222222223x",# quick test showed that GH was case sensitive about the hex in the gist id"https://gist.github.com/a/0000000000111111111122222222223A",# too few path segments"https://gist.github.com","https://gist.github.com/","https://gist.github.com/a",# too many path segments"https://gist.github.com/QuincyLarson/4bb1682ce590dc42402b2edddbca7aaa/4bb1682ce590dc42402b2edddbca7aaa",# these ones were used to bypass validity in the blog"//evil.com/script#gist.github.com","https://gist.github.com@evil.com/","https://gist.github.com/AntonyGarand/a8a0b4a36a040edc6051e888afce8fab/raw/4deb366ddaf0597e82fea808f7f4cb3ad763d98f/poc.js",]valid.all?{|link|valid?link}# => trueinvalid.none?{|link|valid?link}# => true
Nice finding Antony,
For your information, the latest commit was still exploitable :) here is the poc to bypass the regex : gist.github.com/n1nj4sec/9fc83e8bc... /../9fc83e8bc780e5c10739933ec3347460/raw/b46eef9822a00473f720680ed664873c3e20af9f/test.js" (the trick is to use /../)
and the fix implemented : github.com/thepracticaldev/dev.to/...
Great info and writeup! Thank you for sharing. I have to ask a few questions if you wouldnt mind answering. How much time did you spend on this? What is your primary motivation; curiosity, cash, just because? Was the meager $150 reward worth you efforts?
I found the initial XSS within 15 minutes, but the variations and bypasses took few hours.
The primary motivation is to make the internet more secure, and fun part of breaking websites. The challenges and the reward of having an alert is fun.
The 150$ reward is plenty, I'm doing this for fun, and I like this website, so having a reward is only a nice bonus.
Without the website being open source, I would have to perform a black box audit, and finding those vulnerabilities is definitely possible but might require more time.
Thanks for your awesome work. I promise we’ll keep upping the bug bounty program as we go, so keep up with the disclosures!
Is there a hackerone-style disclosure program?
@ben , isn't there an URI class or something like that in Ruby? I think it should handle parsing links much better than custom regex.
Probably you can slightly simplify the code using URI::regexp, is this what you mean?
Kinda. I think Ruby provides something like:
This would work for preventing non-
gist-github.comhosts, but I think we strayed away from this because it wouldn't prevent Bypass 2, where JS is injected via a raw gist link.We could do something like this:
I think with the regex though it would be redundant to check the host.
How about something like this?
Plus, a bunch of test cases:
Although, we should probably check whether you're allowed to have unicode in your username 🤔
Nice finding Antony,
For your information, the latest commit was still exploitable :) here is the poc to bypass the regex :
gist.github.com/n1nj4sec/9fc83e8bc... /../9fc83e8bc780e5c10739933ec3347460/raw/b46eef9822a00473f720680ed664873c3e20af9f/test.js" (the trick is to use /../)
and the fix implemented :
github.com/thepracticaldev/dev.to/...
This patch was also vulnerable ;)
As the regex ended with
$, we could bypass it with a newline, then/../../..+ raw gistgithub.com/thepracticaldev/dev.to/...
This was fixed by using
\Aand\Zinstead of^and$!Nice one!
What an excellent post!
I learned so much from this, and I'm not even a ruby developer!
Really informative and really, really well explained without going over the top with the geekness!
Well done!
Great info and writeup! Thank you for sharing. I have to ask a few questions if you wouldnt mind answering. How much time did you spend on this? What is your primary motivation; curiosity, cash, just because? Was the meager $150 reward worth you efforts?
I found the initial XSS within 15 minutes, but the variations and bypasses took few hours.
The primary motivation is to make the internet more secure, and fun part of breaking websites. The challenges and the reward of having an
alertis fun.The 150$ reward is plenty, I'm doing this for fun, and I like this website, so having a reward is only a nice bonus.
Nice! This is really awesome!
Great job Antony and thanks for the detailed explanation!
Oh wow, I was sweating as I read this! lol.
If dev.to was not open source, would you still be able to find this discovery? How much more effort? Using different approach? Thanks
Without the website being open source, I would have to perform a black box audit, and finding those vulnerabilities is definitely possible but might require more time.
Brilliant catch !!