diff --git a/modules/tftp/server.rb b/modules/tftp/server.rb index 0047f64e1..7feb2232f 100644 --- a/modules/tftp/server.rb +++ b/modules/tftp/server.rb @@ -93,7 +93,8 @@ def pxeconfig_file mac end def self.fetch_boot_file dst, src - filename = dst + '-' + src.split("/")[-1] + + filename = boot_filename(dst, src) destination = Pathname.new(File.expand_path(filename, Proxy::TFTP::Plugin.settings.tftproot)).cleanpath tftproot = Pathname.new(Proxy::TFTP::Plugin.settings.tftproot).cleanpath raise "TFTP destination outside of tftproot" unless destination.to_s.start_with?(tftproot.to_s) @@ -104,4 +105,9 @@ def self.fetch_boot_file dst, src ::Proxy::HttpDownloads.start_download(src.to_s, destination.to_s) end + + def self.boot_filename(dst, src) + # Do not append a '-' if the dst is a directory path + dst.end_with?('/') ? dst + src.split("/")[-1] : dst + '-' + src.split("/")[-1] + end end diff --git a/test/tftp/tftp_test.rb b/test/tftp/tftp_test.rb index e803476af..08833b816 100644 --- a/test/tftp/tftp_test.rb +++ b/test/tftp/tftp_test.rb @@ -43,4 +43,11 @@ def test_paths_outside_tftp_directory_raise_errors end end + def test_boot_filename_has_no_dash_when_prefix_ends_with_slash + assert_equal "a/b/c/somefile", Proxy::TFTP.boot_filename('a/b/c/', '/d/somefile') + end + + def test_boot_filename_uses_dash_when_prefix_does_not_end_with_slash + assert_equal "a/b/c-somefile", Proxy::TFTP.boot_filename('a/b/c', '/d/somefile') + end end