Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b73f3ce5
Commit
b73f3ce5
authored
Sep 17, 2018
by
Nick Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow UrlValidator to work with attr_encrypted
parent
50cb6353
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
3 deletions
+26
-3
app/validators/url_validator.rb
app/validators/url_validator.rb
+11
-3
spec/validators/url_validator_spec.rb
spec/validators/url_validator_spec.rb
+15
-0
No files found.
app/validators/url_validator.rb
View file @
b73f3ce5
...
...
@@ -41,12 +41,13 @@ class UrlValidator < ActiveModel::EachValidator
def
validate_each
(
record
,
attribute
,
value
)
@record
=
record
if
value
.
present?
value
.
strip!
else
unless
value
.
present?
record
.
errors
.
add
(
attribute
,
'must be a valid URL'
)
return
end
value
=
strip_value!
(
record
,
attribute
,
value
)
Gitlab
::
UrlBlocker
.
validate!
(
value
,
blocker_args
)
rescue
Gitlab
::
UrlBlocker
::
BlockedUrlError
=>
e
record
.
errors
.
add
(
attribute
,
"is blocked:
#{
e
.
message
}
"
)
...
...
@@ -54,6 +55,13 @@ class UrlValidator < ActiveModel::EachValidator
private
def
strip_value!
(
record
,
attribute
,
value
)
new_value
=
value
.
strip
return
value
if
new_value
==
value
record
.
public_send
(
"
#{
attribute
}
="
,
new_value
)
# rubocop:disable GitlabSecurity/PublicSend
end
def
default_options
# By default the validator doesn't block any url based on the ip address
{
...
...
spec/validators/url_validator_spec.rb
View file @
b73f3ce5
...
...
@@ -24,6 +24,21 @@ describe UrlValidator do
expect
(
badge
.
errors
.
empty?
).
to
be
true
end
it
'strips urls'
do
badge
.
link_url
=
"
\n\r\n\n
https://127.0.0.1
\r\n\r\n\n\n\n
"
# It's unusual for a validator to modify its arguments. Some extensions,
# such as attr_encrypted, freeze the string to signal that modifications
# will not be persisted, so freeze this string to ensure the scheme is
# compatible with them.
badge
.
link_url
.
freeze
subject
expect
(
badge
.
errors
).
to
be_empty
expect
(
badge
.
link_url
).
to
eq
(
'https://127.0.0.1'
)
end
end
context
'when allow_localhost is set to false'
do
...
...
Write
Preview
Markdown
is supported
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