From 05c9967ac3122b45dd2fe2ae48d67d5e2a3f14d7 Mon Sep 17 00:00:00 2001
From: John Estabrook <jestabro@sentrium.io>
Date: Tue, 12 Nov 2019 08:33:34 -0600
Subject: T1801: escape isolated backslashes before passing to ConfigTree()

---
 python/vyos/config.py | 6 ++++++
 python/vyos/util.py   | 6 ++++++
 2 files changed, 12 insertions(+)

(limited to 'python')

diff --git a/python/vyos/config.py b/python/vyos/config.py
index 13b2c107e..e6a41a92d 100644
--- a/python/vyos/config.py
+++ b/python/vyos/config.py
@@ -69,6 +69,7 @@ import json
 import subprocess
 
 import vyos.configtree
+import vyos.util
 
 
 class VyOSError(Exception):
@@ -111,6 +112,11 @@ class Config(object):
         else:
             session_config_text = running_config_text
 
+        # The output of showConfig does not escape backslashes, as is expected
+        # by ConfigTree().
+        session_config_text = vyos.util.escape_backslash(session_config_text)
+        running_config_text = vyos.util.escape_backslash(running_config_text)
+
         self._session_config = vyos.configtree.ConfigTree(session_config_text)
         self._running_config = vyos.configtree.ConfigTree(running_config_text)
 
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 67a602f7a..659a702fd 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -199,3 +199,9 @@ def is_admin() -> bool:
     current_user = getpass.getuser()
     (_, _, _, admin_group_members) = grp.getgrnam('sudo')
     return current_user in admin_group_members
+
+def escape_backslash(string: str) -> str:
+    """Escape single backslashes in string that are not in escape sequence"""
+    p = re.compile(r'(?<!\\)[\\](?!b|f|n|r|t|\\[^bfnrt])')
+    result = p.sub(r'\\\\', string)
+    return result
-- 
cgit v1.2.3