|
10 | 10 | desc "The name of the user. This uses the 'username@hostname' or username@hostname."
|
11 | 11 | validate do |value|
|
12 | 12 | # http://dev.mysql.com/doc/refman/5.5/en/identifiers.html
|
13 |
| - # Regex should problably be more like this: /^[`'"]?[^`'"]*[`'"]?@[`'"]?[\w%\.]+[`'"]?$/ |
14 | 13 | # If at least one special char is used, string must be quoted
|
15 |
| - raise(ArgumentError, "Database user #{value} must be quotted as it contains special characters") if value =~ /^[^'`"].*[^0-9a-zA-Z$_].*[^'`"]@[\w%\.:]+/ |
16 |
| - raise(ArgumentError, "Invalid database user #{value}") unless value =~ /^(?:['`"][^'`"]*['`"]|[0-9a-zA-Z$_]*)@[\w%\.:]+/ |
17 |
| - username = value.split('@')[0] |
18 |
| - if not ((username =~ /['"`]*['"`]$/ and username.size <= 18) or username.size <= 16) |
19 |
| - raise ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters' |
| 14 | + |
| 15 | + # http://stackoverflow.com/questions/8055727/negating-a-backreference-in-regular-expressions/8057827#8057827 |
| 16 | + if matches = /^(['`"])((?:(?!\1).)+)\1@([\w%\.:]+)$/.match(value) |
| 17 | + user_part = matches[2] |
| 18 | + host_part = matches[3] |
| 19 | + elsif matches = /^([0-9a-zA-Z$_]+)@([\w%\.:]+)$/.match(value) |
| 20 | + user_part = matches[1] |
| 21 | + host_part = matches[2] |
| 22 | + elsif matches = /^(?:(?!['`"]).*)([^0-9a-zA-Z$_]).*@.+$/.match(value) |
| 23 | + # does not start with a quote, but contains a special character |
| 24 | + raise(ArgumentError, "Database user #{value} must be properly quoted, invalid character: '#{matches[1]}'") |
| 25 | + else |
| 26 | + raise(ArgumentError, "Invalid database user #{value}") |
20 | 27 | end
|
| 28 | + |
| 29 | + raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters') if user_part.size > 16 |
21 | 30 | end
|
22 | 31 |
|
23 | 32 | munge do |value|
|
24 |
| - user_part, host_part = value.split('@') |
25 |
| - "#{user_part}@#{host_part.downcase}" |
| 33 | + matches = /^((['`"]?).+\2)@([\w%\.:]+)$/.match(value) |
| 34 | + "#{matches[1]}@#{matches[3].downcase}" |
26 | 35 | end
|
27 | 36 | end
|
28 | 37 |
|
|
0 commit comments