This is a bad regex.
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$
Do it piecemeal.
([a-zA-Z]:)
This requires that the file path starts with a disk, such as C: d: etc.
(\\{2}\w+)\$?)
\\{2} means that the backslash is repeated twice (note that \ must be escaped), followed by some alphanumeric characters ( \w+ ), and then possibly a dollar sign ( \$? ). This is the main part of the UNC path.
([a-zA-Z]:)|(\\{2}\w+)\$?)
| means "or." Thus, it either starts with a drive letter, or from a UNC path. Congratulations to non-Windows users.
(\\(\w[\w].*))
This should be part of the path directory, but actually it is 2 alphanumerics followed by everything except newlines ( .* ), For example \ ab!@ #*(#$*) .
The correct regular expression for this part should be (?:\\\w+)+
(.jpg|.JPG|.gif|.GIF)$
This means that the last 3 characters of the path must be jpg , jpg , gif or gif . Please note that . is not a dot, but matches anything other than \n , so a file name like haha.abcgif or malicious.exe\0gif will pass.
The correct regular expression for this part should be \.(?:jpg|JPG|gif|GIF)$
Together
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$
will match
D:\foo.jpg \\remote$\dummy\..\C:\Windows\System32\Logo.gif C:\Windows\System32\cmd.exe;--gif
and fail
/home/user/pictures/myself.jpg C:\a.jpg C:\d\e.jpg
The correct regular expression is /\.(?:jpg|gif)$/i and check if the downloaded file is really a server-side image.
kennytm
source share