Skip to content

Commit 4f0d431

Browse files
committed
Fix regression in username validation
Commit cdd7132 added logic to catch invalid database usernames, but the regex it uses fails to match usernames with special characters that are properly quoted, causing errors with usernames that used to work in versions < 3.0.0. This fixes the regex so that if the username is quoted, anything is allowed between the quotes. From the docs (http://dev.mysql.com/doc/refman/5.5/en/identifiers.html): "Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000"
1 parent 89762a7 commit 4f0d431

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

lib/puppet/type/mysql_user.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
# Regex should problably be more like this: /^[`'"]?[^`'"]*[`'"]?@[`'"]?[\w%\.]+[`'"]?$/
1414
# If at least one special char is used, string must be quoted
1515
raise(ArgumentError, "Database user #{value} must be quotted as it contains special characters") if value =~ /^[^'`"].*[^0-9a-zA-Z$_].*[^'`"]@[\w%\.:]+/
16-
# If no special char, quoted is not needed, but allowed
17-
# I don't see any case where this could happen, as it should be covered by previous check
18-
raise(ArgumentError, "Invalid database user #{value}") unless value =~ /^['`"]?[0-9a-zA-Z$_]*['`"]?@[\w%\.:]+/
16+
raise(ArgumentError, "Invalid database user #{value}") unless value =~ /^(?:['`"][^'`"]*['`"]|[0-9a-zA-Z$_]*)@[\w%\.:]+/
1917
username = value.split('@')[0]
2018
if username.size > 16
2119
raise ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters'

spec/unit/puppet/type/mysql_user_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
end
5050
end
5151

52+
context 'using `speci!al#`@localhost' do
53+
before :each do
54+
@user = Puppet::Type.type(:mysql_user).new(:name => '`speci!al#`@localhost', :password_hash => 'pass')
55+
end
56+
57+
it 'should accept a quoted user name with special chatracters' do
58+
expect(@user[:name]).to eq('`speci!al#`@localhost')
59+
end
60+
end
61+
5262
context 'using in-valid@localhost' do
5363
it 'should fail with an unquotted username with special char' do
5464
expect {

0 commit comments

Comments
 (0)