summaryrefslogtreecommitdiff
path: root/spec/ruby/security/cve_2018_6914_spec.rb
blob: 997f4c548afbae1f9a7028ca44b1a9b42c67b9c5 (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
80
81
require_relative '../spec_helper'

require 'tempfile'
require 'tmpdir'

describe "CVE-2018-6914 is resisted by" do
  before :each do
    @tmpdir = ENV['TMPDIR']
    @dir = tmp("CVE-2018-6914")
    Dir.mkdir(@dir)
    ENV['TMPDIR'] = @dir

    # Make sure that ENV["TMPDIR"] is used by Dir.tmpdir
    # https://github.com/ruby/ruby/runs/294462511#step:10:134
    10.times do
      break if Dir.tmpdir == File.expand_path(@dir)
      sleep 0.1
    end

    @debug_print = ->(actual) {
      PP.pp({
        actual: actual,
        absolute: File.absolute_path(actual),
        dir: @dir,
        pwd: Dir.pwd,
        tmpdir: @tmpdir,
        Dir_tmpdir: Dir.tmpdir,
        TMPDIR: ENV['TMPDIR'],
        stat: File.stat(@dir),
      }, STDERR)
    }

    @dir << '/'

    @tempfile = nil
  end

  after :each do
    ENV['TMPDIR'] = @tmpdir
    @tempfile.close! if @tempfile
    rm_r @dir
  end

  it "Tempfile.open by deleting separators" do
    @tempfile = Tempfile.open(['../', 'foo'])
    actual = @tempfile.path
    @debug_print.call(actual)
    File.absolute_path(actual).should.start_with?(@dir)
  end

  it "Tempfile.new by deleting separators" do
    @tempfile = Tempfile.new('../foo')
    actual = @tempfile.path
    @debug_print.call(actual)
    File.absolute_path(actual).should.start_with?(@dir)
  end

  it "Tempfile.create by deleting separators" do
    actual = Tempfile.create('../foo') do |t|
      t.path
    end
    @debug_print.call(actual)
    File.absolute_path(actual).should.start_with?(@dir)
  end

  it "Dir.mktmpdir by deleting separators" do
    actual = Dir.mktmpdir('../foo') do |path|
      path
    end
    @debug_print.call(actual)
    File.absolute_path(actual).should.start_with?(@dir)
  end

  it "Dir.mktmpdir with an array by deleting separators" do
    actual = Dir.mktmpdir(['../', 'foo']) do |path|
      path
    end
    @debug_print.call(actual)
    File.absolute_path(actual).should.start_with?(@dir)
  end
end