1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
From: =?utf-8?q?G=C3=BCnter_Obiltschnig?= <guenter.obiltschnig@appinf.com>
Date: Thu, 2 Nov 2017 09:30:27 +0100
Subject: merge zip entry absolute path vulnerability fix (#1968) from develop
---
Zip/src/Decompress.cpp | 4 +-
Zip/src/ZipCommon.cpp | 26 ++++++++++--
Zip/testsuite/src/CompressTest.cpp | 22 +++++-----
Zip/testsuite/src/ZipTest.cpp | 86 ++++++++++++++++++++++++++++++++++----
Zip/testsuite/src/ZipTest.h | 6 ++-
5 files changed, 116 insertions(+), 28 deletions(-)
diff --git a/Zip/src/Decompress.cpp b/Zip/src/Decompress.cpp
index e01f47e..d37b199 100644
--- a/Zip/src/Decompress.cpp
+++ b/Zip/src/Decompress.cpp
@@ -82,7 +82,7 @@ bool Decompress::handleZipEntry(std::istream& zipStream, const ZipLocalFileHeade
{
std::string dirName = hdr.getFileName();
if (!ZipCommon::isValidPath(dirName))
- throw ZipException("Illegal entry name " + dirName + " containing parent directory reference");
+ throw ZipException("Illegal entry name", dirName);
Poco::Path dir(_outDir, dirName);
dir.makeDirectory();
Poco::File aFile(dir);
@@ -102,7 +102,7 @@ bool Decompress::handleZipEntry(std::istream& zipStream, const ZipLocalFileHeade
}
if (!ZipCommon::isValidPath(fileName))
- throw ZipException("Illegal entry name " + fileName + " containing parent directory reference");
+ throw ZipException("Illegal entry name", fileName);
Poco::Path file(fileName);
file.makeFile();
diff --git a/Zip/src/ZipCommon.cpp b/Zip/src/ZipCommon.cpp
index fade822..8241258 100644
--- a/Zip/src/ZipCommon.cpp
+++ b/Zip/src/ZipCommon.cpp
@@ -15,6 +15,7 @@
#include "Poco/Zip/ZipCommon.h"
+#include "Poco/Path.h"
namespace Poco {
@@ -23,16 +24,33 @@ namespace Zip {
bool ZipCommon::isValidPath(const std::string& path)
{
+ try
+ {
+ if (Path(path, Path::PATH_UNIX).isAbsolute() || Path(path, Path::PATH_WINDOWS).isAbsolute())
+ return false;
+ }
+ catch (...)
+ {
+ return false;
+ }
+
if (path == "..")
return false;
- if (path.compare(0, 3, "../") == 0)
+ if ((path.size() >= 3) && path.compare(0, 3, "../") == 0)
+ return false;
+ if ((path.size() >= 3) && path.compare(0, 3, "..\\") == 0)
return false;
- if (path.compare(0, 3, "..\\") == 0)
+ if (path.find("/../") != std::string::npos)
return false;
- if (path.find("/..") != std::string::npos)
+ if (path.find("\\..\\") != std::string::npos)
return false;
- if (path.find("\\..") != std::string::npos)
+ if (path.find("/..\\") != std::string::npos)
return false;
+ if (path.find("\\../") != std::string::npos)
+ return false;
+ if ((path.size() >= 2) && path.compare(0, 2, "~/") == 0)
+ return false;
+
return true;
}
diff --git a/Zip/testsuite/src/CompressTest.cpp b/Zip/testsuite/src/CompressTest.cpp
index ee8bf84..0d66721 100644
--- a/Zip/testsuite/src/CompressTest.cpp
+++ b/Zip/testsuite/src/CompressTest.cpp
@@ -36,7 +36,7 @@ CompressTest::~CompressTest()
void CompressTest::testSingleFile()
{
- std::ofstream out("appinf.zip", std::ios::binary);
+ std::ofstream out(Poco::Path::temp() + "appinf.zip", std::ios::binary);
Poco::Path theFile(ZipTest::getTestFile("test.zip"));
Compress c(out, true);
c.addFile(theFile, theFile.getFileName());
@@ -46,10 +46,9 @@ void CompressTest::testSingleFile()
void CompressTest::testDirectory()
{
- std::ofstream out("pocobin.zip", std::ios::binary);
+ std::ofstream out(Poco::Path::temp() + "pocobin.zip", std::ios::binary);
Poco::File aFile("some/");
- if (aFile.exists())
- aFile.remove(true);
+ if (aFile.exists()) aFile.remove(true);
Poco::File aDir("some/recursive/dir/");
aDir.createDirectories();
Poco::File aDir2("some/other/recursive/dir/");
@@ -65,19 +64,20 @@ void CompressTest::testDirectory()
Compress c(out, true);
c.addRecursive(theFile, ZipCommon::CL_MAXIMUM, false, theFile);
ZipArchive a(c.close());
+ Poco::File(aFile).remove(true);
}
void CompressTest::testManipulator()
{
{
- std::ofstream out("appinf.zip", std::ios::binary);
+ std::ofstream out(Poco::Path::temp() + "appinf.zip", std::ios::binary);
Poco::Path theFile(ZipTest::getTestFile("test.zip"));
Compress c(out, true);
c.addFile(theFile, theFile.getFileName());
ZipArchive a(c.close());
}
- ZipManipulator zm("appinf.zip", true);
+ ZipManipulator zm(Poco::Path::temp() + "appinf.zip", true);
zm.renameFile("test.zip", "renamedtest.zip");
zm.addFile("doc/othertest.zip", ZipTest::getTestFile("test.zip"));
ZipArchive archive=zm.commit();
@@ -88,13 +88,13 @@ void CompressTest::testManipulator()
void CompressTest::testManipulatorDel()
{
{
- std::ofstream out("appinf.zip", std::ios::binary);
+ std::ofstream out(Poco::Path::temp() + "appinf.zip", std::ios::binary);
Poco::Path theFile(ZipTest::getTestFile("test.zip"));
Compress c(out, true);
c.addFile(theFile, theFile.getFileName());
ZipArchive a(c.close());
}
- ZipManipulator zm("appinf.zip", true);
+ ZipManipulator zm(Poco::Path::temp() + "appinf.zip", true);
zm.deleteFile("test.zip");
zm.addFile("doc/data.zip", ZipTest::getTestFile("data.zip"));
ZipArchive archive=zm.commit();
@@ -106,13 +106,13 @@ void CompressTest::testManipulatorDel()
void CompressTest::testManipulatorReplace()
{
{
- std::ofstream out("appinf.zip", std::ios::binary);
+ std::ofstream out(Poco::Path::temp() + "appinf.zip", std::ios::binary);
Poco::Path theFile(ZipTest::getTestFile("test.zip"));
Compress c(out, true);
c.addFile(theFile, theFile.getFileName());
ZipArchive a(c.close());
}
- ZipManipulator zm("appinf.zip", true);
+ ZipManipulator zm(Poco::Path::temp() + "appinf.zip", true);
zm.replaceFile("test.zip", ZipTest::getTestFile("doc.zip"));
ZipArchive archive=zm.commit();
@@ -124,7 +124,7 @@ void CompressTest::testManipulatorReplace()
void CompressTest::testSetZipComment()
{
std::string comment("Testing...123...");
- std::ofstream out("comment.zip", std::ios::binary);
+ std::ofstream out(Poco::Path::temp() + "comment.zip", std::ios::binary);
Poco::Path theFile(ZipTest::getTestFile("test.zip"));
Compress c(out, true);
c.addFile(theFile, theFile.getFileName());
diff --git a/Zip/testsuite/src/ZipTest.cpp b/Zip/testsuite/src/ZipTest.cpp
index 0b89bd9..42562f9 100644
--- a/Zip/testsuite/src/ZipTest.cpp
+++ b/Zip/testsuite/src/ZipTest.cpp
@@ -51,16 +51,16 @@ void ZipTest::testSkipSingleFile()
ZipLocalFileHeader hdr(inp, false, skip);
assert (ZipCommon::HS_FAT == hdr.getHostSystem());
int major = hdr.getMajorVersionNumber();
- int minor = hdr.getMinorVersionNumber();
+ int POCO_UNUSED minor = hdr.getMinorVersionNumber();
assert (major <= 2);
std::size_t hdrSize = hdr.getHeaderSize();
assert (hdrSize > 30);
- ZipCommon::CompressionMethod cm = hdr.getCompressionMethod();
+ ZipCommon::CompressionMethod POCO_UNUSED cm = hdr.getCompressionMethod();
assert (!hdr.isEncrypted());
Poco::DateTime aDate = hdr.lastModifiedAt();
- Poco::UInt32 cS = hdr.getCompressedSize();
- Poco::UInt32 uS = hdr.getUncompressedSize();
- const std::string& fileName = hdr.getFileName();
+ Poco::UInt64 POCO_UNUSED cS = hdr.getCompressedSize();
+ Poco::UInt64 POCO_UNUSED uS = hdr.getUncompressedSize();
+ const std::string& POCO_UNUSED fileName = hdr.getFileName();
}
@@ -84,7 +84,7 @@ void ZipTest::testCrcAndSizeAfterData()
std::string testFile = getTestFile("data.zip");
std::ifstream inp(testFile.c_str(), std::ios::binary);
assert (inp.good());
- Decompress dec(inp, Poco::Path());
+ Decompress dec(inp, Poco::Path::temp());
dec.EError += Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
dec.decompressAllFiles();
dec.EError -= Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
@@ -108,7 +108,7 @@ void ZipTest::testCrcAndSizeAfterDataWithArchive()
Poco::Path path(it->second.getFileName());
if (path.isFile())
{
- std::ofstream os("test.dat");
+ std::ofstream os(Poco::Path::temp() + "test.dat");
Poco::StreamCopier::copyStream(zipis,os);
}
}
@@ -141,7 +141,7 @@ void ZipTest::testDecompress()
std::string testFile = getTestFile("test.zip");
std::ifstream inp(testFile.c_str(), std::ios::binary);
assert (inp.good());
- Decompress dec(inp, Poco::Path());
+ Decompress dec(inp, Poco::Path::temp());
dec.EError += Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
dec.decompressAllFiles();
dec.EError -= Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
@@ -155,7 +155,7 @@ void ZipTest::testDecompressFlat()
std::string testFile = getTestFile("test.zip");
std::ifstream inp(testFile.c_str(), std::ios::binary);
assert (inp.good());
- Decompress dec(inp, Poco::Path(), true);
+ Decompress dec(inp, Poco::Path::temp(), true);
dec.EError += Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
dec.decompressAllFiles();
dec.EError -= Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
@@ -164,6 +164,71 @@ void ZipTest::testDecompressFlat()
}
+void ZipTest::testDecompressVuln()
+{
+ std::string testFile = getTestFile("vuln.zip");
+ std::ifstream inp(testFile.c_str(), std::ios::binary);
+ assert(inp.good());
+ Decompress dec(inp, Poco::Path::temp());
+ dec.EError += Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
+ dec.decompressAllFiles();
+ dec.EError -= Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
+ assert (_errCnt == 1);
+ assert (dec.mapping().empty());
+}
+
+
+void ZipTest::testDecompressFlatVuln()
+{
+ std::string testFile = getTestFile("vuln.zip");
+ std::ifstream inp(testFile.c_str(), std::ios::binary);
+ assert(inp.good());
+ Decompress dec(inp, Poco::Path::temp(), true);
+ dec.EError += Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
+ dec.decompressAllFiles();
+ dec.EError -= Poco::Delegate<ZipTest, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &ZipTest::onDecompressError);
+ assert (_errCnt == 0);
+ assert (!dec.mapping().empty());
+}
+
+
+void ZipTest::testValidPath()
+{
+ assert (ZipCommon::isValidPath("."));
+ assert (ZipCommon::isValidPath("file.txt"));
+ assert (ZipCommon::isValidPath(".file.txt"));
+ assert (ZipCommon::isValidPath("..file.txt"));
+ assert (ZipCommon::isValidPath("file.txt.."));
+ assert (ZipCommon::isValidPath(".file..txt"));
+ assert (ZipCommon::isValidPath("~file..txt"));
+ assert (ZipCommon::isValidPath("~file/~"));
+ assert (ZipCommon::isValidPath("dir/~"));
+ assert (ZipCommon::isValidPath("some"));
+ assert (ZipCommon::isValidPath("some/dir"));
+ assert (ZipCommon::isValidPath("some/dir/or/another"));
+ assert (ZipCommon::isValidPath("some/dir/./another"));
+ assert (ZipCommon::isValidPath("some/dir/or/another/file.txt"));
+ assert (ZipCommon::isValidPath("s~me\\d.r\\.or..\\an..her\\file.txt"));
+ assert (ZipCommon::isValidPath("some\\dir\\or\\another"));
+ assert (ZipCommon::isValidPath("some\\dir\\or\\another\\file.txt"));
+ assert (ZipCommon::isValidPath("s~me\\d.r/.or..\\an..her\\file.txt"));
+
+ assert (!ZipCommon::isValidPath("/../"));
+ assert (!ZipCommon::isValidPath("/"));
+ assert (!ZipCommon::isValidPath("\\..\\"));
+ assert (!ZipCommon::isValidPath("/..\\"));
+ assert (!ZipCommon::isValidPath("\\../"));
+ assert (!ZipCommon::isValidPath(".."));
+ assert (!ZipCommon::isValidPath("~/"));
+ assert (!ZipCommon::isValidPath("~/~"));
+ assert (!ZipCommon::isValidPath("/~"));
+ assert (!ZipCommon::isValidPath("/file.txt"));
+ assert (!ZipCommon::isValidPath("~/file.txt"));
+ assert (!ZipCommon::isValidPath("some/dir/or/../another/file.txt"));
+ assert (!ZipCommon::isValidPath("C:\\Windows\\system32"));
+}
+
+
void ZipTest::onDecompressError(const void* pSender, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string>& info)
{
++_errCnt;
@@ -189,7 +254,10 @@ CppUnit::Test* ZipTest::suite()
CppUnit_addTest(pSuite, ZipTest, testDecompressSingleFile);
CppUnit_addTest(pSuite, ZipTest, testDecompress);
CppUnit_addTest(pSuite, ZipTest, testDecompressFlat);
+ CppUnit_addTest(pSuite, ZipTest, testDecompressVuln);
+ CppUnit_addTest(pSuite, ZipTest, testDecompressFlatVuln);
CppUnit_addTest(pSuite, ZipTest, testCrcAndSizeAfterData);
CppUnit_addTest(pSuite, ZipTest, testCrcAndSizeAfterDataWithArchive);
+ CppUnit_addTest(pSuite, ZipTest, testValidPath);
return pSuite;
}
diff --git a/Zip/testsuite/src/ZipTest.h b/Zip/testsuite/src/ZipTest.h
index 8f8adee..17ae48c 100644
--- a/Zip/testsuite/src/ZipTest.h
+++ b/Zip/testsuite/src/ZipTest.h
@@ -30,10 +30,12 @@ public:
void testSkipSingleFile();
void testDecompressSingleFile();
void testDecompress();
+ void testDecompressFlat();
+ void testDecompressVuln();
+ void testDecompressFlatVuln();
void testCrcAndSizeAfterData();
void testCrcAndSizeAfterDataWithArchive();
-
- void testDecompressFlat();
+ void testValidPath();
void setUp();
void tearDown();
|