From 2e08909a69b77e1e472bbb777ec0e532b453905f Mon Sep 17 00:00:00 2001 From: Doug Holt Date: Tue, 30 Jun 2026 20:42:33 -0600 Subject: [PATCH] fix: handle non-dict parsed JSON in firmware version manifest parser parse_versioning kept the last json.loads-parseable line and then indexed it with a string key. When that line parsed to a non-dict (e.g. a JSON array), the index raised an uncaught TypeError because only KeyError was handled, aborting the task. Guard that the parsed value is a dict before indexing and fall through to the existing 'no JSON could be loaded' path otherwise. Adds a regression test covering the non-dict input case. --- .../files/parse_manifest.py | 7 +++- .../tests/test_parse_manifest.py | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 roles/nvidia-dgx-firmware/tests/test_parse_manifest.py diff --git a/roles/nvidia-dgx-firmware/files/parse_manifest.py b/roles/nvidia-dgx-firmware/files/parse_manifest.py index 0c361cfc4..d0fb1506a 100644 --- a/roles/nvidia-dgx-firmware/files/parse_manifest.py +++ b/roles/nvidia-dgx-firmware/files/parse_manifest.py @@ -3,7 +3,7 @@ import json from collections import OrderedDict -from sys import argv +from sys import argv, exit def return_json(payload): @@ -173,6 +173,11 @@ def return_json(payload): manifest_json = json.loads(line) except ValueError: pass + + if not isinstance(manifest_json, dict): + print('No JSON could be loaded, is the container already running?') + exit(1) + try: if manifest_json['ErrorWritingVersioning']: print('No JSON could be loaded, is the container already running?') diff --git a/roles/nvidia-dgx-firmware/tests/test_parse_manifest.py b/roles/nvidia-dgx-firmware/tests/test_parse_manifest.py new file mode 100644 index 000000000..9135d0978 --- /dev/null +++ b/roles/nvidia-dgx-firmware/tests/test_parse_manifest.py @@ -0,0 +1,34 @@ +import subprocess +import sys +import tempfile +import unittest + +from pathlib import Path + + +SCRIPT = Path(__file__).resolve().parents[1] / 'files' / 'parse_manifest.py' + + +class ParseManifestTest(unittest.TestCase): + def test_parse_versioning_rejects_non_dict_json(self): + with tempfile.TemporaryDirectory() as temp_dir: + manifest = Path(temp_dir) / 'versioning.json' + manifest.write_text('[1,2,3]\n') + + result = subprocess.run( + [sys.executable, str(SCRIPT), 'parse_versioning', str(manifest)], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) + + self.assertEqual(result.returncode, 1) + self.assertEqual( + result.stdout, + 'No JSON could be loaded, is the container already running?\n', + ) + self.assertEqual(result.stderr, '') + + +if __name__ == '__main__': + unittest.main()