Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36644 - Validate root_url before using it #9795

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class LinksController < ApplicationController
skip_before_action :require_login, :authorize, :session_expiry, :update_activity_time, :set_taxonomy, :set_gettext_locale_db, :only => :show
before_action :validate_root_url

def show
url = external_url(type: params[:type], options: params)
Expand Down Expand Up @@ -29,6 +30,16 @@ def external_url(type:, options: {})

private

def validate_root_url
unless params[:root_url].nil?
root_uri = URI.parse(params[:root_url])
unless SETTINGS[:trusted_redirect_domains].include?(root_uri.host) || SETTINGS[:trusted_redirect_domains].any? { |d| root_uri.host.end_with?(".#{d}") }
logger.warn "Denied access to forbidden root_url: #{params[:root_url]}"
not_found
end
end
end

def foreman_org_path(sub_path)
"https://theforeman.org/#{sub_path}"
end
Expand Down
2 changes: 2 additions & 0 deletions config/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

SETTINGS[:hosts] ||= []

SETTINGS[:trusted_redirect_domains] ||= ['theforeman.org', 'redhat.com', 'orcharhino.com'].freeze
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this even be frozen? idk.


# Load plugin config, if any
Dir["#{__dir__}/settings.plugins.d/*.yaml"].each do |f|
SETTINGS.merge! YAML.load(ERB.new(File.read(f)).result)
Expand Down
31 changes: 31 additions & 0 deletions test/controllers/links_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ class LinksControllerTest < ActionController::TestCase
assert_redirected_to /my_plugin/
end

test '#documentation_url receives an allowed root_url domain' do
['theforeman.org', 'redhat.com', 'orcharhino.com'].each do |domain|
evgeni marked this conversation as resolved.
Show resolved Hide resolved
get :show, params: {
type: 'manual',
root_url: "http://#{domain}",
}

assert_redirected_to /#{domain}/
end
end

test '#documentation_url receives an allowed root_url subdomain' do
['theforeman.org', 'redhat.com', 'orcharhino.com'].each do |domain|
get :show, params: {
type: 'manual',
root_url: "http://some-sub.#{domain}",
}

assert_redirected_to /some-sub.#{domain}/
end
end

test '#documentation_url receives a forbidden root_url option' do
get :show, params: {
type: 'manual',
root_url: 'http://www.example.invalid',
}

assert_response :not_found
end

test '#plugin_documentation_url returns foreman docs url for a plugin with a version and a given section' do
get :show, params: {
type: 'plugin_manual',
Expand Down