File tree Expand file tree Collapse file tree 3 files changed +26
-17
lines changed Expand file tree Collapse file tree 3 files changed +26
-17
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "pathname"
4
- require "tmpdir "
4
+ require "tempfile "
5
5
require "active_support/message_encryptor"
6
6
7
7
module ActiveSupport
@@ -69,17 +69,16 @@ def change(&block)
69
69
70
70
private
71
71
def writing ( contents )
72
- tmp_file = " #{ Process . pid } . #{ content_path . basename . to_s . chomp ( ' .enc' ) } "
73
- tmp_path = Pathname . new File . join ( Dir . tmpdir , tmp_file )
74
- tmp_path . binwrite contents
72
+ Tempfile . create ( [ "" , "-" + content_path . basename . to_s . chomp ( " .enc" ) ] ) do | tmp_file |
73
+ tmp_path = Pathname . new ( tmp_file )
74
+ tmp_path . binwrite contents
75
75
76
- yield tmp_path
76
+ yield tmp_path
77
77
78
- updated_contents = tmp_path . binread
78
+ updated_contents = tmp_path . binread
79
79
80
- write ( updated_contents ) if updated_contents != contents
81
- ensure
82
- FileUtils . rm ( tmp_path ) if tmp_path &.exist?
80
+ write ( updated_contents ) if updated_contents != contents
81
+ end
83
82
end
84
83
85
84
Original file line number Diff line number Diff line change @@ -49,6 +49,14 @@ class EncryptedFileTest < ActiveSupport::TestCase
49
49
assert_equal "#{ @content } and went by the lake" , @encrypted_file . read
50
50
end
51
51
52
+ test "change sets restricted permissions" do
53
+ @encrypted_file . write ( @content )
54
+ @encrypted_file . change do |file |
55
+ assert_predicate file , :owned?
56
+ assert_equal "100600" , file . stat . mode . to_s ( 8 ) , "Incorrect mode for #{ file } "
57
+ end
58
+ end
59
+
52
60
test "raise MissingKeyError when key is missing" do
53
61
assert_raise ActiveSupport ::EncryptedFile ::MissingKeyError do
54
62
encrypted_file ( @content_path , key_path : "" , env_key : "" ) . read
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "yaml"
4
+ require "tempfile"
4
5
require "active_support/message_encryptor"
5
6
6
7
module Rails
@@ -87,17 +88,18 @@ def preprocess(path)
87
88
end
88
89
89
90
def writing ( contents )
90
- tmp_file = "#{ File . basename ( path ) } .#{ Process . pid } "
91
- tmp_path = File . join ( Dir . tmpdir , tmp_file )
92
- IO . binwrite ( tmp_path , contents )
91
+ file_name = "#{ File . basename ( path ) } .#{ Process . pid } "
93
92
94
- yield tmp_path
93
+ Tempfile . create ( [ "" , "-" + file_name ] ) do |tmp_file |
94
+ tmp_path = Pathname . new ( tmp_file )
95
+ tmp_path . binwrite contents
95
96
96
- updated_contents = IO . binread ( tmp_path )
97
+ yield tmp_path
97
98
98
- write ( updated_contents ) if updated_contents != contents
99
- ensure
100
- FileUtils . rm ( tmp_path ) if File . exist? ( tmp_path )
99
+ updated_contents = tmp_path . binread
100
+
101
+ write ( updated_contents ) if updated_contents != contents
102
+ end
101
103
end
102
104
103
105
def encryptor
You can’t perform that action at this time.
0 commit comments