Skip to content
Open
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
31 changes: 25 additions & 6 deletions t/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,8 @@ def testServerInsecurePathRelative(self):
rrq.options = {}

# Start the download.
self.assertRaises(
tftpy.TftpException, serverstate.start, rrq.encode().buffer
)
with self.assertRaisesRegex(tftpy.TftpException, 'bad file path'):
serverstate.start(rrq.encode().buffer)

def testServerInsecurePathRootSibling(self):
raddress = "127.0.0.2"
Expand All @@ -456,9 +455,8 @@ def testServerInsecurePathRootSibling(self):
rrq.options = {}

# Start the download.
self.assertRaises(
tftpy.TftpException, serverstate.start, rrq.encode().buffer
)
with self.assertRaisesRegex(tftpy.TftpException, 'bad file path'):
serverstate.start(rrq.encode().buffer)

def testServerSecurePathAbsolute(self):
raddress = "127.0.0.2"
Expand Down Expand Up @@ -502,6 +500,27 @@ def testServerSecurePathRelative(self):
isinstance(serverstate.state, tftpy.TftpStates.TftpStateExpectACK)
)

def testServerPathRoot(self):
raddress = "127.0.0.2"
rport = 10000
timeout = 5
with self.dummyServerDir() as d:
root = '/'
serverstate = tftpy.TftpContexts.TftpContextServer(
raddress, rport, timeout, root
)
rrq = tftpy.TftpPacketTypes.TftpPacketRRQ()
rrq.filename = os.path.join(os.path.abspath(d), "foo", "bar")
rrq.mode = "octet"
rrq.options = {}

# Start the download.
serverstate.start(rrq.encode().buffer)
# Should be in expectack state.
self.assertTrue(
isinstance(serverstate.state, tftpy.TftpStates.TftpStateExpectACK)
)

def testServerDownloadWithStopNow(self, output="/tmp/out"):
log.debug("===> Running testcase testServerDownloadWithStopNow")
root = os.path.dirname(os.path.abspath(__file__))
Expand Down
7 changes: 5 additions & 2 deletions tftpy/TftpStates.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,11 @@ def serverInitial(self, pkt, raddress, rport):
# (e.g. '..') and ensure that is still within the server's
# root directory
self.full_path = os.path.abspath(full_path)
log.debug("full_path is %s", full_path)
if self.full_path.startswith(os.path.normpath(self.context.root) + os.sep):
# Determine root path, replace double slashes with single
# // => /
root_path = (os.path.normpath(self.context.root) + os.sep).replace('//', '/')
log.debug("full_path is %s, server root is %s" % (self.full_path, root_path))
if self.full_path.startswith(root_path):
log.info("requested file is in the server root - good")
else:
log.warning("requested file is not within the server root - bad")
Expand Down