Skip to content

Commit fb9eda2

Browse files
authored
fix(powershell): depth truncation and parsing error (#9181)
1 parent f0b1c4c commit fb9eda2

File tree

5 files changed

+141
-58
lines changed

5 files changed

+141
-58
lines changed

prowler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ All notable changes to the **Prowler SDK** are documented in this file.
4848

4949
### Fixed
5050
- Check `check_name` has no `resource_name` error for GCP provider [(#9169)](https://github.com/prowler-cloud/prowler/pull/9169)
51+
- Depth Truncation and parsing error in PowerShell queries [(#9181)](https://github.com/prowler-cloud/prowler/pull/9181)
5152

5253
---
5354

prowler/lib/powershell/powershell.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,19 @@ def json_parse_output(self, output: str) -> dict:
220220
if output == "":
221221
return {}
222222

223-
json_match = re.search(r"(\[.*\]|\{.*\})", output, re.DOTALL)
224-
if not json_match:
225-
logger.error(
226-
f"Unexpected PowerShell output: {output}\n",
227-
)
228-
else:
223+
decoder = json.JSONDecoder()
224+
for index, character in enumerate(output):
225+
if character not in ("{", "["):
226+
continue
229227
try:
230-
return json.loads(json_match.group(1))
231-
except json.JSONDecodeError as error:
232-
logger.error(
233-
f"Error parsing PowerShell output as JSON: {str(error)}\n",
234-
)
228+
parsed_json, _ = decoder.raw_decode(output[index:])
229+
return parsed_json
230+
except json.JSONDecodeError:
231+
continue
232+
233+
logger.error(
234+
f"Unexpected PowerShell output: {output}\n",
235+
)
235236

236237
return {}
237238

0 commit comments

Comments
 (0)