Project

General

Profile

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

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

View differences:

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

  
12 12
module Gem
13
  VERSION = '2.5.2'
13
  VERSION = '2.5.2.1'
14 14
end
15 15

  
16 16
# Must be first since it unloads the prelude from 1.9.2
lib/rubygems/commands/query_command.rb
226 226
        end
227 227
      end
228 228

  
229
      output << make_entry(matching_tuples, platforms)
229
      output << clean_text(make_entry(matching_tuples, platforms))
230 230
    end
231 231
  end
232 232

  
......
344 344
  end
345 345

  
346 346
  def spec_summary entry, spec
347
    entry << "\n\n" << format_text(spec.summary, 68, 4)
347
    summary = truncate_text(spec.summary, "the summary for #{spec.full_name}")
348
    entry << "\n\n" << format_text(summary, 68, 4)
348 349
  end
349 350

  
350 351
end
lib/rubygems/installer.rb
693 693
      unpack or File.writable?(gem_home)
694 694
  end
695 695

  
696
  def verify_spec_name
697
    return if spec.name =~ Gem::Specification::VALID_NAME_PATTERN
698
    raise Gem::InstallError, "#{spec} has an invalid name"
699
  end
700

  
696 701
  ##
697 702
  # Return the text for an application file.
698 703

  
......
812 817

  
813 818
    ensure_loadable_spec
814 819

  
820
    verify_spec_name
821

  
815 822
    if options[:install_as_default]
816 823
      Gem.ensure_default_gem_subdirectories gem_home
817 824
    else
lib/rubygems/remote_fetcher.rb
104 104
    else
105 105
      target = res.target.to_s.strip
106 106

  
107
      if /\.#{Regexp.quote(host)}\z/ =~ target
107
      if URI("http://" + target).host.end_with?(".#{host}")
108 108
        return URI.parse "#{uri.scheme}://#{target}#{uri.path}"
109 109
      end
110 110

  
lib/rubygems/specification.rb
108 108

  
109 109
  private_constant :LOAD_CACHE if defined? private_constant
110 110

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

  
111 113
  # :startdoc:
112 114

  
113 115
  ##
......
2665 2667
      end
2666 2668
    end
2667 2669

  
2668
    unless String === name then
2670
    if !name.is_a?(String) then
2669 2671
      raise Gem::InvalidSpecificationException,
2670
            "invalid value for attribute name: \"#{name.inspect}\""
2672
            "invalid value for attribute name: \"#{name.inspect}\" must be a string"
2673
    elsif name !~ /[a-zA-Z]/ then
2674
      raise Gem::InvalidSpecificationException,
2675
            "invalid value for attribute name: #{name.dump} must include at least one letter"
2676
    elsif name !~ VALID_NAME_PATTERN then
2677
      raise Gem::InvalidSpecificationException,
2678
            "invalid value for attribute name: #{name.dump} can only include letters, numbers, dashes, and underscores"
2671 2679
    end
2672 2680

  
2673 2681
    if raw_require_paths.empty? then
lib/rubygems/text.rb
6 6

  
7 7
module Gem::Text
8 8

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

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

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

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

  
17 30
    while work.length > wrap do
