From d1ba3b2a08ff8334afd2cf110fd1bdc6b6a2621a Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 11 Oct 2021 07:51:29 +0200 Subject: [PATCH] Make sure all control characters are quoted. --- plugins/module_utils/quoting.py | 7 +++++++ tests/unit/plugins/module_utils/test_quoting.py | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/module_utils/quoting.py b/plugins/module_utils/quoting.py index be01d32e..61d67da5 100644 --- a/plugins/module_utils/quoting.py +++ b/plugins/module_utils/quoting.py @@ -156,6 +156,13 @@ def quote_routeros_argument_value(argument): result.append(b'\\%s' % ESCAPE_SEQUENCE_REVERSED[letter]) quote = True continue + elif ord(letter) < 32: + v = ord(letter) + v1 = v % 16 + v2 = v // 16 + result.append(b'\\%s%s' % (ESCAPE_DIGITS[v2:v2 + 1], ESCAPE_DIGITS[v1:v1 + 1])) + quote = True + continue elif letter in (b' ', b'=', b';', b"'"): quote = True result.append(letter) diff --git a/tests/unit/plugins/module_utils/test_quoting.py b/tests/unit/plugins/module_utils/test_quoting.py index b142db4f..15d6142c 100644 --- a/tests/unit/plugins/module_utils/test_quoting.py +++ b/tests/unit/plugins/module_utils/test_quoting.py @@ -26,6 +26,8 @@ ('a ', {'must_match_everything': False}, ('a', 1)), (r'"a b"', {}, ('a b', 5)), (r'"b\"f"', {}, ('b"f', 6)), + (r'"\01"', {}, ('\x01', 5)), + (r'"\1F"', {}, ('\x1f', 5)), (r'"\FF"', {}, (to_native(b'\xff'), 5)), (r'"\"e"', {}, ('"e', 5)), (r'"\""', {}, ('"', 4)), @@ -225,11 +227,13 @@ def test_quote_routeros_argument_errors(argument, message): ('_', r'"\_"'), ('\a', r'"\a"'), ('\b', r'"\b"'), - # (to_native('\xff'), r'"\f"'), + # (to_native(b'\xff'), r'"\f"'), ('\n', r'"\n"'), ('\r', r'"\r"'), ('\t', r'"\t"'), ('\v', r'"\v"'), + ('\x01', r'"\01"'), + ('\x1f', r'"\1F"'), ]