Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape spaces found while parsing GPO XMLNS tags #67129

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/66992.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes an issue with the LGPO module when trying to parse ADMX/ADML files
that have a space in the XMLNS url in the policyDefinitionsResources header.
18 changes: 18 additions & 0 deletions salt/modules/win_lgpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5061,6 +5061,18 @@ def _remove_invalid_xmlns(xml_file):
return xml_tree


def _encode_xmlns_url(match):
"""
Escape spaces in xmlns urls
"""
before_xmlns = match.group(1)
xmlns = match.group(2)
url = match.group(3)
after_url = match.group(4)
encoded_url = re.sub(r"\s+", "%20", url)
return f'{before_xmlns}{xmlns}="{encoded_url}"{after_url}'


def _parse_xml(adm_file):
"""
Parse the admx/adml file. There are 3 scenarios (so far) that we'll likely
Expand Down Expand Up @@ -5107,6 +5119,12 @@ def _parse_xml(adm_file):
encoding = "utf-16"
raw = raw.decode(encoding)
for line in raw.split("\r\n"):
if 'xmlns="' in line:
line = re.sub(
r'(.*)(\bxmlns(?::\w+)?)\s*=\s*"([^"]+)"(.*)',
_encode_xmlns_url,
line,
)
if 'key="' in line:
start = line.index('key="')
q1 = line[start:].index('"') + start
Expand Down
20 changes: 20 additions & 0 deletions tests/pytests/unit/modules/win_lgpo/test_admx_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ def _test_set_user_policy(lgpo_bin, shell, name, setting, exp_regexes):
],
),
(
# This will need to be fixed for Windows Server 2025
# The bottom two options have been removed in 2025
# Though not set here, we're verifying there were set
"Specify settings for optional component installation and component repair",
"Disabled",
[
Expand All @@ -358,6 +361,8 @@ def _test_set_user_policy(lgpo_bin, shell, name, setting, exp_regexes):
],
),
(
# This will need to be fixed for Windows Server 2025
# The bottom two options have been removed in 2025
"Specify settings for optional component installation and component repair",
{
"Alternate source file path": "",
Expand All @@ -371,6 +376,8 @@ def _test_set_user_policy(lgpo_bin, shell, name, setting, exp_regexes):
],
),
(
# This will need to be fixed for Windows Server 2025
# The bottom two options have been removed in 2025
"Specify settings for optional component installation and component repair",
{
"Alternate source file path": r"\\some\fake\server",
Expand Down Expand Up @@ -757,3 +764,16 @@ def test_set_computer_policy_multiple_policies(clean_comp, lgpo_bin, shell):
r"\\AU[\s]*AllowMUUpdateService[\s]*DELETE",
],
)


def test__encode_xmlns_url():
"""
Tests the _encode_xmlns_url function.
Spaces in the xmlns url should be converted to %20
"""
line = '<policyDefinitionResources xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="1.0" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/Policysecurity intelligence">'
result = re.sub(
r'(.*)(\bxmlns(?::\w+)?)\s*=\s*"([^"]+)"(.*)', win_lgpo._encode_xmlns_url, line
)
expected = '<policyDefinitionResources xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="1.0" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/Policysecurity%20intelligence">'
assert result == expected
Loading