18 31
      if work =~ /^(.{0,#{wrap}})[ \n]/ then
test/rubygems/test_gem_commands_query_command.rb
116 116
    This is a lot of text. This is a lot of text. This is a lot of text.
117 117
    This is a lot of text.
118 118

  
119
pl (1)
120
    Platform: i386-linux
121
    Author: A User
122
    Homepage: http://example.com
123

  
124
    this is a summary
125
    EOF
126

  
127
    assert_equal expected, @ui.output
128
    assert_equal '', @ui.error
129
  end
130

  
131
  def test_execute_details_cleans_text
132
    spec_fetcher do |fetcher|
133
      fetcher.spec 'a', 2 do |s|
134
        s.summary = 'This is a lot of text. ' * 4
135
        s.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
136
        s.homepage = "http://a.example.com/\x03"
137
      end
138

  
139
      fetcher.legacy_platform
140
    end
141

  
142
    @cmd.handle_options %w[-r -d]
143

  
144
    use_ui @ui do
145
      @cmd.execute
146
    end
147

  
148
    expected = <<-EOF
149

  
150
*** REMOTE GEMS ***
151

  
152
a (2)
153
    Authors: Abraham Lincoln ., . Hirohito
154
    Homepage: http://a.example.com/.
155

  
156
    This is a lot of text. This is a lot of text. This is a lot of text.
157
    This is a lot of text.
158

  
159
pl (1)
160
    Platform: i386-linux
161
    Author: A User
162
    Homepage: http://example.com
163

  
164
    this is a summary
165
    EOF
166

  
167
    assert_equal expected, @ui.output
168
    assert_equal '', @ui.error
169
  end
170

  
171
  def test_execute_details_truncates_summary
172
    spec_fetcher do |fetcher|
173
      fetcher.spec 'a', 2 do |s|
174
        s.summary = 'This is a lot of text. ' * 10_000
175
        s.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
176
        s.homepage = "http://a.example.com/\x03"
177
      end
178

  
179
      fetcher.legacy_platform
180
    end
181

  
182
    @cmd.handle_options %w[-r -d]
183

  
184
    use_ui @ui do
185
      @cmd.execute
186
    end
187

  
188
    expected = <<-EOF
189

  
190
*** REMOTE GEMS ***
191

  
192
a (2)
193
    Authors: Abraham Lincoln ., . Hirohito
194
    Homepage: http://a.example.com/.
195

  
196
    Truncating the summary for a-2 to 100,000 characters:
197
#{"    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
198

  
119 199
pl (1)
120 200
    Platform: i386-linux
121 201
    Author: A User
test/rubygems/test_gem_installer.rb
1227 1227
    end
1228 1228
  end
1229 1229

  
1230
  def test_pre_install_checks_malicious_name
1231
    spec = util_spec '../malicious', '1'
1232
    def spec.full_name # so the spec is buildable
1233
      "malicious-1"
1234
    end
1235
    def spec.validate; end
1236

  
1237
    util_build_gem spec
1238

  
1239
    gem = File.join(@gemhome, 'cache', spec.file_name)
1240

  
1241
    use_ui @ui do
1242
      @installer = Gem::Installer.at gem
1243
      e = assert_raises Gem::InstallError do
1244
        @installer.pre_install_checks
1245
      end
1246
      assert_equal '#<Gem::Specification name=../malicious version=1> has an invalid name', e.message
1247
    end
1248
  end
1249

  
1230 1250
  def test_shebang
1231 1251
    util_make_exec @spec, "#!/usr/bin/ruby"
1232 1252

  
test/rubygems/test_gem_remote_fetcher.rb
253 253
    dns.verify
254 254
  end
255 255

  
256
  def test_api_endpoint_ignores_trans_domain_values_that_end_with_original_in_path
257
    uri = URI.parse "http://example.com/foo"
258
    target = MiniTest::Mock.new
259
    target.expect :target, "evil.com/a.example.com"
260

  
261
    dns = MiniTest::Mock.new
262
    dns.expect :getresource, target, [String, Object]
263

  
264
    fetch = Gem::RemoteFetcher.new nil, dns
265
    assert_equal URI.parse("http://example.com/foo"), fetch.api_endpoint(uri)
266

  
267
    target.verify
268
    dns.verify
269
  end
270

  
256 271
  def test_api_endpoint_timeout_warning
257 272
    uri = URI.parse "http://gems.example.com/foo"
258 273

  
test/rubygems/test_gem_specification.rb
2974 2974
      @a1.validate
2975 2975
    end
2976 2976

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

  
2979
    @a1.name = []
2980
    e = assert_raises Gem::InvalidSpecificationException do
2981
      @a1.validate
2982
    end
2983
    assert_equal "invalid value for attribute name: \"[]\" must be a string", e.message
2984

  
2985
    @a1.name = ""
2986
    e = assert_raises Gem::InvalidSpecificationException do
2987
      @a1.validate
2988
    end
2989
    assert_equal "invalid value for attribute name: \"\" must include at least one letter", e.message
2990

  
2991
    @a1.name = "12345"
2992
    e = assert_raises Gem::InvalidSpecificationException do
2993
      @a1.validate
2994
    end
2995
    assert_equal "invalid value for attribute name: \"12345\" must include at least one letter", e.message
2996

  
2997
    @a1.name = "../malicious"
2998
    e = assert_raises Gem::InvalidSpecificationException do
2999
      @a1.validate
3000
    end
3001
    assert_equal "invalid value for attribute name: \"../malicious\" can only include letters, numbers, dashes, and underscores", e.message
3002

  
3003
    @a1.name = "\ba\t"
3004
    e = assert_raises Gem::InvalidSpecificationException do
3005
      @a1.validate
3006
    end
3007
    assert_equal "invalid value for attribute name: \"\\ba\\t\" can only include letters, numbers, dashes, and underscores", e.message
2978 3008
  end
2979 3009

  
2980 3010
  def test_validate_non_nil
test/rubygems/test_gem_text.rb
36 36
    assert_equal expected, format_text(text, 78)
37 37
  end
38 38

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

  
39 43
  def test_min3
40 44
    assert_equal 1, min3(1, 1, 1)
41 45
    assert_equal 1, min3(1, 1, 2)
......
74 78
    assert_equal 7, levenshtein_distance("xxxxxxx", "ZenTest")
75 79
    assert_equal 7, levenshtein_distance("zentest", "xxxxxxx")
76 80
  end
81

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