From 919a93cd4b123f3757ce1ae454ed622019d1d02c Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sun, 14 Apr 2019 12:21:19 -0400 Subject: wsd: improved anonymization algorithm Better hashing algorithm based on FNV-1a. Adds support for salting the hash, and for providing salt via configuration. More unit-tests added, and better formatting. Change-Id: I2be42675d0cdbaa73c3d7faed99e07631a9c20fc Reviewed-on: https://gerrit.libreoffice.org/70034 Reviewed-by: Ashod Nakashian Tested-by: Ashod Nakashian Reviewed-on: https://gerrit.libreoffice.org/71091 --- test/WhiteBoxTests.cpp | 72 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 20 deletions(-) (limited to 'test/WhiteBoxTests.cpp') diff --git a/test/WhiteBoxTests.cpp b/test/WhiteBoxTests.cpp index 8c5bdc70c..39aa197e3 100644 --- a/test/WhiteBoxTests.cpp +++ b/test/WhiteBoxTests.cpp @@ -669,35 +669,67 @@ void WhiteBoxTests::testJson() void WhiteBoxTests::testAnonymization() { static const std::string name = "some name with space"; - CPPUNIT_ASSERT_EQUAL(std::string("#0#77d#"), Util::anonymizeUrl(name)); + static const std::string filename = "filename.ext"; + static const std::string filenameTestx = "testx (6).odt"; + static const std::string path = "/path/to/filename.ext"; + static const std::string plainUrl + = "http://localhost/owncloud/index.php/apps/richdocuments/wopi/files/" + "736_ocgdpzbkm39u?access_token=Hn0zttjbwkvGWb5BHbDa5ArgTykJAyBl&access_token_ttl=0&" + "permission=edit"; + static const std::string fileUrl = "http://localhost/owncloud/index.php/apps/richdocuments/" + "wopi/files/736_ocgdpzbkm39u/" + "secret.odt?access_token=Hn0zttjbwkvGWb5BHbDa5ArgTykJAyBl&" + "access_token_ttl=0&permission=edit"; + + std::uint64_t nAnonymizationSalt = 1111111111182589933; + + CPPUNIT_ASSERT_EQUAL(std::string("#0#5e45aef91248a8aa#"), + Util::anonymizeUrl(name, nAnonymizationSalt)); + CPPUNIT_ASSERT_EQUAL(std::string("#1#8f8d95bd2a202d00#.odt"), + Util::anonymizeUrl(filenameTestx, nAnonymizationSalt)); + CPPUNIT_ASSERT_EQUAL(std::string("/path/to/#2#5c872b2d82ecc8a0#.ext"), + Util::anonymizeUrl(path, nAnonymizationSalt)); + CPPUNIT_ASSERT_EQUAL( + std::string("http://localhost/owncloud/index.php/apps/richdocuments/wopi/files/" + "#3#22c6f0caad277666#?access_token=Hn0zttjbwkvGWb5BHbDa5ArgTykJAyBl&access_" + "token_ttl=0&permission=edit"), + Util::anonymizeUrl(plainUrl, nAnonymizationSalt)); + CPPUNIT_ASSERT_EQUAL( + std::string("http://localhost/owncloud/index.php/apps/richdocuments/wopi/files/" + "736_ocgdpzbkm39u/" + "#4#294f0dfb18f6a80b#.odt?access_token=Hn0zttjbwkvGWb5BHbDa5ArgTykJAyBl&access_" + "token_ttl=0&permission=edit"), + Util::anonymizeUrl(fileUrl, nAnonymizationSalt)); + + nAnonymizationSalt = 0; + + CPPUNIT_ASSERT_EQUAL(std::string("#0#5e45aef91248a8aa#"), Util::anonymizeUrl(name, nAnonymizationSalt)); Util::mapAnonymized(name, name); - CPPUNIT_ASSERT_EQUAL(name, Util::anonymizeUrl(name)); + CPPUNIT_ASSERT_EQUAL(name, Util::anonymizeUrl(name, nAnonymizationSalt)); - static const std::string filename = "filename.ext"; - CPPUNIT_ASSERT_EQUAL(std::string("#1#341#.ext"), Util::anonymizeUrl(filename)); - Util::mapAnonymized("filename", "filename"); - CPPUNIT_ASSERT_EQUAL(name, Util::anonymizeUrl(name)); + CPPUNIT_ASSERT_EQUAL(std::string("#2#5c872b2d82ecc8a0#.ext"), + Util::anonymizeUrl(filename, nAnonymizationSalt)); + Util::mapAnonymized("filename", "filename"); // Identity map of the filename without extension. + CPPUNIT_ASSERT_EQUAL(filename, Util::anonymizeUrl(filename, nAnonymizationSalt)); - static const std::string filenameTestx = "testx (6).odt"; - CPPUNIT_ASSERT_EQUAL(std::string("#2#2df#.odt"), Util::anonymizeUrl(filenameTestx)); - Util::mapAnonymized("testx (6)", "testx (6)"); - CPPUNIT_ASSERT_EQUAL(filenameTestx, Util::anonymizeUrl(filenameTestx)); + CPPUNIT_ASSERT_EQUAL(std::string("#1#8f8d95bd2a202d00#.odt"), + Util::anonymizeUrl(filenameTestx, nAnonymizationSalt)); + Util::mapAnonymized("testx (6)", + "testx (6)"); // Identity map of the filename without extension. + CPPUNIT_ASSERT_EQUAL(filenameTestx, Util::anonymizeUrl(filenameTestx, nAnonymizationSalt)); - static const std::string path = "/path/to/filename.ext"; - CPPUNIT_ASSERT_EQUAL(path, Util::anonymizeUrl(path)); + CPPUNIT_ASSERT_EQUAL(path, Util::anonymizeUrl(path, nAnonymizationSalt)); - static const std::string plainUrl = "http://localhost/owncloud/index.php/apps/richdocuments/wopi/files/736_ocgdpzbkm39u?access_token=Hn0zttjbwkvGWb5BHbDa5ArgTykJAyBl&access_token_ttl=0&permission=edit"; - const std::string urlAnonymized = Util::replace(plainUrl, "736_ocgdpzbkm39u", "#3#5a1#"); - CPPUNIT_ASSERT_EQUAL(urlAnonymized, Util::anonymizeUrl(plainUrl)); + const std::string urlAnonymized = Util::replace(plainUrl, "736_ocgdpzbkm39u", "#3#22c6f0caad277666#"); + CPPUNIT_ASSERT_EQUAL(urlAnonymized, Util::anonymizeUrl(plainUrl, nAnonymizationSalt)); Util::mapAnonymized("736_ocgdpzbkm39u", "736_ocgdpzbkm39u"); - CPPUNIT_ASSERT_EQUAL(plainUrl, Util::anonymizeUrl(plainUrl)); + CPPUNIT_ASSERT_EQUAL(plainUrl, Util::anonymizeUrl(plainUrl, nAnonymizationSalt)); - static const std::string fileUrl = "http://localhost/owncloud/index.php/apps/richdocuments/wopi/files/736_ocgdpzbkm39u/secret.odt?access_token=Hn0zttjbwkvGWb5BHbDa5ArgTykJAyBl&access_token_ttl=0&permission=edit"; - const std::string urlAnonymized2 = Util::replace(fileUrl, "secret", "#4#286#"); - CPPUNIT_ASSERT_EQUAL(urlAnonymized2, Util::anonymizeUrl(fileUrl)); + const std::string urlAnonymized2 = Util::replace(fileUrl, "secret", "#4#294f0dfb18f6a80b#"); + CPPUNIT_ASSERT_EQUAL(urlAnonymized2, Util::anonymizeUrl(fileUrl, nAnonymizationSalt)); Util::mapAnonymized("secret", "736_ocgdpzbkm39u"); const std::string urlAnonymized3 = Util::replace(fileUrl, "secret", "736_ocgdpzbkm39u"); - CPPUNIT_ASSERT_EQUAL(urlAnonymized3, Util::anonymizeUrl(fileUrl)); + CPPUNIT_ASSERT_EQUAL(urlAnonymized3, Util::anonymizeUrl(fileUrl, nAnonymizationSalt)); } CPPUNIT_TEST_SUITE_REGISTRATION(WhiteBoxTests); -- cgit v1.2.3