Project

General

Profile

Bug #13842 ยป rubygems-2613-ruby22.patch

hsbt (Hiroshi SHIBATA), 08/28/2017 08:03 AM

View differences:

lib/rubygems.rb
9 9
require 'thread'
10 10

  
11 11
module Gem
12
  VERSION = '2.4.5.2'
12
  VERSION = '2.4.5.3'
13 13
end
14 14

  
15 15
# Must be first since it unloads the prelude from 1.9.2
lib/rubygems/commands/query_command.rb
218 218
        end
219 219
      end
220 220

  
221
      output << make_entry(matching_tuples, platforms)
221
      output << clean_text(make_entry(matching_tuples, platforms))
222 222
    end
223 223
  end
224 224

  
......
336 336
  end
337 337

  
338 338
  def spec_summary entry, spec
339
    entry << "\n\n" << format_text(spec.summary, 68, 4)
339
    summary = truncate_text(spec.summary, "the summary for #{spec.full_name}")
340
    entry << "\n\n" << format_text(summary, 68, 4)
340 341
  end
341 342

  
342 343
end
lib/rubygems/installer.rb
646 646
      unpack or File.writable?(gem_home)
647 647
  end
648 648

  
649
  def verify_spec_name
650
    return if spec.name =~ Gem::Specification::VALID_NAME_PATTERN
651
    raise Gem::InstallError, "#{spec} has an invalid name"
652
  end
653

  
649 654
  ##
650 655
  # Return the text for an application file.
651 656

  
......
771 776

  
772 777
    ensure_loadable_spec
773 778

  
779
    verify_spec_name
780

  
774 781
    if options[:install_as_default]
775 782
      Gem.ensure_default_gem_subdirectories gem_home
776 783
    else
lib/rubygems/remote_fetcher.rb
96 96
    else
97 97
      target = res.target.to_s.strip
98 98

  
99
      if /\.#{Regexp.quote(host)}\z/ =~ target
99
      if URI("http://" + target).host.end_with?(".#{host}")
100 100
        return URI.parse "#{uri.scheme}://#{target}#{uri.path}"
101 101
      end
102 102

  
lib/rubygems/specification.rb
106 106

  
107 107
  private_constant :LOAD_CACHE if defined? private_constant
108 108

  
109
  VALID_NAME_PATTERN = /\A[a-zA-Z0-9\.\-\_]+\z/ # :nodoc:
110

  
109 111
  # :startdoc:
110 112

  
111 113
  ##
......
2477 2479
      end
2478 2480
    end
2479 2481

  
2480
    unless String === name then
2482
    if !name.is_a?(String) then
2481 2483
      raise Gem::InvalidSpecificationException,
2482
            "invalid value for attribute name: \"#{name.inspect}\""
2484
            "invalid value for attribute name: \"#{name.inspect}\" must be a string"
2485
    elsif name !~ /[a-zA-Z]/ then
2486
      raise Gem::InvalidSpecificationException,
2487
            "invalid value for attribute name: #{name.dump} must include at least one letter"
2488
    elsif name !~ VALID_NAME_PATTERN then
2489
      raise Gem::InvalidSpecificationException,
2490
            "invalid value for attribute name: #{name.dump} can only include letters, numbers, dashes, and underscores"
2483 2491
    end
2484 2492

  
2485 2493
    if raw_require_paths.empty? then
lib/rubygems/text.rb
5 5

  
6 6
module Gem::Text
7 7

  
8
  ##
9
  # Remove any non-printable characters and make the text suitable for
10
  # printing.
11
  def clean_text(text)
12
    text.gsub(/[\000-\b\v-\f\016-\037\177]/, ".".freeze)
13
  end
14

  
15
  def truncate_text(text, description, max_length = 100_000)
16
    raise ArgumentError, "max_length must be positive" unless max_length > 0
17
    return text if text.size <= max_length
18
    "Truncating #{description} to #{max_length.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse} characters:\n" + text[0, max_length]
