Skip to content

Commit 542c43e

Browse files
author
Morgan Haskel
committed
Improved user validation and munging
We want to make sure we are validating the entire user parameter (and validating it consistently between mysql_user and mysql_grant). Additionally, for munging we do not want to do anything that could truncate the username.
1 parent 1d82477 commit 542c43e

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

lib/puppet/type/mysql_grant.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def initialize(*args)
6565
# If at least one special char is used, string must be quoted
6666

6767
# http://stackoverflow.com/questions/8055727/negating-a-backreference-in-regular-expressions/8057827#8057827
68-
if matches = /^(['`"])((?!\1).)*\1@([\w%\.:\-]+)/.match(value)
68+
if matches = /^(['`"])((?!\1).)*\1@([\w%\.:\-\/]+)$/.match(value)
6969
user_part = matches[2]
7070
host_part = matches[3]
71-
elsif matches = /^([0-9a-zA-Z$_]*)@([\w%\.:\-]+)/.match(value)
71+
elsif matches = /^([0-9a-zA-Z$_]*)@([\w%\.:\-\/]+)$/.match(value)
7272
user_part = matches[1]
7373
host_part = matches[2]
7474
elsif matches = /^((?!['`"]).*[^0-9a-zA-Z$_].*)@(.+)$/.match(value)
@@ -87,6 +87,11 @@ def initialize(*args)
8787
end
8888
end
8989
end
90+
91+
munge do |value|
92+
matches = /^((['`"]?).*\2)@(.+)$/.match(value)
93+
"#{matches[1]}@#{matches[3].downcase}"
94+
end
9095
end
9196

9297
newproperty(:options, :array_matching => :all) do

lib/puppet/type/mysql_user.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# If at least one special char is used, string must be quoted
1515

1616
# http://stackoverflow.com/questions/8055727/negating-a-backreference-in-regular-expressions/8057827#8057827
17-
if matches = /^(['`"])((?:(?!\1).)*)\1@([\w%\.:\-]+)/.match(value)
17+
if matches = /^(['`"])((?:(?!\1).)*)\1@([\w%\.:\-\/]+)$/.match(value)
1818
user_part = matches[2]
1919
host_part = matches[3]
20-
elsif matches = /^([0-9a-zA-Z$_]*)@([\w%\.:\-]+)/.match(value)
20+
elsif matches = /^([0-9a-zA-Z$_]*)@([\w%\.:\-\/]+)$/.match(value)
2121
user_part = matches[1]
2222
host_part = matches[2]
2323
elsif matches = /^((?!['`"]).*[^0-9a-zA-Z$_].*)@(.+)$/.match(value)
@@ -38,7 +38,7 @@
3838
end
3939

4040
munge do |value|
41-
matches = /^((['`"]?).*\2)@([\w%\.:\-]+)/.match(value)
41+
matches = /^((['`"]?).*\2)@(.+)$/.match(value)
4242
"#{matches[1]}@#{matches[3].downcase}"
4343
end
4444
end

spec/unit/puppet/type/mysql_user_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
end
5252
end
5353

54+
context 'using foo@192.168.1.0/255.255.255.0' do
55+
before :each do
56+
@user = Puppet::Type.type(:mysql_user).new(:name => 'foo@192.168.1.0/255.255.255.0', :password_hash => 'pass')
57+
end
58+
59+
it 'should create the user with the netmask' do
60+
expect(@user[:name]).to eq('foo@192.168.1.0/255.255.255.0')
61+
end
62+
end
63+
5464
context 'using allo_wed$char@localhost' do
5565
before :each do
5666
@user = Puppet::Type.type(:mysql_user).new(:name => 'allo_wed$char@localhost', :password_hash => 'pass')

0 commit comments

Comments
 (0)