/src/qtbase/src/network/access/qhstspolicy.cpp
Line | Count | Source |
1 | | // Copyright (C) 2017 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:significant reason:default |
4 | | |
5 | | #include "qhstspolicy.h" |
6 | | |
7 | | #include <QtCore/qdatetime.h> |
8 | | #include <QtCore/qstring.h> |
9 | | |
10 | | QT_BEGIN_NAMESPACE |
11 | | |
12 | | /*! |
13 | | \class QHstsPolicy |
14 | | \brief The QHstsPolicy class specifies that a host supports HTTP Strict Transport |
15 | | Security policy (HSTS). |
16 | | \since 5.9 |
17 | | \ingroup network |
18 | | \inmodule QtNetwork |
19 | | |
20 | | HSTS policy defines a period of time during which QNetworkAccessManager |
21 | | should only access a host in a secure fashion. HSTS policy is defined by |
22 | | RFC6797. |
23 | | |
24 | | You can set expiry time and host name for this policy, and control whether it |
25 | | applies to subdomains, either in the constructor or by calling \l setExpiry(), |
26 | | \l setHost() and \l setIncludesSubDomains(). |
27 | | |
28 | | \sa QNetworkAccessManager::setStrictTransportSecurityEnabled() |
29 | | */ |
30 | | |
31 | | /* |
32 | | \enum QHstsPolicy::PolicyFlag |
33 | | |
34 | | Specifies attributes that a policy can have. |
35 | | |
36 | | \value IncludeSubDomains HSTS policy also applies to subdomains. |
37 | | */ |
38 | | |
39 | | class QHstsPolicyPrivate : public QSharedData |
40 | | { |
41 | | public: |
42 | | QUrl url; |
43 | | QDateTime expiry; |
44 | | bool includeSubDomains = false; |
45 | | |
46 | | bool operator == (const QHstsPolicyPrivate &other) const |
47 | 0 | { |
48 | 0 | return url.host() == other.url.host() && expiry == other.expiry |
49 | 0 | && includeSubDomains == other.includeSubDomains; |
50 | 0 | } |
51 | | }; |
52 | | |
53 | | /*! |
54 | | \fn bool QHstsPolicy::operator==(const QHstsPolicy &lhs, const QHstsPolicy &rhs) |
55 | | |
56 | | Returns \c true if the two policies \a lhs and \a rhs have the same host and |
57 | | expiration date while agreeing on whether to include or exclude subdomains. |
58 | | */ |
59 | | |
60 | | /*! |
61 | | \fn bool QHstsPolicy::operator!=(const QHstsPolicy &lhs, const QHstsPolicy &rhs) |
62 | | |
63 | | Returns \c true if the two policies \a lhs and \a rhs do not have the same host |
64 | | or expiration date, or do not agree on whether to include or exclude subdomains. |
65 | | */ |
66 | | |
67 | | /*! |
68 | | \internal |
69 | | */ |
70 | | bool QHstsPolicy::isEqual(const QHstsPolicy &other) const |
71 | 0 | { |
72 | 0 | return *d == *other.d; |
73 | 0 | } |
74 | | |
75 | | /*! |
76 | | Constructs an invalid (expired) policy with empty host name and subdomains |
77 | | not included. |
78 | | */ |
79 | 0 | QHstsPolicy::QHstsPolicy() : d(new QHstsPolicyPrivate) |
80 | 0 | { |
81 | 0 | } |
82 | | |
83 | | /*! |
84 | | \enum QHstsPolicy::PolicyFlag |
85 | | |
86 | | \value IncludeSubDomains Indicates whether a policy must include subdomains |
87 | | */ |
88 | | |
89 | | /*! |
90 | | Constructs QHstsPolicy with \a expiry (in UTC); \a flags is a value indicating |
91 | | whether this policy must also include subdomains, \a host data is interpreted |
92 | | according to \a mode. |
93 | | |
94 | | \sa QUrl::setHost(), QUrl::ParsingMode, QHstsPolicy::PolicyFlag |
95 | | */ |
96 | | QHstsPolicy::QHstsPolicy(const QDateTime &expiry, PolicyFlags flags, |
97 | | const QString &host, QUrl::ParsingMode mode) |
98 | 0 | : d(new QHstsPolicyPrivate) |
99 | 0 | { |
100 | 0 | d->url.setHost(host, mode); |
101 | 0 | d->expiry = expiry; |
102 | 0 | d->includeSubDomains = flags.testFlag(IncludeSubDomains); |
103 | 0 | } |
104 | | |
105 | | /*! |
106 | | Creates a copy of \a other object. |
107 | | */ |
108 | | QHstsPolicy::QHstsPolicy(const QHstsPolicy &other) |
109 | 0 | : d(new QHstsPolicyPrivate(*other.d)) |
110 | 0 | { |
111 | 0 | } |
112 | | |
113 | | /*! |
114 | | Destructor. |
115 | | */ |
116 | | QHstsPolicy::~QHstsPolicy() |
117 | 0 | { |
118 | 0 | } |
119 | | |
120 | | /*! |
121 | | Copy-assignment operator, makes a copy of \a other. |
122 | | */ |
123 | | QHstsPolicy &QHstsPolicy::operator=(const QHstsPolicy &other) |
124 | 0 | { |
125 | 0 | d = other.d; |
126 | 0 | return *this; |
127 | 0 | } |
128 | | |
129 | | /*! |
130 | | Sets a host, \a host data is interpreted according to \a mode parameter. |
131 | | |
132 | | \sa host(), QUrl::setHost(), QUrl::ParsingMode |
133 | | */ |
134 | | void QHstsPolicy::setHost(const QString &host, QUrl::ParsingMode mode) |
135 | 0 | { |
136 | 0 | d->url.setHost(host, mode); |
137 | 0 | } |
138 | | |
139 | | /*! |
140 | | Returns a host for a given policy, formatted according to \a options. |
141 | | |
142 | | \sa setHost(), QUrl::host(), QUrl::ComponentFormattingOptions |
143 | | */ |
144 | | QString QHstsPolicy::host(QUrl::ComponentFormattingOptions options) const |
145 | 0 | { |
146 | 0 | return d->url.host(options); |
147 | 0 | } |
148 | | |
149 | | /*! |
150 | | Sets the expiration date for the policy (in UTC) to \a expiry. |
151 | | |
152 | | \sa expiry() |
153 | | */ |
154 | | void QHstsPolicy::setExpiry(const QDateTime &expiry) |
155 | 0 | { |
156 | 0 | d->expiry = expiry; |
157 | 0 | } |
158 | | |
159 | | /*! |
160 | | Returns the expiration date for the policy (in UTC). |
161 | | |
162 | | \sa setExpiry() |
163 | | */ |
164 | | QDateTime QHstsPolicy::expiry() const |
165 | 0 | { |
166 | 0 | return d->expiry; |
167 | 0 | } |
168 | | |
169 | | /*! |
170 | | Sets whether subdomains are included for this policy to \a include. |
171 | | |
172 | | \sa includesSubDomains() |
173 | | */ |
174 | | void QHstsPolicy::setIncludesSubDomains(bool include) |
175 | 0 | { |
176 | 0 | d->includeSubDomains = include; |
177 | 0 | } |
178 | | |
179 | | /*! |
180 | | Returns \c true if this policy also includes subdomains. |
181 | | |
182 | | \sa setIncludesSubDomains() |
183 | | */ |
184 | | bool QHstsPolicy::includesSubDomains() const |
185 | 0 | { |
186 | 0 | return d->includeSubDomains; |
187 | 0 | } |
188 | | |
189 | | /*! |
190 | | Return \c true if this policy has a valid expiration date and this date |
191 | | is greater than QDateTime::currentGetDateTimeUtc(). |
192 | | |
193 | | \sa setExpiry(), expiry() |
194 | | */ |
195 | | bool QHstsPolicy::isExpired() const |
196 | 0 | { |
197 | 0 | return !d->expiry.isValid() || d->expiry <= QDateTime::currentDateTimeUtc(); |
198 | 0 | } |
199 | | |
200 | | /*! |
201 | | \fn void QHstsPolicy::swap(QHstsPolicy &other) |
202 | | \memberswap{policy} |
203 | | */ |
204 | | |
205 | | QT_END_NAMESPACE |