Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
0983beb72c18
Commit
0983beb7
authored
Jan 31, 2018
by
Douwe Maan
Browse files
Validate User username only on Namespace, and bubble up appropriately
parent
744ac38e166e
Changes
12
Hide whitespace changes
Inline
Side-by-side
app/models/user.rb
View file @
0983beb7
...
...
@@ -151,9 +151,7 @@
validates
:projects_limit
,
presence:
true
,
numericality:
{
greater_than_or_equal_to:
0
,
less_than_or_equal_to:
Gitlab
::
Database
::
MAX_INT_VALUE
}
validates
:username
,
user_path:
true
,
presence:
true
validates
:username
,
presence:
true
validates
:namespace
,
presence:
true
validate
:namespace_move_dir_allowed
,
if: :username_changed?
...
...
app/validators/abstract_path_validator.rb
View file @
0983beb7
...
...
@@ -13,10 +13,6 @@
raise
NotImplementedError
end
def
self
.
full_path
(
record
,
value
)
value
end
def
self
.
valid_path?
(
path
)
encode!
(
path
)
"
#{
path
}
/"
=~
path_regex
...
...
@@ -28,7 +24,7 @@
return
end
full_path
=
self
.
class
.
full_path
(
record
,
value
)
full_path
=
record
.
build_full_path
return
unless
full_path
unless
self
.
class
.
valid_path?
(
full_path
)
...
...
app/validators/namespace_path_validator.rb
View file @
0983beb7
...
...
@@ -12,8 +12,4 @@
def
self
.
format_error_message
Gitlab
::
PathRegex
.
namespace_format_message
end
def
self
.
full_path
(
record
,
value
)
record
.
build_full_path
end
end
app/validators/project_path_validator.rb
View file @
0983beb7
...
...
@@ -12,8 +12,4 @@
def
self
.
format_error_message
Gitlab
::
PathRegex
.
project_path_format_message
end
def
self
.
full_path
(
record
,
value
)
record
.
build_full_path
end
end
app/validators/user_path_validator.rb
deleted
100644 → 0
View file @
744ac38e
class
UserPathValidator
<
AbstractPathValidator
extend
Gitlab
::
EncodingHelper
def
self
.
path_regex
Gitlab
::
PathRegex
.
root_namespace_path_regex
end
def
self
.
format_regex
Gitlab
::
PathRegex
.
namespace_format_regex
end
def
self
.
format_error_message
Gitlab
::
PathRegex
.
namespace_format_message
end
end
lib/constraints/user_url_constrainer.rb
View file @
0983beb7
...
...
@@ -2,7 +2,7 @@
def
matches?
(
request
)
full_path
=
request
.
params
[
:username
]
return
false
unless
User
PathValidator
.
valid_path?
(
full_path
)
return
false
unless
Namespace
PathValidator
.
valid_path?
(
full_path
)
User
.
find_by_full_path
(
full_path
,
follow_redirects:
request
.
get?
).
present?
end
...
...
lib/gitlab/o_auth/user.rb
View file @
0983beb7
...
...
@@ -178,7 +178,7 @@
valid_username
=
::
Namespace
.
clean_path
(
username
)
uniquify
=
Uniquify
.
new
valid_username
=
uniquify
.
string
(
valid_username
)
{
|
s
|
!
User
PathValidator
.
valid_path?
(
s
)
}
valid_username
=
uniquify
.
string
(
valid_username
)
{
|
s
|
!
Namespace
PathValidator
.
valid_path?
(
s
)
}
name
=
auth_hash
.
name
name
=
valid_username
if
name
.
strip
.
empty?
...
...
lib/gitlab/path_regex.rb
View file @
0983beb7
...
...
@@ -171,11 +171,7 @@
@project_git_route_regex
||=
/
#{
project_route_regex
}
\.git/
.
freeze
end
def
root_namespace_path_regex
@root_namespace_path_regex
||=
%r{
\A
#{
root_namespace_route_regex
}
/
\z
}
end
def
full_namespace_path_regex
@full_namespace_path_regex
||=
%r{
\A
#{
full_namespace_route_regex
}
/
\z
}
end
...
...
@@ -178,12 +174,8 @@
def
full_namespace_path_regex
@full_namespace_path_regex
||=
%r{
\A
#{
full_namespace_route_regex
}
/
\z
}
end
def
project_path_regex
@project_path_regex
||=
%r{
\A
#{
project_route_regex
}
/
\z
}
end
def
full_project_path_regex
@full_project_path_regex
||=
%r{
\A
#{
full_namespace_route_regex
}
/
#{
project_route_regex
}
/
\z
}
end
...
...
@@ -186,11 +178,7 @@
def
full_project_path_regex
@full_project_path_regex
||=
%r{
\A
#{
full_namespace_route_regex
}
/
#{
project_route_regex
}
/
\z
}
end
def
full_namespace_format_regex
@namespace_format_regex
||=
/A
#{
FULL_NAMESPACE_FORMAT_REGEX
}
\z/
.
freeze
end
def
namespace_format_regex
@namespace_format_regex
||=
/\A
#{
NAMESPACE_FORMAT_REGEX
}
\z/
.
freeze
end
...
...
spec/lib/gitlab/path_regex_spec.rb
View file @
0983beb7
...
...
@@ -194,8 +194,8 @@
end
end
describe
'.root_namespace_
path
_regex'
do
subject
{
described_class
.
root_namespace_
path
_regex
}
describe
'.root_namespace_
route
_regex'
do
subject
{
%r{
\A
#{
described_class
.
root_namespace_
route
_regex
}
/
\z
}
}
it
'rejects top level routes'
do
expect
(
subject
).
not_to
match
(
'admin/'
)
...
...
@@ -318,8 +318,8 @@
end
end
describe
'.project_
path
_regex'
do
subject
{
described_class
.
project_
path
_regex
}
describe
'.project_
route
_regex'
do
subject
{
%r{
\A
#{
described_class
.
project_
route
_regex
}
/
\z
}
}
it
'accepts top level routes'
do
expect
(
subject
).
to
match
(
'admin/'
)
...
...
spec/models/user_spec.rb
View file @
0983beb7
...
...
@@ -140,7 +140,19 @@
user
=
build
(
:user
,
username:
username
)
expect
(
user
).
not_to
be_valid
expect
(
user
.
errors
.
messages
[
:"namespace.route.path"
].
first
).
to
eq
(
'foo has been taken before. Please use another one'
)
expect
(
user
.
errors
.
full_messages
).
to
eq
([
'Username has been taken before'
])
end
end
context
'when the username is in use by another user'
do
let
(
:username
)
{
'foo'
}
let!
(
:other_user
)
{
create
(
:user
,
username:
username
)
}
it
'is invalid'
do
user
=
build
(
:user
,
username:
username
)
expect
(
user
).
not_to
be_valid
expect
(
user
.
errors
.
full_messages
).
to
eq
([
'Username has already been taken'
])
end
end
end
...
...
@@ -2634,7 +2646,7 @@
it
'should raise an ActiveRecord::RecordInvalid exception'
do
user2
=
build
(
:user
,
username:
'foo'
)
expect
{
user2
.
save!
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
,
/
Namespace route path foo
has been taken before/
)
expect
{
user2
.
save!
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
,
/
Username
has been taken before/
)
end
end
...
...
spec/services/groups/transfer_service_spec.rb
View file @
0983beb7
...
...
@@ -177,7 +177,7 @@
it
'should add an error on group'
do
transfer_service
.
execute
(
new_parent_group
)
expect
(
transfer_service
.
error
).
to
eq
(
'Transfer failed: Validation failed:
Route p
ath has already been taken
, Route is invalid
'
)
expect
(
transfer_service
.
error
).
to
eq
(
'Transfer failed: Validation failed:
P
ath has already been taken'
)
end
end
...
...
spec/validators/user_path_validator_spec.rb
deleted
100644 → 0
View file @
744ac38e
require
'spec_helper'
describe
UserPathValidator
do
let
(
:validator
)
{
described_class
.
new
(
attributes:
[
:username
])
}
describe
'.valid_path?'
do
it
'handles invalid utf8'
do
expect
(
described_class
.
valid_path?
(
"a
\0
weird
\255
path"
)).
to
be_falsey
end
end
describe
'#validates_each'
do
it
'adds a message when the path is not in the correct format'
do
user
=
build
(
:user
)
validator
.
validate_each
(
user
,
:username
,
"Path with spaces, and comma's!"
)
expect
(
user
.
errors
[
:username
]).
to
include
(
Gitlab
::
PathRegex
.
namespace_format_message
)
end
it
'adds a message when the path is reserved when creating'
do
user
=
build
(
:user
,
username:
'help'
)
validator
.
validate_each
(
user
,
:username
,
'help'
)
expect
(
user
.
errors
[
:username
]).
to
include
(
'help is a reserved name'
)
end
it
'adds a message when the path is reserved when updating'
do
user
=
create
(
:user
)
user
.
username
=
'help'
validator
.
validate_each
(
user
,
:username
,
'help'
)
expect
(
user
.
errors
[
:username
]).
to
include
(
'help is a reserved name'
)
end
end
end
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment