Welcome to mirror list, hosted at ThFree Co, Russian Federation.

decompressed_gzip_size_validator.rb « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a92f30076716d411cb583d6d46a7ca7c724112c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

module Gitlab
  module Ci
    class DecompressedGzipSizeValidator
      DEFAULT_MAX_BYTES = 4.gigabytes.freeze
      TIMEOUT_LIMIT = 210.seconds

      ServiceError = Class.new(StandardError)

      def initialize(archive_path:, max_bytes: DEFAULT_MAX_BYTES)
        @archive_path = archive_path
        @max_bytes = max_bytes
      end

      def valid?
        validate
      end

      private

      def validate
        pgrps = nil
        valid_archive = true

        validate_archive_path

        Timeout.timeout(TIMEOUT_LIMIT) do
          stderr_r, stderr_w = IO.pipe
          stdout, wait_threads = Open3.pipeline_r(*command, pgroup: true, err: stderr_w)

          # When validation is performed on a small archive (e.g. 100 bytes)
          # `wait_thr` finishes before we can get process group id. Do not
          # raise exception in this scenario.
          pgrps = wait_threads.map do |wait_thr|
            Process.getpgid(wait_thr[:pid])
          rescue Errno::ESRCH
            nil
          end
          pgrps.compact!

          status = wait_threads.last.value

          if status.success?
            result = stdout.readline

            valid_archive = false if result.to_i > max_bytes
          else
            valid_archive = false
          end

        ensure
          stdout.close
          stderr_w.close
          stderr_r.close
        end

        valid_archive
      rescue StandardError
        pgrps.each { |pgrp| Process.kill(-1, pgrp) } if pgrps

        false
      end

      def validate_archive_path
        Gitlab::Utils.check_path_traversal!(archive_path)

        raise(ServiceError, 'Archive path is a symlink') if File.lstat(archive_path).symlink?
        raise(ServiceError, 'Archive path is not a file') unless File.file?(archive_path)
      end

      def command
        [['gzip', '-dc', archive_path], ['wc', '-c']]
      end

      attr_reader :archive_path, :max_bytes
    end
  end
end