Commit da961187 authored by Guido Trotter's avatar Guido Trotter
Browse files

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: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 55efe6da
......@@ -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.
......
......@@ -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()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment