Skip to content

Commit 761c40a

Browse files
committed
Short-circuit credentials path check
1 parent 11eca4e commit 761c40a

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/fcm.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,21 @@ def credentials_error_msg(param)
302302
raise InvalidCredentialError, error_msg
303303
end
304304

305-
def filename_or_io_like?(path)
306-
(path.is_a?(String) || path.respond_to?(:open)) && File.file?(path)
305+
def open_json_key_path?(path)
306+
valid_io_object = path.respond_to?(:open)
307+
if valid_io_object && File.file?(path)
308+
return true
309+
end
310+
311+
max_path_len = 1024
312+
valid_path = path.is_a?(String) && path.length <= max_path_len
313+
valid_path && File.file?(path)
307314
end
308315

309316
def json_key
310317
@json_key ||= if @json_key_path.respond_to?(:read)
311318
@json_key_path
312-
elsif filename_or_io_like?(@json_key_path)
319+
elsif open_json_key_path?(@json_key_path)
313320
File.open(@json_key_path)
314321
else
315322
credentials_error_msg(@json_key_path)

spec/fcm_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
'c7f234d487c8%40developer.gserviceaccount.com'
2525
end
2626

27+
let(:large_file_name) do
28+
1021.times.map do |el|
29+
"a"
30+
end.join('') + '.txt'
31+
end
32+
2733
let(:creds_error) do
2834
FCM::InvalidCredentialError
2935
end
@@ -33,7 +39,7 @@
3339
"type": 'service_account',
3440
"project_id": 'example',
3541
"private_key_id": 'c09c4593eee53707ca9f4208fbd6fe72b29fc7ab',
36-
"private_key": OpenSSL::PKey::RSA.new(2048),
42+
"private_key": OpenSSL::PKey::RSA.new(2048).to_s,
3743
"client_email": client_email,
3844
"client_id": 'acedc3c0a63b3562376386f0.apps.googleusercontent.com',
3945
"auth_uri": 'https://accounts.google.com/o/oauth2/auth',
@@ -62,6 +68,12 @@
6268
expect(fcm.__send__(:json_key).class).to eq(File)
6369
end
6470

71+
it 'raises an error when passed a large path' do
72+
expect do
73+
FCM.new(large_file_name).__send__(:json_key)
74+
end.to raise_error(creds_error)
75+
end
76+
6577
it 'can be an IO object' do
6678
fcm = FCM.new(StringIO.new('hey'))
6779
expect(fcm.__send__(:json_key).class).to eq(StringIO)

0 commit comments

Comments
 (0)