2021-09-09 17:09:36 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Routing
|
|
|
|
module PseudonymizationHelper
|
2021-10-20 14:12:31 -04:00
|
|
|
class MaskHelper
|
|
|
|
QUERY_PARAMS_TO_MASK = %w[
|
|
|
|
assignee_username
|
|
|
|
author_username
|
|
|
|
].freeze
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
def initialize(request_object, group, project)
|
|
|
|
@request = request_object
|
|
|
|
@group = group
|
|
|
|
@project = project
|
|
|
|
end
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
def mask_params
|
|
|
|
return default_root_url + @request.original_fullpath unless has_maskable_params?
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
masked_params = @request.path_parameters.to_h do |key, value|
|
|
|
|
case key
|
|
|
|
when :project_id
|
|
|
|
[key, "project#{@project&.id}"]
|
2021-11-03 05:10:11 -04:00
|
|
|
when :namespace_id, :group_id
|
2021-10-20 14:12:31 -04:00
|
|
|
namespace = @group || @project&.namespace
|
|
|
|
[key, "namespace#{namespace&.id}"]
|
|
|
|
when :id
|
|
|
|
[key, mask_id(value)]
|
|
|
|
else
|
|
|
|
[key, value]
|
|
|
|
end
|
|
|
|
end
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-11-10 07:10:12 -05:00
|
|
|
Gitlab::Routing.url_helpers.url_for(masked_params.merge(params: masked_query_params))
|
2021-10-20 14:12:31 -04:00
|
|
|
end
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
private
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
def mask_id(value)
|
|
|
|
if @request.path_parameters[:controller] == 'projects/blob'
|
|
|
|
':repository_path'
|
|
|
|
elsif @request.path_parameters[:controller] == 'projects'
|
|
|
|
"project#{@project&.id}"
|
|
|
|
elsif @request.path_parameters[:controller] == 'groups'
|
|
|
|
"namespace#{@group&.id}"
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
def has_maskable_params?
|
|
|
|
request_params = @request.path_parameters.to_h
|
2021-11-03 05:10:11 -04:00
|
|
|
request_params.key?(:namespace_id) || request_params.key?(:group_id) || request_params.key?(:project_id) || request_params.key?(:id) || @request.query_string.present?
|
2021-10-20 14:12:31 -04:00
|
|
|
end
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
def masked_query_params
|
|
|
|
return {} unless @request.query_string.present?
|
2021-09-17 14:11:44 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
query_string_hash = Rack::Utils.parse_nested_query(@request.query_string)
|
|
|
|
|
|
|
|
QUERY_PARAMS_TO_MASK.each do |maskable_attribute|
|
|
|
|
next unless query_string_hash.has_key?(maskable_attribute)
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
query_string_hash[maskable_attribute] = "masked_#{maskable_attribute}"
|
|
|
|
end
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
query_string_hash
|
2021-09-09 17:09:36 -04:00
|
|
|
end
|
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
def default_root_url
|
|
|
|
Gitlab::Routing.url_helpers.root_url(only_path: false)
|
2021-09-09 17:09:36 -04:00
|
|
|
end
|
2021-10-20 14:12:31 -04:00
|
|
|
end
|
2021-09-09 17:09:36 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
def masked_page_url
|
|
|
|
return unless Feature.enabled?(:mask_page_urls, type: :ops)
|
2021-09-17 14:11:44 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
current_group = group if defined?(group)
|
|
|
|
current_project = project if defined?(project)
|
|
|
|
mask_helper = MaskHelper.new(request, current_group, current_project)
|
|
|
|
mask_helper.mask_params
|
2021-11-03 05:10:11 -04:00
|
|
|
|
|
|
|
# We rescue all exception for time being till we test this helper extensively.
|
|
|
|
# Check https://gitlab.com/gitlab-org/gitlab/-/merge_requests/72864#note_711515501
|
|
|
|
rescue => e # rubocop:disable Style/RescueStandardError
|
2021-10-20 14:12:31 -04:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, url: request.original_fullpath)
|
|
|
|
nil
|
2021-09-09 17:09:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|