19
  end
20

  
8 21
  ##
9 22
  # Wraps +text+ to +wrap+ characters and optionally indents by +indent+
10 23
  # characters
11 24

  
12 25
  def format_text(text, wrap, indent=0)
13 26
    result = []
14
    work = text.dup
27
    work = clean_text(text)
15 28

  
16 29
    while work.length > wrap do
17 30
      if work =~ /^(.{0,#{wrap}})[ \n]/ then
test/rubygems/test_gem_commands_query_command.rb
147 147
    This is a lot of text. This is a lot of text. This is a lot of text.
148 148
    This is a lot of text.
149 149

  
150
pl (1)
151
    Platform: i386-linux
152
    Author: A User
153
    Homepage: http://example.com
154

  
155
    this is a summary
156
    EOF
157

  
158
    assert_equal expected, @ui.output
159
    assert_equal '', @ui.error
160
  end
161

  
162
  def test_execute_details_cleans_text
163
    spec_fetcher do |fetcher|
164
      fetcher.spec 'a', 2 do |s|
165
        s.summary = 'This is a lot of text. ' * 4
166
        s.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
167
        s.homepage = "http://a.example.com/\x03"
168
      end
169

  
170
      fetcher.legacy_platform
171
    end
172

  
173
    @cmd.handle_options %w[-r -d]
174

  
175
    use_ui @ui do
176
      @cmd.execute
177
    end
178

  
179
    expected = <<-EOF
180

  
181
*** REMOTE GEMS ***
182

  
183
a (2)
184
    Authors: Abraham Lincoln ., . Hirohito
185
    Homepage: http://a.example.com/.
186

  
187
    This is a lot of text. This is a lot of text. This is a lot of text.
188
    This is a lot of text.
189

  
190
pl (1)
191
    Platform: i386-linux
192
    Author: A User
193
    Homepage: http://example.com
194

  
195
    this is a summary
196
    EOF
197

  
198
    assert_equal expected, @ui.output
199
    assert_equal '', @ui.error
200
  end
201

  
202
  def test_execute_details_truncates_summary
203
    spec_fetcher do |fetcher|
204
      fetcher.spec 'a', 2 do |s|
205
        s.summary = 'This is a lot of text. ' * 10_000
206
        s.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
207
        s.homepage = "http://a.example.com/\x03"
208
      end
209

  
210
      fetcher.legacy_platform
211
    end
212

  
213
    @cmd.handle_options %w[-r -d]
214

  
215
    use_ui @ui do
216
      @cmd.execute
217
    end
218

  
219
    expected = <<-EOF
220

  
221
*** REMOTE GEMS ***
222

  
223
a (2)
224
    Authors: Abraham Lincoln ., . Hirohito
225
    Homepage: http://a.example.com/.
226

  
227
    Truncating the summary for a-2 to 100,000 characters:
228
#{"    This is a lot of text. This is a lot of text. This is a lot of text.\n" * 1449}    This is a lot of te
229

  
150 230
pl (1)
151 231
    Platform: i386-linux
152 232
    Author: A User
test/rubygems/test_gem_installer.rb
1214 1214
    end
1215 1215
  end
1216 1216

  
1217
  def test_pre_install_checks_malicious_name
1218
    spec = util_spec '../malicious', '1'
1219
    def spec.full_name # so the spec is buildable
1220
      "malicious-1"
1221
    end
1222
    def spec.validate; end
1223

  
1224
    util_build_gem spec
1225

  
1226
    gem = File.join(@gemhome, 'cache', spec.file_name)
1227

  
1228
    use_ui @ui do
1229
      @installer = Gem::Installer.at gem
1230
      e = assert_raises Gem::InstallError do
1231
        @installer.pre_install_checks
1232
      end
1233
      assert_equal '#<Gem::Specification name=../malicious version=1> has an invalid name', e.message
1234
    end
1235
  end
1236

  
1217 1237
  def test_shebang
1218 1238
    util_make_exec @spec, "#!/usr/bin/ruby"
1219 1239

  
test/rubygems/test_gem_remote_fetcher.rb
181 181
    dns.verify
182 182
  end
183 183

  
184
  def test_api_endpoint_ignores_trans_domain_values_that_end_with_original_in_path
185
    uri = URI.parse "http://example.com/foo"
186
    target = MiniTest::Mock.new
187
    target.expect :target, "evil.com/a.example.com"
188

  
189
    dns = MiniTest::Mock.new
190
    dns.expect :getresource, target, [String, Object]
191

  
192
    fetch = Gem::RemoteFetcher.new nil, dns
193
    assert_equal URI.parse("http://example.com/foo"), fetch.api_endpoint(uri)
194

  
195
    target.verify
196
    dns.verify
197
  end
198

  
184 199
  def test_api_endpoint_ignores_trans_domain_values
185 200
    uri = URI.parse "http://gems.example.com/foo"
186 201
    target = MiniTest::Mock.new
test/rubygems/test_gem_specification.rb
2610 2610
      @a1.validate
2611 2611
    end
2612 2612

  
2613
    assert_equal 'invalid value for attribute name: ":json"', e.message
2613
    assert_equal 'invalid value for attribute name: ":json" must be a string', e.message
2614

  
2615
    @a1.name = []
2616
    e = assert_raises Gem::InvalidSpecificationException do
2617
      @a1.validate
2618
    end
2619
    assert_equal "invalid value for attribute name: \"[]\" must be a string", e.message
2620

  
2621
    @a1.name = ""
2622
    e = assert_raises Gem::InvalidSpecificationException do
2623
      @a1.validate
2624
    end
2625
    assert_equal "invalid value for attribute name: \"\" must include at least one letter", e.message
2626

  
2627
    @a1.name = "12345"
2628
    e = assert_raises Gem::InvalidSpecificationException do
2629
      @a1.validate
2630
    end
2631
    assert_equal "invalid value for attribute name: \"12345\" must include at least one letter", e.message
2632

  
2633
    @a1.name = "../malicious"
2634
    e = assert_raises Gem::InvalidSpecificationException do
2635
      @a1.validate
2636
    end
2637
    assert_equal "invalid value for attribute name: \"../malicious\" can only include letters, numbers, dashes, and underscores", e.message
2638

  
2639
    @a1.name = "\ba\t"
2640
    e = assert_raises Gem::InvalidSpecificationException do
2641
      @a1.validate
2642
    end
2643
    assert_equal "invalid value for attribute name: \"\\ba\\t\" can only include letters, numbers, dashes, and underscores", e.message
2614 2644
  end
2615 2645

  
2616 2646
  def test_validate_non_nil
test/rubygems/test_gem_text.rb
35 35
    assert_equal expected, format_text(text, 78)
36 36
  end
37 37

  
38
  def test_format_removes_nonprintable_characters
39
    assert_equal "text with weird .. stuff .", format_text("text with weird \x1b\x02 stuff \x7f", 40)
40
  end
41

  
38 42
  def test_min3
39 43
    assert_equal 1, min3(1, 1, 1)
40 44
    assert_equal 1, min3(1, 1, 2)
......
71 75
    assert_equal 7, levenshtein_distance("xxxxxxx", "ZenTest")
72 76
    assert_equal 7, levenshtein_distance("zentest", "xxxxxxx")
73 77
  end
78

  
79
  def test_truncate_text
80
    assert_equal "abc", truncate_text("abc", "desc")
81
    assert_equal "Truncating desc to 2 characters:\nab", truncate_text("abc", "desc", 2)
82
    s = "ab" * 500_001
83
    assert_equal "Truncating desc to 1,000,000 characters:\n#{s[0, 1_000_000]}", truncate_text(s, "desc", 1_000_000)
84
  end
74 85
end