News for hackers. Stuff that matters.
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by sync on Monday February 11, 2013 @01:30PM (#5202337)
This one seems pretty amateur and should have been caught as part of a pull request/code review process.

   - @regex = /^(#{Regexp.escape(@prefix)})(.+?)(#{Regexp.escape(@suffix)})$/
   + @regex = /\A(#{Regexp.escape(@prefix)})(.+?)(#{Regexp.escape(@suffix)})\z/
^ and $ only match the first line in ruby, whereas \A and \z match across all lines.
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by heironimus on Monday February 11, 2013 @01:49PM (#5202470)
"This one seems pretty amateur and should have been caught as part of a pull request/code review process"

I say that about 95% of the bugs I ever see...especially ones written by me.

Re:ActiveRecord Vulnerability - Circumvention of attr_protected by getsat on Monday February 11, 2013 @01:41PM (#5202412)
More specifically, \A and \z are "start of string" and "end of string" respectively.

Why not \Z, you ask? \Z will match to the end of the string but ignore a trailing newline.

  irb(main):001:0> !!("hello\n" =~ /\Ahello\z/)
  => false
  irb(main):002:0> !!("hello\n" =~ /\Ahello\Z/)
  => true
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by tptacek on Monday February 11, 2013 @02:17PM (#5202657)
This is an extremely common bug that is not specific to Rails. It would be worth reviewing your code to look at every regex to see if you have similar flaws.
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by pjungwir on Monday February 11, 2013 @02:53PM (#5202849)
I seem to remember a blog post about this regex issue here on HN a few months ago. It definitely surprised me to learn that Ruby doesn't treat $ as end-of-string by default.
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by fxn on Monday February 11, 2013 @03:22PM (#5202984)
I blogged in http://advogato.org/person/fxn/diary/498.html some key differences between Perl and Ruby regexp flags (which includes this gotcha).
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by homakov on Monday February 11, 2013 @05:48PM (#5203900)
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by jscheel on Monday February 11, 2013 @02:46PM (#5202807)
I feel a bit dumb, but I never would have thought about this one. So, is this best practice for any kind of validation regex?
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by Groxx on Monday February 11, 2013 @04:34PM (#5203430)
Yes. Otherwise, say your username validation regex looks like /^[a-z0-9]+$/ (one which I see all the time). It's pretty simple for me to send this: "a\n☃" ("a\n<snowman>" if you can't see it) and it'll validate. I say "pretty simple" because you can do it in many browsers just by pasting text with a newline in it into a form field - it can even be done by accident, no malicious intent necessary.

In general, you may be better off avoiding regexes when you can, especially if it's security-sensitive. They're very useful, but they're very easy to get wrong, especially when they get complex. This case, for instance, looks like it would have been impossible if they checked if the attribute were in a list, instead of building a regex. It might be faster with a regex in this case, but for most people that's a (massively) premature optimization for (imperceptibly) small gain.

Re:ActiveRecord Vulnerability - Circumvention of attr_protected by pfortuny on Monday February 11, 2013 @02:32PM (#5202747)
This is what everybody says when studying mathematical proofs of theorems, especially the good ones. Obvious, isn't it?
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by mpk on Monday February 11, 2013 @04:00PM (#5203190)
That depends on how long it's been in the codebase. As this case is covered in the Rails security guide _and_ the Ruby security reviewer's guide I'd expect it to be quite old and now being found because it hasn't been properly audited before or indicate that the review process needs some work.
Re:ActiveRecord Vulnerability - Circumvention of attr_protected by tomjen3 on Monday February 11, 2013 @03:27PM (#5203005)
So basically this attack relies on putting a not-blacklisted identifier on the first line?

How do you then get rails to assign it to the correct (blacklisted) identifier?