Skip to content

Commit 36b4898

Browse files
committed
Replace deprecated yaml loader
Replaces the deprecated pyyaml `load` method with the `full_load` method, which prevents arbitrary code execution. Includes a test suite for the `read` helper method.
1 parent b0bd254 commit 36b4898

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

aws_lambda/aws_lambda.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ def get_concurrency(cfg):
752752

753753

754754
def read_cfg(path_to_config_file, profile_name):
755-
cfg = read(path_to_config_file, loader=yaml.load)
755+
cfg = read(path_to_config_file, loader=yaml.full_load)
756756
if profile_name is not None:
757757
cfg['profile'] = profile_name
758758
elif 'AWS_PROFILE' in os.environ:

tests/unit/test_readHelper.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import unittest
3+
import yaml
4+
from yaml import YAMLLoadWarning
5+
from aws_lambda.helpers import read
6+
7+
class TestReadHelper(unittest.TestCase):
8+
9+
TEST_FILE = 'readTmp.txt'
10+
11+
def setUp(self):
12+
with open(TestReadHelper.TEST_FILE, 'w') as tmp_file:
13+
tmp_file.write('testYaml: testing')
14+
15+
def tearDown(self):
16+
os.remove(TestReadHelper.TEST_FILE)
17+
18+
def test_read_no_loader_non_binary(self):
19+
fileContents = read(TestReadHelper.TEST_FILE)
20+
self.assertEqual(fileContents, 'testYaml: testing')
21+
22+
def test_read_yaml_loader_non_binary(self):
23+
testYaml = read(TestReadHelper.TEST_FILE, loader=yaml.full_load)
24+
self.assertEqual(testYaml['testYaml'], 'testing')
25+
26+
def test_read_no_loader_binary_mode(self):
27+
fileContents = read(TestReadHelper.TEST_FILE, binary_file=True)
28+
self.assertEqual(fileContents, b'testYaml: testing')
29+
30+
def test_read_yaml_loader_binary_mode(self):
31+
testYaml = read(
32+
TestReadHelper.TEST_FILE,
33+
loader=yaml.full_load,
34+
binary_file=True
35+
)
36+
self.assertEqual(testYaml['testYaml'], 'testing')
37+
38+
def test_read_yaml_old_load_warns(self):
39+
with self.assertWarns(YAMLLoadWarning):
40+
testYaml = read(TestReadHelper.TEST_FILE, loader=yaml.load)
41+
self.assertEqual(testYaml['testYaml'], 'testing')

0 commit comments

Comments
 (0)