Leon's Blogging

Coding blogging for hackers.

Ruby - Regular Expressions Security

| Comments

之前在做 code review 發現的一個問題,平常沒注意到很容易忽略!

^ and $ are the start and end of line anchors

1
2
# url 的 regular expressions
/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/?.*)?$)/ix

原因在於 ^ and $ 會根據每一行去判斷,因此像以下

1
2
3
javascript:exploit_code();/* # injection
http://hi.com # pass
*/

第二行會 pass,因此造成 injection 問題,改成以下

\A and \Z are the start and end of string anchors

1
\A(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/?.*)?$)\z

就會判斷一整串

參考文件

Comments