Skip to content

Commit 4483990

Browse files
authored
Merge pull request #122 from launchdarkly/eb/ch58025/yaml-load
use yaml.safe_load() to avoid code execution vulnerability in file data source
2 parents 3e9c68b + 669e772 commit 4483990

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

ldclient/impl/integrations/files/file_data_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _load_file(self, path, all_data):
8080

8181
def _parse_content(self, content):
8282
if have_yaml:
83-
return yaml.load(content) # pyyaml correctly parses JSON too
83+
return yaml.safe_load(content) # pyyaml correctly parses JSON too
8484
return json.loads(content)
8585

8686
def _add_item(self, all_data, kind, item):

testing/test_file_data_source.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,28 @@ def test_evaluates_simplified_flag_with_client_as_expected():
246246
os.remove(path)
247247
if client is not None:
248248
client.close()
249+
250+
unsafe_yaml_caused_method_to_be_called = False
251+
252+
def arbitrary_method_called_from_yaml(x):
253+
global unsafe_yaml_caused_method_to_be_called
254+
unsafe_yaml_caused_method_to_be_called = True
255+
256+
def test_does_not_allow_unsafe_yaml():
257+
if not have_yaml:
258+
pytest.skip("skipping file source test with YAML because pyyaml isn't available")
259+
260+
# This extended syntax defined by pyyaml allows arbitrary code execution. We should be using
261+
# yaml.safe_load() which does not support such things.
262+
unsafe_yaml = '''
263+
!!python/object/apply:testing.test_file_data_source.arbitrary_method_called_from_yaml ["hi"]
264+
'''
265+
path = make_temp_file(unsafe_yaml)
266+
try:
267+
factory = Files.new_data_source(paths = path)
268+
client = LDClient(config=Config(update_processor_class = factory, send_events = False))
269+
finally:
270+
os.remove(path)
271+
if client is not None:
272+
client.close()
273+
assert unsafe_yaml_caused_method_to_be_called == False

0 commit comments

Comments
 (0)