ruby-changes:50783
From: nobu <ko1@a...>
Date: Wed, 28 Mar 2018 18:58:58 +0900 (JST)
Subject: [ruby-changes:50783] nobu:r62989 (trunk): dir.c: check NUL bytes
nobu 2018-03-28 18:58:52 +0900 (Wed, 28 Mar 2018) New Revision: 62989 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62989 Log: dir.c: check NUL bytes * dir.c (GlobPathValue): should be used in rb_push_glob only. other methods should use FilePathValue. https://hackerone.com/reports/302338 * dir.c (rb_push_glob): expand GlobPathValue Modified files: trunk/dir.c trunk/test/ruby/test_dir.rb Index: test/ruby/test_dir.rb =================================================================== --- test/ruby/test_dir.rb (revision 62988) +++ test/ruby/test_dir.rb (revision 62989) @@ -156,6 +156,9 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L156 open(File.join(@root, "}}a"), "wb") {} assert_equal(%w(}}{} }}a).map {|f| File.join(@root, f)}, Dir.glob(File.join(@root, '}}{\{\},a}'))) assert_equal(%w(}}{} }}a b c).map {|f| File.join(@root, f)}, Dir.glob(File.join(@root, '{\}\}{\{\},a},b,c}'))) + assert_raise(ArgumentError) { + Dir.glob([[@root, File.join(@root, "*")].join("\0")]) + } end def test_glob_recursive @@ -229,21 +232,25 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L232 def test_entries assert_entries(Dir.open(@root) {|dir| dir.entries}) assert_entries(Dir.entries(@root).to_a) + assert_raise(ArgumentError) {Dir.entries(@root+"\0")} end def test_foreach assert_entries(Dir.open(@root) {|dir| dir.each.to_a}) assert_entries(Dir.foreach(@root).to_a) + assert_raise(ArgumentError) {Dir.foreach(@root+"\0").to_a} end def test_children assert_entries(Dir.open(@root) {|dir| dir.children}, true) assert_entries(Dir.children(@root), true) + assert_raise(ArgumentError) {Dir.children(@root+"\0")} end def test_each_child assert_entries(Dir.open(@root) {|dir| dir.each_child.to_a}, true) assert_entries(Dir.each_child(@root).to_a, true) + assert_raise(ArgumentError) {Dir.each_child(@root+"\0").to_a} end def test_dir_enc @@ -400,6 +407,7 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L407 end assert_raise(Errno::ENOENT) {Dir.empty?(@nodir)} assert_not_send([Dir, :empty?, File.join(@root, "b")]) + assert_raise(ArgumentError) {Dir.empty?(@root+"\0")} end def test_glob_gc_for_fd Index: dir.c =================================================================== --- dir.c (revision 62988) +++ dir.c (revision 62989) @@ -472,15 +472,6 @@ static const rb_data_type_t dir_data_typ https://github.com/ruby/ruby/blob/trunk/dir.c#L472 static VALUE dir_close(VALUE); -#define GlobPathValue(str, safe) \ - /* can contain null bytes as separators */ \ - (!RB_TYPE_P((str), T_STRING) ? \ - (void)FilePathValue(str) : \ - (void)(check_safe_glob((str), (safe)), \ - check_glob_encoding(str), (str))) -#define check_safe_glob(str, safe) ((safe) ? rb_check_safe_obj(str) : (void)0) -#define check_glob_encoding(str) rb_enc_check((str), rb_enc_from_encoding(rb_usascii_encoding())) - static VALUE dir_s_alloc(VALUE klass) { @@ -551,7 +542,7 @@ dir_initialize(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/dir.c#L542 } } - GlobPathValue(dirname, FALSE); + FilePathValue(dirname); orig = rb_str_dup_frozen(dirname); dirname = rb_str_encode_ospath(dirname); dirname = rb_str_dup_frozen(dirname); @@ -2545,7 +2536,14 @@ rb_push_glob(VALUE str, VALUE base, int https://github.com/ruby/ruby/blob/trunk/dir.c#L2536 long offset = 0; VALUE ary; - GlobPathValue(str, TRUE); + /* can contain null bytes as separators */ + if (!RB_TYPE_P((str), T_STRING)) { + FilePathValue(str); + } + else { + rb_check_safe_obj(str); + rb_enc_check(str, rb_enc_from_encoding(rb_usascii_encoding())); + } ary = rb_ary_new(); while (offset < RSTRING_LEN(str)) { @@ -2575,7 +2573,7 @@ dir_globs(long argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/dir.c#L2573 for (i = 0; i < argc; ++i) { int status; VALUE str = argv[i]; - GlobPathValue(str, TRUE); + FilePathValue(str); status = push_glob(ary, str, base, flags); if (status) GLOB_JUMP_TAG(status); } @@ -2600,7 +2598,7 @@ dir_glob_options(VALUE opt, VALUE *base, https://github.com/ruby/ruby/blob/trunk/dir.c#L2598 } #endif else { - GlobPathValue(args[0], TRUE); + FilePathValue(args[0]); if (!RSTRING_LEN(args[0])) args[0] = Qnil; *base = args[0]; } @@ -3185,7 +3183,7 @@ rb_dir_s_empty_p(VALUE obj, VALUE dirnam https://github.com/ruby/ruby/blob/trunk/dir.c#L3183 const char *path; enum {false_on_notdir = 1}; - GlobPathValue(dirname, FALSE); + FilePathValue(dirname); orig = rb_str_dup_frozen(dirname); dirname = rb_str_encode_ospath(dirname); dirname = rb_str_dup_frozen(dirname); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/