Skip to content

Commit 5b3ed93

Browse files
authored
Merge pull request #61 from utopia-php/feat-keys-no-leading-special-chars
Disallow leading special characters for Keys
2 parents 16abc6a + abdcc34 commit 5b3ed93

3 files changed

Lines changed: 21 additions & 17 deletions

File tree

src/Database/Validator/Key.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Key extends Validator
99
/**
1010
* @var string
1111
*/
12-
protected $message = 'Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore';
12+
protected $message = 'Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char';
1313

1414
/**
1515
* Get Description.
@@ -38,8 +38,9 @@ public function isValid($value)
3838
return false;
3939
}
4040

41-
// no leading underscores
42-
if(mb_substr($value, 0, 1) === '_') {
41+
// no leading special characters
42+
$leading = \mb_substr($value, 0, 1);
43+
if($leading === '_' || $leading === '.' || $leading === '-') {
4344
return false;
4445
}
4546

tests/Database/Validator/KeyTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ public function testValues()
3232
$this->assertEquals(true, $this->object->isValid('asdas7as9as'));
3333
$this->assertEquals(true, $this->object->isValid('5f058a8925807'));
3434

35-
// No leading underscore
35+
// No leading special chars
3636
$this->assertEquals(false, $this->object->isValid('_asdasdasdas'));
37-
$this->assertEquals(true, $this->object->isValid('a_sdasdasdas'));
37+
$this->assertEquals(false, $this->object->isValid('.as5dasdasdas'));
38+
$this->assertEquals(false, $this->object->isValid('-as5dasdasdas'));
3839

3940
// Special chars allowed: underscore, period, hyphen
4041
$this->assertEquals(true, $this->object->isValid('as5dadasdas_'));
4142
$this->assertEquals(true, $this->object->isValid('as_5dasdasdas'));
42-
$this->assertEquals(true, $this->object->isValid('.as5dasdasdas'));
4343
$this->assertEquals(true, $this->object->isValid('as5dasdasdas.'));
4444
$this->assertEquals(true, $this->object->isValid('as.5dasdasdas'));
45-
$this->assertEquals(true, $this->object->isValid('-as5dasdasdas'));
4645
$this->assertEquals(true, $this->object->isValid('as5dasdasdas-'));
4746
$this->assertEquals(true, $this->object->isValid('as-5dasdasdas'));
4847

tests/Database/Validator/PermissionsTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,26 @@ public function testInvalidPermissions()
9797
$this->assertEquals($object->getDescription(), 'Permission roles must be one of: all, guest, member');
9898

9999
// team:$value, member:$value and user:$value must have valid Key for $value
100-
// No leading underscores
100+
// No leading special chars
101101
$this->assertEquals($object->isValid(['member:_1234']), false);
102-
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
102+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
103+
$this->assertEquals($object->isValid(['member:-1234']), false);
104+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
105+
$this->assertEquals($object->isValid(['member:.1234']), false);
106+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
103107

104-
// No special characters
108+
// No unsupported special characters
105109
$this->assertEquals($object->isValid(['member:12$4']), false);
106-
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
110+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
107111
$this->assertEquals($object->isValid(['user:12&4']), false);
108-
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
112+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
109113
$this->assertEquals($object->isValid(['member:ab(124']), false);
110-
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
114+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
111115

112116
// Shorter than 36 chars
113117
$this->assertEquals($object->isValid(['user:aaaaaaaabbbbbbbbccccccccddddddddeeee']), true);
114118
$this->assertEquals($object->isValid(['user:aaaaaaaabbbbbbbbccccccccddddddddeeeee']), false);
115-
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
119+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
116120

117121
// Permission role must begin with one of: member, role, team, user
118122
$this->assertEquals($object->isValid(['memmber:1234']), false);
@@ -126,7 +130,7 @@ public function testInvalidPermissions()
126130

127131
// Team permission
128132
$this->assertEquals($object->isValid(['team:_abcd']), false);
129-
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
133+
$this->assertEquals($object->getDescription(), '[role:$id] $id must be a valid key: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
130134
$this->assertEquals($object->isValid(['team:abcd/']), false);
131135
$this->assertEquals($object->getDescription(), 'Team role must not be empty.');
132136
$this->assertEquals($object->isValid(['team:/abcd']), false);
@@ -136,8 +140,8 @@ public function testInvalidPermissions()
136140
$this->assertEquals($object->isValid(['team:abcd/e/fgh']), false);
137141
$this->assertEquals($object->getDescription(), 'Permission roles may contain at most one "/" character.');
138142
$this->assertEquals($object->isValid(['team:ab&cd3/efgh']), false);
139-
$this->assertEquals($object->getDescription(), '[team:$teamId/$role] $teamID and $role must be valid keys: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
143+
$this->assertEquals($object->getDescription(), '[team:$teamId/$role] $teamID and $role must be valid keys: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
140144
$this->assertEquals($object->isValid(['team:abcd/ef*gh']), false);
141-
$this->assertEquals($object->getDescription(), '[team:$teamId/$role] $teamID and $role must be valid keys: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a leading underscore');
145+
$this->assertEquals($object->getDescription(), '[team:$teamId/$role] $teamID and $role must be valid keys: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char');
142146
}
143147
}

0 commit comments

Comments
 (0)