From da961187f97344fde390140ebb2f10d10d334d51 Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Fri, 22 May 2009 13:35:30 +0100 Subject: [PATCH] Add utils.IsNormAbsPath function Currently most of the time we check for absolute path, but that doesn't protect us from some invalid paths. In some places we should be more strict, and this function should help us to. Signed-off-by: Guido Trotter <ultrotter@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/utils.py | 7 +++++++ test/ganeti.utils_unittest.py | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/utils.py b/lib/utils.py index e876e2c3e..3a0881fcd 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1785,6 +1785,13 @@ def SetupLogging(logfile, debug=False, stderr_logging=False, program="", # we need to re-raise the exception raise +def IsNormAbsPath(path): + """Check whether a path is absolute and also "normal". + + This avoids things like /dir/../../other/path to be valid. + + """ + return os.path.normpath(path) == path and os.path.isabs(path) def TailFile(fname, lines=20): """Return the last lines from a file. diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py index 1c2992c3d..433bfe336 100755 --- a/test/ganeti.utils_unittest.py +++ b/test/ganeti.utils_unittest.py @@ -44,7 +44,7 @@ from ganeti.utils import IsProcessAlive, RunCmd, \ ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \ ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \ SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree, OwnIpAddress, \ - TailFile, ForceDictType + TailFile, ForceDictType, IsNormAbsPath from ganeti.errors import LockError, UnitParseError, GenericError, \ ProgrammerError @@ -969,5 +969,23 @@ class TestForceDictType(unittest.TestCase): self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'}) +class TestIsAbsNormPath(unittest.TestCase): + """Testing case for IsProcessAlive""" + + def _pathTestHelper(self, path, result): + if result: + self.assert_(IsNormAbsPath(path), + "Path %s should be absolute and normal" % path) + else: + self.assert_(not IsNormAbsPath(path), + "Path %s should not be absolute and normal" % path) + + def testBase(self): + self._pathTestHelper('/etc', True) + self._pathTestHelper('/srv', True) + self._pathTestHelper('etc', False) + self._pathTestHelper('/etc/../root', False) + self._pathTestHelper('/etc/', False) + if __name__ == '__main__': unittest.main() -- GitLab