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
@@ -87,17 +87,16 @@ def change(&block)
87
87
88
88
private
89
89
def writing ( contents )
90
- tmp_file = " #{ Process . pid } . #{ content_path . basename . to_s . chomp ( ' .enc' ) } "
91
- tmp_path = Pathname . new File . join ( Dir . tmpdir , tmp_file )
92
- tmp_path . binwrite contents
90
+ Tempfile . create ( [ "" , "-" + content_path . basename . to_s . chomp ( " .enc" ) ] ) do | tmp_file |
91
+ tmp_path = Pathname . new ( tmp_file )
92
+ tmp_path . binwrite contents
93
93
94
- yield tmp_path
94
+ yield tmp_path
95
95
96
- updated_contents = tmp_path . binread
96
+ updated_contents = tmp_path . binread
97
97
98
- write ( updated_contents ) if updated_contents != contents
99
- ensure
100
- FileUtils . rm ( tmp_path ) if tmp_path &.exist?
98
+ write ( updated_contents ) if updated_contents != contents
99
+ end
101
100
end
102
101
103
102
Original file line number Diff line number Diff line change @@ -52,6 +52,14 @@ class EncryptedFileTest < ActiveSupport::TestCase
52
52
assert_equal "#{ @content } and went by the lake" , @encrypted_file . read
53
53
end
54
54
55
+ test "change sets restricted permissions" do
56
+ @encrypted_file . write ( @content )
57
+ @encrypted_file . change do |file |
58
+ assert_predicate file , :owned?
59
+ assert_equal "100600" , file . stat . mode . to_s ( 8 ) , "Incorrect mode for #{ file } "
60
+ end
61
+ end
62
+
55
63
test "raise MissingKeyError when key is missing" do
56
64
assert_raise ActiveSupport ::EncryptedFile ::MissingKeyError do
57
65
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