From 4f0d4311d9e177efe7bae5a57f58280eae893a91 Mon Sep 17 00:00:00 2001 From: Mason Malone Date: Thu, 13 Nov 2014 18:18:02 -0500 Subject: [PATCH] Fix regression in username validation Commit cdd7132ff9c3a8ac63621899f1e82bdd0ec58974 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" --- lib/puppet/type/mysql_user.rb | 4 +--- spec/unit/puppet/type/mysql_user_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/puppet/type/mysql_user.rb b/lib/puppet/type/mysql_user.rb index f9041de23..72b672e48 100644 --- a/lib/puppet/type/mysql_user.rb +++ b/lib/puppet/type/mysql_user.rb @@ -13,9 +13,7 @@ # Regex should problably be more like this: /^[`'"]?[^`'"]*[`'"]?@[`'"]?[\w%\.]+[`'"]?$/ # If at least one special char is used, string must be quoted raise(ArgumentError, "Database user #{value} must be quotted as it contains special characters") if value =~ /^[^'`"].*[^0-9a-zA-Z$_].*[^'`"]@[\w%\.:]+/ - # If no special char, quoted is not needed, but allowed - # I don't see any case where this could happen, as it should be covered by previous check - raise(ArgumentError, "Invalid database user #{value}") unless value =~ /^['`"]?[0-9a-zA-Z$_]*['`"]?@[\w%\.:]+/ + raise(ArgumentError, "Invalid database user #{value}") unless value =~ /^(?:['`"][^'`"]*['`"]|[0-9a-zA-Z$_]*)@[\w%\.:]+/ username = value.split('@')[0] if username.size > 16 raise ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters' diff --git a/spec/unit/puppet/type/mysql_user_spec.rb b/spec/unit/puppet/type/mysql_user_spec.rb index 94a8817d6..25ad03201 100644 --- a/spec/unit/puppet/type/mysql_user_spec.rb +++ b/spec/unit/puppet/type/mysql_user_spec.rb @@ -49,6 +49,16 @@ end end + context 'using `speci!al#`@localhost' do + before :each do + @user = Puppet::Type.type(:mysql_user).new(:name => '`speci!al#`@localhost', :password_hash => 'pass') + end + + it 'should accept a quoted user name with special chatracters' do + expect(@user[:name]).to eq('`speci!al#`@localhost') + end + end + context 'using in-valid@localhost' do it 'should fail with an unquotted username with special char' do expect {