Skip to content

Commit 92b844e

Browse files
fix: replace shell=True with secure subprocess in TranscodePerturbation
Security Fix: Convert amr-nb, ogg, and g711 codecs in TranscodePerturbation from shell=True to secure subprocess patterns using explicit argument lists. - Use subprocess.Popen with pipes for amr-nb and ogg codecs - Use subprocess.run with list arguments for g711 codec - Remove unused 'Any' import to fix linting errors Related to #15165 Signed-off-by: Rudra Tiwari <tiwarirudra2006@gmail.com>
1 parent 5d46d4a commit 92b844e

File tree

1 file changed

+39
-12
lines changed
  • nemo/collections/asr/parts/preprocessing

1 file changed

+39
-12
lines changed

nemo/collections/asr/parts/preprocessing/perturb.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import random
4040
import subprocess
4141
from tempfile import NamedTemporaryFile
42-
from typing import Any, List, Optional, Union
42+
from typing import List, Optional, Union
4343

4444
import librosa
4545
import numpy as np
@@ -1032,23 +1032,50 @@ def perturb(self, data):
10321032
transcoded_f = NamedTemporaryFile(suffix="_amr.wav")
10331033
rates = list(range(0, 4))
10341034
rate = rates[random.randint(0, len(rates) - 1)]
1035-
_ = subprocess.check_output(
1036-
f"sox {orig_f.name} -V0 -C {rate} -t amr-nb - | sox -t amr-nb - -V0 -b 16 -r 16000 {transcoded_f.name}",
1037-
shell=True,
1038-
)
1035+
with subprocess.Popen(
1036+
["sox", orig_f.name, "-V0", "-C", str(rate), "-t", "amr-nb", "-"],
1037+
stdout=subprocess.PIPE,
1038+
stderr=subprocess.PIPE,
1039+
) as sox_encode:
1040+
subprocess.run(
1041+
["sox", "-t", "amr-nb", "-", "-V0", "-b", "16", "-r", "16000", transcoded_f.name],
1042+
stdin=sox_encode.stdout,
1043+
check=True,
1044+
)
10391045
elif self._codecs[codec_ind] == "ogg":
10401046
transcoded_f = NamedTemporaryFile(suffix="_ogg.wav")
10411047
rates = list(range(-1, 8))
10421048
rate = rates[random.randint(0, len(rates) - 1)]
1043-
_ = subprocess.check_output(
1044-
f"sox {orig_f.name} -V0 -C {rate} -t ogg - | sox -t ogg - -V0 -b 16 -r 16000 {transcoded_f.name}",
1045-
shell=True,
1046-
)
1049+
with subprocess.Popen(
1050+
["sox", orig_f.name, "-V0", "-C", str(rate), "-t", "ogg", "-"],
1051+
stdout=subprocess.PIPE,
1052+
stderr=subprocess.PIPE,
1053+
) as sox_encode:
1054+
subprocess.run(
1055+
["sox", "-t", "ogg", "-", "-V0", "-b", "16", "-r", "16000", transcoded_f.name],
1056+
stdin=sox_encode.stdout,
1057+
check=True,
1058+
)
10471059
elif self._codecs[codec_ind] == "g711":
10481060
transcoded_f = NamedTemporaryFile(suffix="_g711.wav")
1049-
_ = subprocess.check_output(
1050-
f"sox {orig_f.name} -V0 -r 8000 -c 1 -e a-law {transcoded_f.name} lowpass 3400 highpass 300",
1051-
shell=True,
1061+
subprocess.run(
1062+
[
1063+
"sox",
1064+
orig_f.name,
1065+
"-V0",
1066+
"-r",
1067+
"8000",
1068+
"-c",
1069+
"1",
1070+
"-e",
1071+
"a-law",
1072+
transcoded_f.name,
1073+
"lowpass",
1074+
"3400",
1075+
"highpass",
1076+
"300",
1077+
],
1078+
check=True,
10521079
)
10531080

10541081
new_data = AudioSegment.from_file(transcoded_f.name, target_sr=16000)

0 commit comments

Comments
 (0)