| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
#include <QtTest/QtTest> |
| 30 |
|
| 31 |
#include <QtCore/qdatetime.h> |
| 32 |
#include <QtCore/qvector.h> |
| 33 |
#include <QtCore/qpair.h> |
| 34 |
#include <QtCore/qurl.h> |
| 35 |
#include <QtCore/qdir.h> |
| 36 |
|
| 37 |
#include <QtNetwork/private/qhstsstore_p.h> |
| 38 |
#include <QtNetwork/private/qhsts_p.h> |
| 39 |
|
| 40 |
QT_USE_NAMESPACE |
| 41 |
|
| 42 |
class tst_QHsts : public QObject |
| 43 |
{ |
| 44 |
Q_OBJECT |
| 45 |
private Q_SLOTS: |
| 46 |
void testSingleKnownHost_data(); |
| 47 |
void testSingleKnownHost(); |
| 48 |
void testMultilpeKnownHosts(); |
| 49 |
void testPolicyExpiration(); |
| 50 |
void testSTSHeaderParser(); |
| 51 |
void testStore(); |
| 52 |
}; |
| 53 |
|
| 54 |
void tst_QHsts::testSingleKnownHost_data() |
| 55 |
{ |
| 56 |
QTest::addColumn<QUrl>("knownHost"); |
| 57 |
QTest::addColumn<QDateTime>("policyExpires"); |
| 58 |
QTest::addColumn<bool>("includeSubDomains"); |
| 59 |
QTest::addColumn<QUrl>("hostToTest"); |
| 60 |
QTest::addColumn<bool>("isKnown"); |
| 61 |
|
| 62 |
const QDateTime currentUTC = QDateTime::currentDateTimeUtc(); |
| 63 |
const QUrl knownHost(QLatin1String("http://example.com")); |
| 64 |
const QUrl validSubdomain(QLatin1String("https://sub.example.com/ohoho")); |
| 65 |
const QUrl unknownDomain(QLatin1String("http://example.org")); |
| 66 |
const QUrl subSubdomain(QLatin1String("https://level3.level2.example.com")); |
| 67 |
|
| 68 |
const QDateTime validDate(currentUTC.addSecs(1000)); |
| 69 |
QTest::newRow("same-known") << knownHost << validDate << false << knownHost << true; |
| 70 |
QTest::newRow("subexcluded") << knownHost << validDate << false << validSubdomain << false; |
| 71 |
QTest::newRow("subincluded") << knownHost << validDate << true << validSubdomain << true; |
| 72 |
QTest::newRow("unknown-subexcluded") << knownHost << validDate << false << unknownDomain << false; |
| 73 |
QTest::newRow("unknown-subincluded") << knownHost << validDate << true << unknownDomain << false; |
| 74 |
QTest::newRow("sub-subdomain-subincluded") << knownHost << validDate << true << subSubdomain << true; |
| 75 |
QTest::newRow("sub-subdomain-subexcluded") << knownHost << validDate << false << subSubdomain << false; |
| 76 |
|
| 77 |
const QDateTime invalidDate; |
| 78 |
QTest::newRow("invalid-time") << knownHost << invalidDate << false << knownHost << false; |
| 79 |
QTest::newRow("invalid-time-subexcluded") << knownHost << invalidDate << false |
| 80 |
<< validSubdomain << false; |
| 81 |
QTest::newRow("invalid-time-subincluded") << knownHost << invalidDate << true |
| 82 |
<< validSubdomain << false; |
| 83 |
|
| 84 |
const QDateTime expiredDate(currentUTC.addSecs(-1000)); |
| 85 |
QTest::newRow("expired-time") << knownHost << expiredDate << false << knownHost << false; |
| 86 |
QTest::newRow("expired-time-subexcluded") << knownHost << expiredDate << false |
| 87 |
<< validSubdomain << false; |
| 88 |
QTest::newRow("expired-time-subincluded") << knownHost << expiredDate << true |
| 89 |
<< validSubdomain << false; |
| 90 |
const QUrl ipAsHost(QLatin1String("http://127.0.0.1")); |
| 91 |
QTest::newRow("ip-address-in-hostname") << ipAsHost << validDate << false |
| 92 |
<< ipAsHost << false; |
| 93 |
|
| 94 |
const QUrl anyIPv4AsHost(QLatin1String("http://0.0.0.0")); |
| 95 |
QTest::newRow("anyip4-address-in-hostname") << anyIPv4AsHost << validDate |
| 96 |
<< false << anyIPv4AsHost << false; |
| 97 |
const QUrl anyIPv6AsHost(QLatin1String("http://[::]")); |
| 98 |
QTest::newRow("anyip6-address-in-hostname") << anyIPv6AsHost << validDate |
| 99 |
<< false << anyIPv6AsHost << false; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
void tst_QHsts::testSingleKnownHost() |
| 104 |
{ |
| 105 |
QFETCH(const QUrl, knownHost); |
| 106 |
QFETCH(const QDateTime, policyExpires); |
| 107 |
QFETCH(const bool, includeSubDomains); |
| 108 |
QFETCH(const QUrl, hostToTest); |
| 109 |
QFETCH(const bool, isKnown); |
| 110 |
|
| 111 |
QHstsCache cache; |
| 112 |
cache.updateKnownHost(knownHost, policyExpires, includeSubDomains); |
| 113 |
QCOMPARE(cache.isKnownHost(hostToTest), isKnown); |
| 114 |
} |
| 115 |
|
| 116 |
void tst_QHsts::testMultilpeKnownHosts() |
| 117 |
{ |
| 118 |
const QDateTime currentUTC = QDateTime::currentDateTimeUtc(); |
| 119 |
const QDateTime validDate(currentUTC.addSecs(10000)); |
| 120 |
const QDateTime expiredDate(currentUTC.addSecs(-10000)); |
| 121 |
const QUrl exampleCom(QLatin1String("https://example.com")); |
| 122 |
const QUrl subExampleCom(QLatin1String("https://sub.example.com")); |
| 123 |
|
| 124 |
QHstsCache cache; |
| 125 |
// example.com is HSTS and includes subdomains: |
| 126 |
cache.updateKnownHost(exampleCom, validDate, true); |
| 127 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 128 |
QVERIFY(cache.isKnownHost(subExampleCom)); |
| 129 |
// example.com can set its policy not to include subdomains: |
| 130 |
cache.updateKnownHost(exampleCom, validDate, false); |
| 131 |
QVERIFY(!cache.isKnownHost(subExampleCom)); |
| 132 |
// but sub.example.com can set its own policy: |
| 133 |
cache.updateKnownHost(subExampleCom, validDate, false); |
| 134 |
QVERIFY(cache.isKnownHost(subExampleCom)); |
| 135 |
// let's say example.com's policy has expired: |
| 136 |
cache.updateKnownHost(exampleCom, expiredDate, false); |
| 137 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 138 |
// it should not affect sub.example.com's policy: |
| 139 |
QVERIFY(cache.isKnownHost(subExampleCom)); |
| 140 |
|
| 141 |
// clear cache and invalidate all policies: |
| 142 |
cache.clear(); |
| 143 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 144 |
QVERIFY(!cache.isKnownHost(subExampleCom)); |
| 145 |
|
| 146 |
// siblings: |
| 147 |
const QUrl anotherSub(QLatin1String("https://sub2.example.com")); |
| 148 |
cache.updateKnownHost(subExampleCom, validDate, true); |
| 149 |
cache.updateKnownHost(anotherSub, validDate, true); |
| 150 |
QVERIFY(cache.isKnownHost(subExampleCom)); |
| 151 |
QVERIFY(cache.isKnownHost(anotherSub)); |
| 152 |
// they cannot set superdomain's policy: |
| 153 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 154 |
// a sibling cannot set another sibling's policy: |
| 155 |
cache.updateKnownHost(anotherSub, expiredDate, false); |
| 156 |
QVERIFY(cache.isKnownHost(subExampleCom)); |
| 157 |
QVERIFY(!cache.isKnownHost(anotherSub)); |
| 158 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 159 |
// let's make example.com known again: |
| 160 |
cache.updateKnownHost(exampleCom, validDate, true); |
| 161 |
// a subdomain cannot affect its superdomain's policy: |
| 162 |
cache.updateKnownHost(subExampleCom, expiredDate, true); |
| 163 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 164 |
// and this superdomain includes subdomains in its HSTS policy: |
| 165 |
QVERIFY(cache.isKnownHost(subExampleCom)); |
| 166 |
QVERIFY(cache.isKnownHost(anotherSub)); |
| 167 |
|
| 168 |
// a subdomain (with its subdomains) cannot affect its superdomain's policy: |
| 169 |
cache.updateKnownHost(exampleCom, expiredDate, true); |
| 170 |
cache.updateKnownHost(subExampleCom, validDate, true); |
| 171 |
QVERIFY(cache.isKnownHost(subExampleCom)); |
| 172 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 173 |
} |
| 174 |
|
| 175 |
void tst_QHsts::testPolicyExpiration() |
| 176 |
{ |
| 177 |
QDateTime currentUTC = QDateTime::currentDateTimeUtc(); |
| 178 |
const QUrl exampleCom(QLatin1String("http://example.com")); |
| 179 |
const QUrl subdomain(QLatin1String("http://subdomain.example.com")); |
| 180 |
const qint64 lifeTimeMS = 50; |
| 181 |
|
| 182 |
QHstsCache cache; |
| 183 |
// start with 'includeSubDomains' and 5 s. lifetime: |
| 184 |
cache.updateKnownHost(exampleCom, currentUTC.addMSecs(lifeTimeMS), true); |
| 185 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 186 |
QVERIFY(cache.isKnownHost(subdomain)); |
| 187 |
// wait for approx. a half of lifetime: |
| 188 |
QTest::qWait(lifeTimeMS / 2); |
| 189 |
|
| 190 |
if (QDateTime::currentDateTimeUtc() < currentUTC.addMSecs(lifeTimeMS)) { |
| 191 |
// Should still be valid: |
| 192 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 193 |
QVERIFY(cache.isKnownHost(subdomain)); |
| 194 |
} |
| 195 |
|
| 196 |
QTest::qWait(lifeTimeMS); |
| 197 |
// expired: |
| 198 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 199 |
QVERIFY(!cache.isKnownHost(subdomain)); |
| 200 |
|
| 201 |
// now check that superdomain's policy expires, but not subdomain's policy: |
| 202 |
currentUTC = QDateTime::currentDateTimeUtc(); |
| 203 |
cache.updateKnownHost(exampleCom, currentUTC.addMSecs(lifeTimeMS / 5), true); |
| 204 |
cache.updateKnownHost(subdomain, currentUTC.addMSecs(lifeTimeMS), true); |
| 205 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 206 |
QVERIFY(cache.isKnownHost(subdomain)); |
| 207 |
QTest::qWait(lifeTimeMS / 2); |
| 208 |
if (QDateTime::currentDateTimeUtc() < currentUTC.addMSecs(lifeTimeMS)) { |
| 209 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 210 |
QVERIFY(cache.isKnownHost(subdomain)); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void tst_QHsts::testSTSHeaderParser() |
| 215 |
{ |
| 216 |
QHstsHeaderParser parser; |
| 217 |
using Header = QPair<QByteArray, QByteArray>; |
| 218 |
using Headers = QList<Header>; |
| 219 |
|
| 220 |
QVERIFY(!parser.includeSubDomains()); |
| 221 |
QVERIFY(!parser.expirationDate().isValid()); |
| 222 |
Headers list; |
| 223 |
QVERIFY(!parser.parse(list)); |
| 224 |
QVERIFY(!parser.includeSubDomains()); |
| 225 |
QVERIFY(!parser.expirationDate().isValid()); |
| 226 |
|
| 227 |
list << Header("Strict-Transport-security", "200"); |
| 228 |
QVERIFY(!parser.parse(list)); |
| 229 |
QVERIFY(!parser.includeSubDomains()); |
| 230 |
QVERIFY(!parser.expirationDate().isValid()); |
| 231 |
|
| 232 |
// This header is missing REQUIRED max-age directive, so we'll ignore it: |
| 233 |
list << Header("Strict-Transport-Security", "includeSubDomains"); |
| 234 |
QVERIFY(!parser.parse(list)); |
| 235 |
QVERIFY(!parser.includeSubDomains()); |
| 236 |
QVERIFY(!parser.expirationDate().isValid()); |
| 237 |
|
| 238 |
list.pop_back(); |
| 239 |
list << Header("Strict-Transport-Security", "includeSubDomains;max-age=1000"); |
| 240 |
QVERIFY(parser.parse(list)); |
| 241 |
QVERIFY(parser.expirationDate() > QDateTime::currentDateTimeUtc()); |
| 242 |
QVERIFY(parser.includeSubDomains()); |
| 243 |
|
| 244 |
list.pop_back(); |
| 245 |
list << Header("strict-transport-security", "includeSubDomains;max-age=1000"); |
| 246 |
QVERIFY(parser.parse(list)); |
| 247 |
QVERIFY(parser.expirationDate() > QDateTime::currentDateTimeUtc()); |
| 248 |
QVERIFY(parser.includeSubDomains()); |
| 249 |
|
| 250 |
list.pop_back(); |
| 251 |
// Invalid (includeSubDomains twice): |
| 252 |
list << Header("Strict-Transport-Security", "max-age = 1000 ; includeSubDomains;includeSubDomains"); |
| 253 |
QVERIFY(!parser.parse(list)); |
| 254 |
QVERIFY(!parser.includeSubDomains()); |
| 255 |
QVERIFY(!parser.expirationDate().isValid()); |
| 256 |
|
| 257 |
list.pop_back(); |
| 258 |
// Invalid (weird number of seconds): |
| 259 |
list << Header("Strict-Transport-Security", "max-age=-1000 ; includeSubDomains"); |
| 260 |
QVERIFY(!parser.parse(list)); |
| 261 |
QVERIFY(!parser.includeSubDomains()); |
| 262 |
QVERIFY(!parser.expirationDate().isValid()); |
| 263 |
|
| 264 |
list.pop_back(); |
| 265 |
// Note, directives are case-insensitive + we should ignore unknown directive. |
| 266 |
list << Header("Strict-Transport-Security", ";max-age=1000 ;includesubdomains;;" |
| 267 |
"nowsomeunknownheader=\"somevaluewithescapes\\;\""); |
| 268 |
QVERIFY(parser.parse(list)); |
| 269 |
QVERIFY(parser.includeSubDomains()); |
| 270 |
QVERIFY(parser.expirationDate().isValid()); |
| 271 |
|
| 272 |
list.pop_back(); |
| 273 |
// Check that we know how to unescape max-age: |
| 274 |
list << Header("Strict-Transport-Security", "max-age=\"1000\""); |
| 275 |
QVERIFY(parser.parse(list)); |
| 276 |
QVERIFY(!parser.includeSubDomains()); |
| 277 |
QVERIFY(parser.expirationDate().isValid()); |
| 278 |
|
| 279 |
list.pop_back(); |
| 280 |
// The only STS header, with invalid syntax though, to be ignored: |
| 281 |
list << Header("Strict-Transport-Security", "max-age; max-age=15768000"); |
| 282 |
QVERIFY(!parser.parse(list)); |
| 283 |
QVERIFY(!parser.includeSubDomains()); |
| 284 |
QVERIFY(!parser.expirationDate().isValid()); |
| 285 |
|
| 286 |
// Now we check that our parse chosses the first valid STS header and ignores |
| 287 |
// others: |
| 288 |
list.clear(); |
| 289 |
list << Header("Strict-Transport-Security", "includeSubdomains; max-age=\"hehehe\";"); |
| 290 |
list << Header("Strict-Transport-Security", "max-age=10101"); |
| 291 |
QVERIFY(parser.parse(list)); |
| 292 |
QVERIFY(!parser.includeSubDomains()); |
| 293 |
QVERIFY(parser.expirationDate().isValid()); |
| 294 |
|
| 295 |
|
| 296 |
list.clear(); |
| 297 |
list << Header("Strict-Transport-Security", "max-age=0"); |
| 298 |
QVERIFY(parser.parse(list)); |
| 299 |
QVERIFY(!parser.includeSubDomains()); |
| 300 |
QVERIFY(parser.expirationDate() <= QDateTime::currentDateTimeUtc()); |
| 301 |
|
| 302 |
// Parsing is case-insensitive: |
| 303 |
list.pop_back(); |
| 304 |
list << Header("Strict-Transport-Security", "Max-aGE=1000; InclUdesUbdomains"); |
| 305 |
QVERIFY(parser.parse(list)); |
| 306 |
QVERIFY(parser.includeSubDomains()); |
| 307 |
QVERIFY(parser.expirationDate().isValid()); |
| 308 |
|
| 309 |
// Grammar of STS header is quite permissive, let's check we can parse |
| 310 |
// some weird but valid header: |
| 311 |
list.pop_back(); |
| 312 |
list << Header("Strict-Transport-Security", ";;; max-age = 17; ; ; ; ;;; ;;" |
| 313 |
";;; ; includeSubdomains ;;thisIsUnknownDirective;;;;"); |
| 314 |
QVERIFY(parser.parse(list)); |
| 315 |
QVERIFY(parser.includeSubDomains()); |
| 316 |
QVERIFY(parser.expirationDate().isValid()); |
| 317 |
|
| 318 |
list.pop_back(); |
| 319 |
list << Header("Strict-Transport-Security", "max-age=1000; includeSubDomains bogon"); |
| 320 |
QVERIFY(!parser.parse(list)); |
| 321 |
QVERIFY(!parser.includeSubDomains()); |
| 322 |
QVERIFY(!parser.expirationDate().isValid()); |
| 323 |
} |
| 324 |
|
| 325 |
const QLatin1String storeDir("."); |
| 326 |
|
| 327 |
struct TestStoreDeleter |
| 328 |
{ |
| 329 |
~TestStoreDeleter() |
| 330 |
{ |
| 331 |
QDir cwd; |
| 332 |
if (!cwd.remove(QHstsStore::absoluteFilePath(storeDir))) |
| 333 |
qWarning() << "tst_QHsts::testStore: failed to remove the hsts store file"; |
| 334 |
} |
| 335 |
}; |
| 336 |
|
| 337 |
void tst_QHsts::testStore() |
| 338 |
{ |
| 339 |
// Delete the store's file after we finish the test. |
| 340 |
TestStoreDeleter cleaner; |
| 341 |
|
| 342 |
const QUrl exampleCom(QStringLiteral("http://example.com")); |
| 343 |
const QUrl subDomain(QStringLiteral("http://subdomain.example.com")); |
| 344 |
const QDateTime validDate(QDateTime::currentDateTimeUtc().addDays(1)); |
| 345 |
|
| 346 |
{ |
| 347 |
// We start from an empty cache and empty store: |
| 348 |
QHstsCache cache; |
| 349 |
QHstsStore store(storeDir); |
| 350 |
cache.setStore(&store); |
| 351 |
QVERIFY(!cache.isKnownHost(exampleCom)); |
| 352 |
QVERIFY(!cache.isKnownHost(subDomain)); |
| 353 |
// (1) This will also store the policy: |
| 354 |
cache.updateKnownHost(exampleCom, validDate, true); |
| 355 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 356 |
QVERIFY(cache.isKnownHost(subDomain)); |
| 357 |
} |
| 358 |
{ |
| 359 |
// Test the policy stored at (1): |
| 360 |
QHstsCache cache; |
| 361 |
QHstsStore store(storeDir); |
| 362 |
cache.setStore(&store); |
| 363 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 364 |
QVERIFY(cache.isKnownHost(subDomain)); |
| 365 |
// (2) Remove subdomains: |
| 366 |
cache.updateKnownHost(exampleCom, validDate, false); |
| 367 |
QVERIFY(!cache.isKnownHost(subDomain)); |
| 368 |
} |
| 369 |
{ |
| 370 |
// Test the previous update (2): |
| 371 |
QHstsCache cache; |
| 372 |
QHstsStore store(storeDir); |
| 373 |
cache.setStore(&store); |
| 374 |
QVERIFY(cache.isKnownHost(exampleCom)); |
| 375 |
QVERIFY(!cache.isKnownHost(subDomain)); |
| 376 |
} |
| 377 |
{ |
| 378 |
QHstsCache cache; |
| 379 |
cache.updateKnownHost(subDomain, validDate, false); |
| 380 |
QVERIFY(cache.isKnownHost(subDomain)); |
| 381 |
QHstsStore store(storeDir); |
| 382 |
// (3) This should store policy from cache, over old policy from store: |
| 383 |
cache.setStore(&store); |
| 384 |
} |
| 385 |
{ |
| 386 |
// Test that (3) was stored: |
| 387 |
QHstsCache cache; |
| 388 |
QHstsStore store(storeDir); |
| 389 |
cache.setStore(&store); |
| 390 |
QVERIFY(cache.isKnownHost(subDomain)); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
QTEST_MAIN(tst_QHsts) |
| 395 |
|
| 396 |
#include "tst_qhsts.moc" |