forked from theforeman/foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #12698 - Insufficient URL validation Smart Proxy and Medium.
Problem: The regex that validates smart proxies URLs only matches 'beginning of text'. This allows us to add just \n after a valid URL and put anything after it. For instance, javascript:alert('hacked'). I haven't found any link to the Foreman proxy URL so the script would not trigger, but if we were to put link_to @smart_proxy.url somewhere (or a plugin did this) it would be unsafe. Same problem occurrs on Medium path. Solution: Make the regex match the end of the URL with \Z. I substituted the regex by an standard one, URI.regexp so we don't have to maintain it anymore. Extra: I added one test for this, but other tests have been rearranged to use stubs rather than building actual SmartProxy objects & associations.
- Loading branch information
Showing
6 changed files
with
97 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class UrlSchemaValidator < ActiveModel::EachValidator | ||
def initialize(args) | ||
@schemas = args[:in] | ||
super | ||
end | ||
|
||
def validate_each(record, attribute, value) | ||
unless value =~ /\A#{URI.regexp(@schemas)}\z/ | ||
error_message = _('URL must be valid and schema must be one of %s') % | ||
@schemas.to_sentence | ||
record.errors.add(attribute, error_message) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'test_helper' | ||
|
||
class UrlSchemaValidatorTest < ActiveSupport::TestCase | ||
class Validatable | ||
include ActiveModel::Validations | ||
validates :url, :url_schema => ['http', 'https', 'nfs', 'ftp'] | ||
attr_accessor :url | ||
end | ||
|
||
setup do | ||
@validatable = Validatable.new | ||
end | ||
|
||
test 'url regexp does not match new lines' do | ||
@validatable.url = "http://puppet.example.com:4568\njavascript('alert')" | ||
refute_valid @validatable | ||
end | ||
|
||
test 'passes if url uses one of the specified schemas' do | ||
@validatable.url = 'ftp://puppet.example.com:4568' | ||
assert_valid @validatable | ||
end | ||
|
||
test 'fails if url contains the wrong schema' do | ||
@validatable.url = 'unix://puppet.example.com:4568' | ||
refute_valid @validatable | ||
assert_match /URL must be valid/, @validatable.errors.messages.to_s | ||
end | ||
end |