Module: Routing::PseudonymizationHelper

Included in:
GitlabRoutingHelper
Defined in:
app/helpers/routing/pseudonymization_helper.rb

Defined Under Namespace

Classes: MaskHelper

Constant Summary collapse

PSEUDONOMIZED_NAMESPACE =
"namespace"
PSEUDONOMIZED_PROJECT =
"project"
PSEUDONOMIZED_USERNAME =
"username"
PSEUDONOMIZED_GROUP =
"group"
PSEUDONOMIZED_ID =
"id"

Instance Method Summary collapse

Instance Method Details

#masked_page_url(group:, project:) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/helpers/routing/pseudonymization_helper.rb', line 97

def masked_page_url(group:, project:)
  # Disabling of page url masking is only available when Snowplow is configured.
  return if Gitlab::CurrentSettings.snowplow_enabled? && Feature.disabled?(:mask_page_urls, type: :ops)

  mask_helper = MaskHelper.new(request, group, project)
  mask_helper.mask_params

# 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
  Gitlab::ErrorTracking.track_exception(e, url: request.original_fullpath)
  nil
end

#masked_query_params(uri) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/helpers/routing/pseudonymization_helper.rb', line 145

def masked_query_params(uri)
  query_params = CGI.parse(uri.query.to_s)
  query_params.transform_keys!(&:downcase)

  return if query_params.empty?

  query_params.each do |key, _|
    query_params[key] = ["masked_#{key}"] unless MaskHelper::QUERY_PARAMS_TO_NOT_MASK.include?(key)
  end

  query_params
end

#masked_referrer_url(url) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/helpers/routing/pseudonymization_helper.rb', line 111

def masked_referrer_url(url)
  return unless url

  params = referrer_params(url)

  return unless params && params[:controller]
  return if params[:action] == "route_not_found"

  original_id = params[:id]

  case params[:controller]
  when 'groups'
    params[:id] = PSEUDONOMIZED_NAMESPACE
  when 'projects'
    params[:id] = PSEUDONOMIZED_PROJECT
  when 'users'
    params[:username] = PSEUDONOMIZED_USERNAME
  else
    params[:id] = PSEUDONOMIZED_ID if params[:id]
  end

  params[:project_id] = PSEUDONOMIZED_PROJECT if params[:project_id]
  params[:group_id] = PSEUDONOMIZED_GROUP if params[:group_id]
  params[:namespace_id] = PSEUDONOMIZED_NAMESPACE if params[:namespace_id]

  masked_query_params = masked_query_params(URI.parse(url))

  Gitlab::Routing.url_helpers.url_for(params.merge(params: masked_query_params))
rescue ActionController::UrlGenerationError
  # If URL cannot be constructed with placeholder, use original ID
  params[:id] = original_id
  Gitlab::Routing.url_helpers.url_for(params.merge(params: masked_query_params))
end

#referrer_params(url) ⇒ Object



158
159
160
161
162
163
# File 'app/helpers/routing/pseudonymization_helper.rb', line 158

def referrer_params(url)
  Rails.application.routes.recognize_path(url)
rescue StandardError => e
  Gitlab::ErrorTracking.track_exception(e, url: request.original_fullpath)
  nil
end