Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtldurl.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
5#include <QtNetwork/private/qtnetworkglobal_p.h>
6
7#if QT_CONFIG(topleveldomain)
8
9#include "QtCore/qfile.h"
10#include "QtCore/qloggingcategory.h"
11#include "QtCore/qstandardpaths.h"
12#include "QtCore/qstring.h"
13
14#if !QT_CONFIG(publicsuffix_qt) && !QT_CONFIG(publicsuffix_system)
15# error Enable at least one feature: publicsuffix-qt, publicsuffix-system
16#endif
17
18#if QT_CONFIG(publicsuffix_qt)
19# include "psl_data.cpp"
20#endif
21
22// Defined in src/3rdparty/libpsl/src/lookup_string_in_fixed_set.c
23extern "C" int LookupStringInFixedSet(const unsigned char *graph, std::size_t length,
24 const char *key, std::size_t key_length);
25
26QT_BEGIN_NAMESPACE
27
28using namespace Qt::StringLiterals;
29
30#if QT_CONFIG(publicsuffix_system)
31Q_STATIC_LOGGING_CATEGORY(lcTld, "qt.network.tld")
32#endif
33
34static constexpr int PSL_NOT_FOUND = -1;
35static constexpr int PSL_FLAG_EXCEPTION = 1 << 0;
36static constexpr int PSL_FLAG_WILDCARD = 1 << 1;
37
38class QPublicSuffixDatabase final
39{
40public:
41#if QT_CONFIG(publicsuffix_system)
42 QPublicSuffixDatabase();
43#endif // QT_CONFIG(publicsuffix_system)
44
45 int lookupDomain(QByteArrayView domain) const;
46
47private:
48 QByteArrayView m_data
49#if QT_CONFIG(publicsuffix_qt)
50 {
51 kDafsa, sizeof(kDafsa)
52 }
53#endif // QT_CONFIG(publicsuffix_qt)
54 ;
55
56#if QT_CONFIG(publicsuffix_system)
57 std::unique_ptr<QFile> m_dev;
58 QByteArray m_storage;
59 bool loadFile(const QString &fileName);
60#endif // QT_CONFIG(publicsuffix_system)
61};
62
63int QPublicSuffixDatabase::lookupDomain(QByteArrayView domain) const
64{
65 return LookupStringInFixedSet(reinterpret_cast<const unsigned char *>(m_data.constData()),
66 m_data.size(), domain.data(), domain.size());
67}
68
69#if QT_CONFIG(publicsuffix_system)
70
71static QStringList locatePublicSuffixFiles()
72{
73 return QStandardPaths::locateAll(QStandardPaths::GenericDataLocation,
74 u"publicsuffix/public_suffix_list.dafsa"_s);
75}
76
77QPublicSuffixDatabase::QPublicSuffixDatabase()
78{
79 for (auto &&fileName : locatePublicSuffixFiles()) {
80 if (loadFile(fileName))
81 return;
82 }
83
84#if QT_CONFIG(publicsuffix_qt)
85 qCDebug(lcTld, "Using builtin publicsuffix list");
86#else
87 qCWarning(lcTld, "No usable publicsuffix file found");
88#endif
89}
90
91bool QPublicSuffixDatabase::loadFile(const QString &fileName)
92{
93 static const QByteArrayView DafsaFileHeader = ".DAFSA@PSL_0 \n";
94
95 qCDebug(lcTld, "Loading publicsuffix file: %s", qUtf8Printable(fileName));
96
97 auto systemFile = std::make_unique<QFile>(fileName);
98
99 if (!systemFile->open(QIODevice::ReadOnly)) {
100 qCDebug(lcTld, "Failed to open publicsuffix file: %s",
101 qUtf8Printable(systemFile->errorString()));
102 return false;
103 }
104
105 auto fileSize = systemFile->size();
106 // Check if there is enough data for header, version byte and some data
107 if (fileSize < DafsaFileHeader.size() + 2) {
108 qCWarning(lcTld, "publicsuffix file is too small: %zu", std::size_t(fileSize));
109 return false;
110 }
111
112 auto header = systemFile->read(DafsaFileHeader.size());
113 if (header != DafsaFileHeader) {
114 qCWarning(lcTld, "Invalid publicsuffix file header: %s", header.toHex().constData());
115 return false;
116 }
117
118 // Check if the file is UTF-8 compatible
119 if (!systemFile->seek(fileSize - 1)) {
120 qCWarning(lcTld, "Failed to seek to the end of file: %s",
121 qUtf8Printable(systemFile->errorString()));
122 return false;
123 }
124
125 char version;
126 if (systemFile->read(&version, 1) != 1) {
127 qCWarning(lcTld, "Failed to read publicsuffix version");
128 return false;
129 }
130
131 if (version != 0x01) {
132 qCWarning(lcTld, "Unsupported publicsuffix version: %d", int(version));
133 return false;
134 }
135
136 const auto dataSize = fileSize - DafsaFileHeader.size() - 1;
137 // Try to map the file first
138 auto mappedData = systemFile->map(DafsaFileHeader.size(), dataSize);
139 if (mappedData) {
140 qCDebug(lcTld, "Using mapped system publicsuffix data");
141 systemFile->close();
142 m_data = QByteArrayView(mappedData, dataSize);
143 m_dev = std::move(systemFile);
144 return true;
145 }
146
147 qCDebug(lcTld, "Failed to map publicsuffix file: %s",
148 qUtf8Printable(systemFile->errorString()));
149
150 systemFile->seek(DafsaFileHeader.size());
151 m_storage = systemFile->read(dataSize);
152 if (m_storage.size() != dataSize) {
153 qCWarning(lcTld, "Failed to read publicsuffix file");
154 m_storage.clear();
155 return false;
156 }
157
158 qCDebug(lcTld, "Using system publicsuffix data");
159 m_data = m_storage;
160
161 return true;
162}
163
164Q_GLOBAL_STATIC(QPublicSuffixDatabase, publicSuffix);
165
166#else
167
168static const QPublicSuffixDatabase m_publicSuffix;
169
170#endif // QT_CONFIG(publicsuffix_system)
171
172/*!
173 \internal
174
175 Return true if \a domain is a top-level-domain per Qt's copy of the Mozilla public suffix list.
176
177 The \a domain must be in lower-case format (as per QString::toLower()).
178*/
179
180Q_NETWORK_EXPORT bool qIsEffectiveTLD(QStringView domain)
181{
182 // for domain 'foo.bar.com':
183 // 1. return false if TLD table contains '!foo.bar.com'
184 // 2. return true if TLD table contains 'foo.bar.com'
185 // 3. return true if the table contains '*.bar.com'
186
187 QByteArray decodedDomain = domain.toUtf8();
188 QByteArrayView domainView(decodedDomain);
189
190#if QT_CONFIG(publicsuffix_system)
191 if (publicSuffix.isDestroyed())
192 return false;
193#else
194 auto publicSuffix = &m_publicSuffix;
195#endif // QT_CONFIG(publicsuffix_system)
196
197 auto ret = publicSuffix->lookupDomain(domainView);
198 if (ret != PSL_NOT_FOUND) {
199 if (ret & PSL_FLAG_EXCEPTION) // 1
200 return false;
201 if ((ret & PSL_FLAG_WILDCARD) == 0) // 2
202 return true;
203 }
204
205 const auto dot = domainView.indexOf('.');
206 if (dot < 0) // Actual TLD: may be effective if the subject of a wildcard rule:
207 return ret != PSL_NOT_FOUND;
208 ret = publicSuffix->lookupDomain(domainView.sliced(dot + 1)); // 3
209 if (ret == PSL_NOT_FOUND)
210 return false;
211 return (ret & PSL_FLAG_WILDCARD) != 0;
212}
213
214QT_END_NAMESPACE
215
216#endif // QT_CONFIG(topleveldomain)