Skip to content

Commit 5251744

Browse files
committed
Replace sanitized_text method - Fix #7
Remove `sanitized_text` method and use `IO.popen` rather than `system` to prevent command injection.
1 parent f163d36 commit 5251744

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

lib/espeak/speech.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'open3.rb'
2-
31
module ESpeak
42
class Speech
53
attr_reader :options, :text
@@ -21,37 +19,40 @@ def initialize(text, options={})
2119
# Speaks text
2220
#
2321
def speak
24-
system(espeak_command(command_options))
22+
IO.popen(espeak_command(command_options), 'r').read
2523
end
2624

2725
# Generates mp3 file as a result of
2826
# Text-To-Speech conversion.
2927
#
3028
def save(filename)
31-
system(espeak_command(command_options, "--stdout") + " | " + lame_command(filename, command_options))
29+
speech = bytes_wav
30+
res = IO.popen(lame_command(filename, command_options), 'r+') do |process|
31+
process.write(speech)
32+
process.close_write
33+
process.read
34+
end
35+
res.to_s
3236
end
3337

3438
# Returns mp3 file bytes as a result of
3539
# Text-To-Speech conversion.
3640
#
3741
def bytes()
38-
stdout_str, stderr_str, process = Open3.capture3(espeak_command(command_options, "--stdout") + " | " + std_lame_command(command_options))
39-
stdout_str
42+
speech = bytes_wav
43+
res = IO.popen(std_lame_command(command_options), 'r+') do |process|
44+
process.write(speech)
45+
process.close_write
46+
process.read
47+
end
48+
res.to_s
4049
end
4150

4251
# Returns wav file bytes as a result of
4352
# Text-To-Speech conversion.
4453
#
4554
def bytes_wav()
46-
stdout_str, stderr_str, process = Open3.capture3(espeak_command(command_options, "--stdout"))
47-
stdout_str
48-
end
49-
50-
# espeak dies handling some chars
51-
# this function sanitizes text
52-
#
53-
def sanitized_text
54-
@text.gsub(/(!|\?|"|`|\\)/, ' ').strip
55+
IO.popen(espeak_command(command_options, "--stdout"), 'r').read
5556
end
5657

5758
private
@@ -73,15 +74,15 @@ def default_options
7374
end
7475

7576
def espeak_command(options, flags="")
76-
%|espeak "#{sanitized_text}" #{flags} -v#{options[:voice]} -p#{options[:pitch]} -k#{options[:capital]} -s#{options[:speed]}|
77+
['espeak', "#{@text}", "#{flags}", "-v#{options[:voice]}", "-p#{options[:pitch]}", "-k#{options[:capital]}", "-s#{options[:speed]}"]
7778
end
7879

7980
def std_lame_command(options)
8081
lame_command("-", options)
8182
end
8283

8384
def lame_command(filename, options)
84-
"lame -V2 - #{filename} #{'--quiet' if options[:quiet] == true}"
85+
['lame', '-V2', '-', "#{filename}", "#{'--quiet' if options[:quiet] == true}"]
8586
end
8687

8788
def symbolize_keys(hash)

0 commit comments

Comments
 (0)