summaryrefslogtreecommitdiff
path: root/lib/gitlab/logging/cloudflare_helper.rb
blob: 5cffe335bb55a59cc8b63aed9fc99cd5032f5a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

module Gitlab
  module Logging
    module CloudflareHelper
      CLOUDFLARE_CUSTOM_HEADERS = { 'Cf-Ray' => :cf_ray, 'Cf-Request-Id' => :cf_request_id }.freeze

      def store_cloudflare_headers!(payload, request)
        CLOUDFLARE_CUSTOM_HEADERS.each do |header, value|
          payload[value] = request.headers[header] if valid_cloudflare_header?(request.headers[header])
        end
      end

      def valid_cloudflare_header?(value)
        return false unless value.present?
        return false if value.length > 64
        return false if value.index(/[^[A-Za-z0-9-]]/)

        true
      end
    end
  end
end