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
|
/*
* qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
* Copyright (C) 2026 Danilo M. <danix@danix.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <QtTest>
#include "requestinterceptor.h"
class TestInterceptor : public QObject
{
Q_OBJECT
private slots:
void blocksRemoteHttpByDefault();
void blocksRemoteHttpsByDefault();
void blocksFileUrlsAlways();
void allowsCidForCurrentMessage();
void blocksCidForForeignMessage();
void allowRemoteFlagPermitsHttpButNotFile();
void recordsThatSomethingWasBlocked();
void resetClearsBlockedFlag();
// Adversarial additions.
void schemeIsCaseInsensitiveAndStillBlocked();
void qtmaildirSchemeIsCaseInsensitiveAllow();
void qtmaildirDocumentUrlIsAllowed();
void qtmaildirOtherPathIsBlocked();
void qtmaildirBlockedWhenNoDocumentUrlSet();
void resetForNewMessageDoesNotClearDocumentUrl();
void cidUrlDoesNotParseAsUserinfo();
void cidPercentEncodingDoesNotBypassAllowlist();
void javascriptSchemeBlocked();
void dataSchemeBlocked();
void blobSchemeBlocked();
void aboutSchemeBlocked();
void chromeSchemeBlocked();
void qrcSchemeBlocked();
void filesystemSchemeBlocked();
void protocolRelativeUrlBlocked();
void emptyUrlBlocked();
void blankUrlBlocked();
void colonOnlyUrlBlocked();
void fragmentOnlyUrlBlocked();
};
void TestInterceptor::blocksRemoteHttpByDefault()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("http://tracker.example/pixel.gif"))));
}
void TestInterceptor::blocksRemoteHttpsByDefault()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("https://cdn.example/style.css"))));
}
void TestInterceptor::blocksFileUrlsAlways()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
// Even with remote content explicitly allowed, local files stay blocked:
// a message must never read the filesystem.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("file:///etc/passwd"))));
}
void TestInterceptor::allowsCidForCurrentMessage()
{
RequestInterceptor interceptor;
interceptor.setAllowedCids({ QStringLiteral("logo@example.org") });
QVERIFY(interceptor.shouldAllow(QUrl(QStringLiteral("cid:logo@example.org"))));
}
void TestInterceptor::blocksCidForForeignMessage()
{
RequestInterceptor interceptor;
interceptor.setAllowedCids({ QStringLiteral("logo@example.org") });
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("cid:other@example.org"))));
}
void TestInterceptor::allowRemoteFlagPermitsHttpButNotFile()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
QVERIFY(interceptor.shouldAllow(QUrl(QStringLiteral("https://cdn.example/img.png"))));
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("file:///etc/passwd"))));
}
void TestInterceptor::recordsThatSomethingWasBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.blockedAnything());
interceptor.shouldAllow(QUrl(QStringLiteral("http://tracker.example/p.gif")));
// Drives the "Remote content blocked" banner in the message header.
QVERIFY(interceptor.blockedAnything());
}
void TestInterceptor::resetClearsBlockedFlag()
{
RequestInterceptor interceptor;
interceptor.shouldAllow(QUrl(QStringLiteral("http://tracker.example/p.gif")));
QVERIFY(interceptor.blockedAnything());
interceptor.resetForNewMessage();
QVERIFY(!interceptor.blockedAnything());
// Remote permission never carries over to the next message.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("https://cdn.example/x.png"))));
}
void TestInterceptor::schemeIsCaseInsensitiveAndStillBlocked()
{
// QUrl::scheme() normalizes to lowercase, so "HTTP://..." must still hit
// the http branch (and be blocked without allowRemote), not fall through
// unexpectedly to an allow path.
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("HTTP://tracker.example/pixel.gif"))));
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("HTTPS://tracker.example/pixel.gif"))));
interceptor.setAllowRemote(true);
QVERIFY(interceptor.shouldAllow(QUrl(QStringLiteral("HTTP://tracker.example/pixel.gif"))));
}
void TestInterceptor::qtmaildirSchemeIsCaseInsensitiveAllow()
{
// QUrl normalizes scheme (and host, for authority-form URLs) to lowercase
// on parse, so a differently-cased spelling of the exact document URL
// still compares equal via QUrl::operator== (verified empirically:
// QUrl("qtmaildir://message") == QUrl("QTMAILDIR://message") is true).
RequestInterceptor interceptor;
interceptor.setDocumentUrl(QUrl(QStringLiteral("qtmaildir://body/index.html")));
QVERIFY(interceptor.shouldAllow(QUrl(QStringLiteral("QTMAILDIR://body/index.html"))));
}
void TestInterceptor::qtmaildirDocumentUrlIsAllowed()
{
RequestInterceptor interceptor;
const QUrl doc(QStringLiteral("qtmaildir://message"));
interceptor.setDocumentUrl(doc);
QVERIFY(interceptor.shouldAllow(doc));
}
void TestInterceptor::qtmaildirOtherPathIsBlocked()
{
// Defense in depth: the qtmaildir: scheme is trusted only for the exact
// document URL the application itself set, never for the whole scheme.
// A hostile message body can put any qtmaildir: URL in <img src> or
// <link href>; none of these variants may pass.
RequestInterceptor interceptor;
interceptor.setDocumentUrl(QUrl(QStringLiteral("qtmaildir://message")));
// Path traversal off the document URL.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("qtmaildir://message/../etc"))));
// A different qtmaildir origin entirely.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("qtmaildir://other"))));
// Opaque (non-authority) form of the scheme.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("qtmaildir:whatever"))));
// A sub-path of the document URL is still not the document URL itself.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("qtmaildir://message/cid/foo"))));
QVERIFY(interceptor.blockedAnything());
}
void TestInterceptor::qtmaildirBlockedWhenNoDocumentUrlSet()
{
// Fail closed: if MessageView forgets to call setDocumentUrl(), nothing
// on the qtmaildir: scheme should be reachable, not even the URL that
// would otherwise be the legitimate document.
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("qtmaildir://message"))));
}
void TestInterceptor::resetForNewMessageDoesNotClearDocumentUrl()
{
// The document/base URL is a property of the view's current navigation,
// not of an individual message's content, so switching to a new message
// (resetForNewMessage) must not force MessageView to re-supply it.
RequestInterceptor interceptor;
const QUrl doc(QStringLiteral("qtmaildir://message"));
interceptor.setDocumentUrl(doc);
interceptor.shouldAllow(QUrl(QStringLiteral("http://tracker.example/p.gif")));
QVERIFY(interceptor.blockedAnything());
interceptor.resetForNewMessage();
QVERIFY(!interceptor.blockedAnything());
QCOMPARE(interceptor.documentUrl(), doc);
QVERIFY(interceptor.shouldAllow(doc));
}
void TestInterceptor::cidUrlDoesNotParseAsUserinfo()
{
// Pin down QUrl's actual parsing of a cid: URL containing '@', so a
// future Qt version change would be caught here rather than silently
// breaking the allowlist comparison in shouldAllow().
QUrl url(QStringLiteral("cid:logo@example.org"));
QCOMPARE(url.path(), QStringLiteral("logo@example.org"));
QCOMPARE(url.host(), QString());
QCOMPARE(url.userName(), QString());
RequestInterceptor interceptor;
interceptor.setAllowedCids({ QStringLiteral("logo@example.org") });
QVERIFY(interceptor.shouldAllow(url));
}
void TestInterceptor::cidPercentEncodingDoesNotBypassAllowlist()
{
// QUrl::path() returns the percent-DECODED form (verified empirically:
// QUrl("cid:%6Cogo@example.org").path() == "logo@example.org", and this
// holds for full-string encodings too). A percent-encoded spelling of an
// allowed id therefore compares equal to that same allowed id, which is
// correct URI equivalence, not a bypass: decoding cannot turn a foreign
// id into a *different* allowed id's literal string, only into its own
// canonical form. What matters for the security boundary is that a
// genuinely foreign id, encoded or not, is still rejected.
RequestInterceptor interceptor;
interceptor.setAllowedCids({ QStringLiteral("logo@example.org") });
QVERIFY(interceptor.shouldAllow(QUrl(QStringLiteral("cid:%6Cogo@example.org"))));
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("cid:other@example.org"))));
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("cid:%6Fther@example.org"))));
}
void TestInterceptor::javascriptSchemeBlocked()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("javascript:alert(1)"))));
}
void TestInterceptor::dataSchemeBlocked()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("data:text/html,<script>alert(1)</script>"))));
}
void TestInterceptor::blobSchemeBlocked()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("blob:https://example.org/uuid"))));
}
void TestInterceptor::aboutSchemeBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("about:blank"))));
}
void TestInterceptor::chromeSchemeBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("chrome://settings"))));
}
void TestInterceptor::qrcSchemeBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("qrc:/icons/foo.png"))));
}
void TestInterceptor::filesystemSchemeBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("filesystem:https://example.org/temporary/foo"))));
}
void TestInterceptor::protocolRelativeUrlBlocked()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("//tracker.example/pixel.gif"))));
}
void TestInterceptor::emptyUrlBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl()));
}
void TestInterceptor::blankUrlBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral(""))));
}
void TestInterceptor::colonOnlyUrlBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral(":"))));
}
void TestInterceptor::fragmentOnlyUrlBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("#fragment"))));
}
QTEST_MAIN(TestInterceptor)
#include "test_interceptor.moc"
|