~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/LegacySchemeRegistry.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Apple Inc. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE, INC. ``AS IS'' AND ANY
 
14
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
18
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
19
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
20
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
21
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
22
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
23
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
24
 *
 
25
 */
 
26
#include "config.h"
 
27
#include "LegacySchemeRegistry.h"
 
28
 
 
29
#include "RuntimeApplicationChecks.h"
 
30
#include <wtf/Lock.h>
 
31
#include <wtf/Locker.h>
 
32
#include <wtf/MainThread.h>
 
33
#include <wtf/NeverDestroyed.h>
 
34
#include <wtf/URLParser.h>
 
35
 
 
36
#if ENABLE(CONTENT_FILTERING)
 
37
#include "ContentFilter.h"
 
38
#endif
 
39
#if USE(QUICK_LOOK)
 
40
#include "QuickLook.h"
 
41
#endif
 
42
 
 
43
namespace WebCore {
 
44
 
 
45
// FIXME: URLSchemesMap is a peculiar type name given that it is a set.
 
46
 
 
47
static const URLSchemesMap& builtinLocalURLSchemes();
 
48
static const Vector<String>& builtinSecureSchemes();
 
49
static const Vector<String>& builtinSchemesWithUniqueOrigins();
 
50
static const Vector<String>& builtinEmptyDocumentSchemes();
 
51
static const Vector<String>& builtinCanDisplayOnlyIfCanRequestSchemes();
 
52
static const Vector<String>& builtinCORSEnabledSchemes();
 
53
 
 
54
using StringVectorFunction = const Vector<String>& (*)();
 
55
 
 
56
static void add(URLSchemesMap& set, StringVectorFunction function)
 
57
{
 
58
    for (auto& scheme : function())
 
59
        set.add(scheme);
 
60
}
 
61
 
 
62
static NeverDestroyed<URLSchemesMap> makeNeverDestroyedSchemeSet(const Vector<String>& (*function)())
 
63
{
 
64
    URLSchemesMap set;
 
65
    add(set, function);
 
66
    return set;
 
67
}
 
68
 
 
69
static Lock schemeRegistryLock;
 
70
 
 
71
static const URLSchemesMap& allBuiltinSchemes()
 
72
{
 
73
    static const auto schemes = makeNeverDestroyed([] {
 
74
        static const StringVectorFunction functions[] {
 
75
            builtinSecureSchemes,
 
76
            builtinSchemesWithUniqueOrigins,
 
77
            builtinEmptyDocumentSchemes,
 
78
            builtinCanDisplayOnlyIfCanRequestSchemes,
 
79
            builtinCORSEnabledSchemes,
 
80
        };
 
81
 
 
82
        // Other misc schemes that the LegacySchemeRegistry doesn't know about.
 
83
        static const char* const otherSchemes[] = {
 
84
            "webkit-fake-url",
 
85
#if PLATFORM(MAC)
 
86
            "safari-extension",
 
87
#endif
 
88
#if USE(QUICK_LOOK)
 
89
            QLPreviewProtocol,
 
90
#endif
 
91
#if ENABLE(CONTENT_FILTERING)
 
92
            ContentFilter::urlScheme(),
 
93
#endif
 
94
        };
 
95
 
 
96
        URLSchemesMap set;
 
97
        {
 
98
            Locker<Lock> locker(schemeRegistryLock);
 
99
            for (auto& scheme : builtinLocalURLSchemes())
 
100
                set.add(scheme);
 
101
 
 
102
            for (auto& function : functions)
 
103
                add(set, function);
 
104
        }
 
105
        for (auto& scheme : otherSchemes)
 
106
            set.add(scheme);
 
107
        return set;
 
108
    }());
 
109
    return schemes;
 
110
}
 
111
 
 
112
static const URLSchemesMap& builtinLocalURLSchemes()
 
113
{
 
114
    ASSERT(schemeRegistryLock.isHeld());
 
115
    static const auto schemes = makeNeverDestroyed(URLSchemesMap {
 
116
        "file",
 
117
#if PLATFORM(COCOA)
 
118
        "applewebdata",
 
119
#endif
 
120
    });
 
121
    return schemes;
 
122
}
 
123
 
 
124
static URLSchemesMap& localURLSchemes()
 
125
{
 
126
    ASSERT(schemeRegistryLock.isHeld());
 
127
    static NeverDestroyed<URLSchemesMap> localSchemes = builtinLocalURLSchemes();
 
128
    return localSchemes;
 
129
}
 
130
 
 
131
static URLSchemesMap& displayIsolatedURLSchemes()
 
132
{
 
133
    ASSERT(schemeRegistryLock.isHeld());
 
134
    static NeverDestroyed<URLSchemesMap> displayIsolatedSchemes;
 
135
    return displayIsolatedSchemes;
 
136
}
 
137
 
 
138
const Vector<String>& builtinSecureSchemes()
 
139
{
 
140
    ASSERT(schemeRegistryLock.isHeld());
 
141
    static const auto schemes = makeNeverDestroyed(Vector<String> {
 
142
        "https",
 
143
        "about",
 
144
        "data",
 
145
        "wss",
 
146
#if PLATFORM(GTK) || PLATFORM(WPE)
 
147
        "resource",
 
148
#endif
 
149
    });
 
150
    return schemes;
 
151
}
 
152
 
 
153
static URLSchemesMap& secureSchemes()
 
154
{
 
155
    ASSERT(schemeRegistryLock.isHeld());
 
156
    static auto secureSchemes = makeNeverDestroyedSchemeSet(builtinSecureSchemes);
 
157
    return secureSchemes;
 
158
}
 
159
 
 
160
const Vector<String>& builtinSchemesWithUniqueOrigins()
 
161
{
 
162
    ASSERT(schemeRegistryLock.isHeld());
 
163
    static const auto schemes = makeNeverDestroyed(Vector<String> {
 
164
        "about",
 
165
        "javascript",
 
166
        // This is an intentional difference from the behavior the HTML specification calls for.
 
167
        // See https://bugs.webkit.org/show_bug.cgi?id=11885
 
168
        "data",
 
169
    });
 
170
    return schemes;
 
171
}
 
172
 
 
173
static URLSchemesMap& schemesWithUniqueOrigins()
 
174
{
 
175
    ASSERT(schemeRegistryLock.isHeld());
 
176
    static auto schemesWithUniqueOrigins = makeNeverDestroyedSchemeSet(builtinSchemesWithUniqueOrigins);
 
177
    return schemesWithUniqueOrigins;
 
178
}
 
179
 
 
180
const Vector<String>& builtinEmptyDocumentSchemes()
 
181
{
 
182
    ASSERT(isMainThread());
 
183
    static const auto schemes = makeNeverDestroyed(Vector<String> { "about" });
 
184
    return schemes;
 
185
}
 
186
 
 
187
static URLSchemesMap& emptyDocumentSchemes()
 
188
{
 
189
    ASSERT(isMainThread());
 
190
    static auto emptyDocumentSchemes = makeNeverDestroyedSchemeSet(builtinEmptyDocumentSchemes);
 
191
    return emptyDocumentSchemes;
 
192
}
 
193
 
 
194
static URLSchemesMap& schemesForbiddenFromDomainRelaxation()
 
195
{
 
196
    ASSERT(isMainThread());
 
197
    static NeverDestroyed<URLSchemesMap> schemes;
 
198
    return schemes;
 
199
}
 
200
 
 
201
const Vector<String>& builtinCanDisplayOnlyIfCanRequestSchemes()
 
202
{
 
203
    ASSERT(schemeRegistryLock.isHeld());
 
204
    static const auto schemes = makeNeverDestroyed(Vector<String> { "blob" });
 
205
    return schemes;
 
206
}
 
207
 
 
208
static URLSchemesMap& canDisplayOnlyIfCanRequestSchemes()
 
209
{
 
210
    ASSERT(!isInNetworkProcess());
 
211
    ASSERT(schemeRegistryLock.isHeld());
 
212
    static auto canDisplayOnlyIfCanRequestSchemes = makeNeverDestroyedSchemeSet(builtinCanDisplayOnlyIfCanRequestSchemes);
 
213
    return canDisplayOnlyIfCanRequestSchemes;
 
214
}
 
215
 
 
216
static URLSchemesMap& notAllowingJavascriptURLsSchemes()
 
217
{
 
218
    ASSERT(isMainThread());
 
219
    static NeverDestroyed<URLSchemesMap> notAllowingJavascriptURLsSchemes;
 
220
    return notAllowingJavascriptURLsSchemes;
 
221
}
 
222
 
 
223
void LegacySchemeRegistry::registerURLSchemeAsLocal(const String& scheme)
 
224
{
 
225
    if (scheme.isNull())
 
226
        return;
 
227
 
 
228
    Locker<Lock> locker(schemeRegistryLock);
 
229
    localURLSchemes().add(scheme);
 
230
}
 
231
 
 
232
void LegacySchemeRegistry::removeURLSchemeRegisteredAsLocal(const String& scheme)
 
233
{
 
234
    Locker<Lock> locker(schemeRegistryLock);
 
235
    if (builtinLocalURLSchemes().contains(scheme))
 
236
        return;
 
237
 
 
238
    localURLSchemes().remove(scheme);
 
239
}
 
240
 
 
241
static URLSchemesMap& schemesAllowingDatabaseAccessInPrivateBrowsing()
 
242
{
 
243
    ASSERT(isMainThread());
 
244
    static NeverDestroyed<URLSchemesMap> schemesAllowingDatabaseAccessInPrivateBrowsing;
 
245
    return schemesAllowingDatabaseAccessInPrivateBrowsing;
 
246
}
 
247
 
 
248
const Vector<String>& builtinCORSEnabledSchemes()
 
249
{
 
250
    ASSERT(isMainThread());
 
251
    static const auto schemes = makeNeverDestroyed(Vector<String> { "http", "https" });
 
252
    return schemes;
 
253
}
 
254
 
 
255
static URLSchemesMap& CORSEnabledSchemes()
 
256
{
 
257
    ASSERT(isMainThread());
 
258
    // FIXME: http://bugs.webkit.org/show_bug.cgi?id=77160
 
259
    static auto schemes = makeNeverDestroyedSchemeSet(builtinCORSEnabledSchemes);
 
260
    return schemes;
 
261
}
 
262
 
 
263
static URLSchemesMap& ContentSecurityPolicyBypassingSchemes()
 
264
{
 
265
    ASSERT(schemeRegistryLock.isHeld());
 
266
    static NeverDestroyed<URLSchemesMap> schemes;
 
267
    return schemes;
 
268
}
 
269
 
 
270
static URLSchemesMap& cachePartitioningSchemes()
 
271
{
 
272
    ASSERT(schemeRegistryLock.isHeld());
 
273
    static NeverDestroyed<URLSchemesMap> schemes;
 
274
    return schemes;
 
275
}
 
276
 
 
277
static URLSchemesMap& serviceWorkerSchemes()
 
278
{
 
279
    ASSERT(schemeRegistryLock.isHeld());
 
280
    static NeverDestroyed<URLSchemesMap> schemes;
 
281
    return schemes;
 
282
}
 
283
 
 
284
static URLSchemesMap& alwaysRevalidatedSchemes()
 
285
{
 
286
    ASSERT(isMainThread());
 
287
    static NeverDestroyed<URLSchemesMap> schemes;
 
288
    return schemes;
 
289
}
 
290
 
 
291
bool LegacySchemeRegistry::shouldTreatURLSchemeAsLocal(const String& scheme)
 
292
{
 
293
    if (scheme.isNull())
 
294
        return false;
 
295
 
 
296
    Locker<Lock> locker(schemeRegistryLock);
 
297
    return localURLSchemes().contains(scheme);
 
298
}
 
299
 
 
300
void LegacySchemeRegistry::registerURLSchemeAsNoAccess(const String& scheme)
 
301
{
 
302
    if (scheme.isNull())
 
303
        return;
 
304
 
 
305
    Locker<Lock> locker(schemeRegistryLock);
 
306
    schemesWithUniqueOrigins().add(scheme);
 
307
}
 
308
 
 
309
bool LegacySchemeRegistry::shouldTreatURLSchemeAsNoAccess(const String& scheme)
 
310
{
 
311
    if (scheme.isNull())
 
312
        return false;
 
313
 
 
314
    Locker<Lock> locker(schemeRegistryLock);
 
315
    return schemesWithUniqueOrigins().contains(scheme);
 
316
}
 
317
 
 
318
void LegacySchemeRegistry::registerURLSchemeAsDisplayIsolated(const String& scheme)
 
319
{
 
320
    if (scheme.isNull())
 
321
        return;
 
322
 
 
323
    Locker<Lock> locker(schemeRegistryLock);
 
324
    displayIsolatedURLSchemes().add(scheme);
 
325
}
 
326
 
 
327
bool LegacySchemeRegistry::shouldTreatURLSchemeAsDisplayIsolated(const String& scheme)
 
328
{
 
329
    if (scheme.isNull())
 
330
        return false;
 
331
 
 
332
    Locker<Lock> locker(schemeRegistryLock);
 
333
    return displayIsolatedURLSchemes().contains(scheme);
 
334
}
 
335
 
 
336
void LegacySchemeRegistry::registerURLSchemeAsSecure(const String& scheme)
 
337
{
 
338
    if (scheme.isNull())
 
339
        return;
 
340
 
 
341
    Locker<Lock> locker(schemeRegistryLock);
 
342
    secureSchemes().add(scheme);
 
343
}
 
344
 
 
345
bool LegacySchemeRegistry::shouldTreatURLSchemeAsSecure(const String& scheme)
 
346
{
 
347
    if (scheme.isNull())
 
348
        return false;
 
349
 
 
350
    Locker<Lock> locker(schemeRegistryLock);
 
351
    return secureSchemes().contains(scheme);
 
352
}
 
353
 
 
354
void LegacySchemeRegistry::registerURLSchemeAsEmptyDocument(const String& scheme)
 
355
{
 
356
    if (scheme.isNull())
 
357
        return;
 
358
    emptyDocumentSchemes().add(scheme);
 
359
}
 
360
 
 
361
bool LegacySchemeRegistry::shouldLoadURLSchemeAsEmptyDocument(const String& scheme)
 
362
{
 
363
    return !scheme.isNull() && emptyDocumentSchemes().contains(scheme);
 
364
}
 
365
 
 
366
void LegacySchemeRegistry::setDomainRelaxationForbiddenForURLScheme(bool forbidden, const String& scheme)
 
367
{
 
368
    if (scheme.isNull())
 
369
        return;
 
370
 
 
371
    if (forbidden)
 
372
        schemesForbiddenFromDomainRelaxation().add(scheme);
 
373
    else
 
374
        schemesForbiddenFromDomainRelaxation().remove(scheme);
 
375
}
 
376
 
 
377
bool LegacySchemeRegistry::isDomainRelaxationForbiddenForURLScheme(const String& scheme)
 
378
{
 
379
    return !scheme.isNull() && schemesForbiddenFromDomainRelaxation().contains(scheme);
 
380
}
 
381
 
 
382
bool LegacySchemeRegistry::canDisplayOnlyIfCanRequest(const String& scheme)
 
383
{
 
384
    ASSERT(!isInNetworkProcess());
 
385
    if (scheme.isNull())
 
386
        return false;
 
387
 
 
388
    Locker<Lock> locker(schemeRegistryLock);
 
389
    return canDisplayOnlyIfCanRequestSchemes().contains(scheme);
 
390
}
 
391
 
 
392
void LegacySchemeRegistry::registerAsCanDisplayOnlyIfCanRequest(const String& scheme)
 
393
{
 
394
    ASSERT(!isInNetworkProcess());
 
395
    if (scheme.isNull())
 
396
        return;
 
397
 
 
398
    Locker<Lock> locker(schemeRegistryLock);
 
399
    canDisplayOnlyIfCanRequestSchemes().add(scheme);
 
400
}
 
401
 
 
402
void LegacySchemeRegistry::registerURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
 
403
{
 
404
    if (scheme.isNull())
 
405
        return;
 
406
    notAllowingJavascriptURLsSchemes().add(scheme);
 
407
}
 
408
 
 
409
bool LegacySchemeRegistry::shouldTreatURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
 
410
{
 
411
    return !scheme.isNull() && notAllowingJavascriptURLsSchemes().contains(scheme);
 
412
}
 
413
 
 
414
void LegacySchemeRegistry::registerURLSchemeAsAllowingDatabaseAccessInPrivateBrowsing(const String& scheme)
 
415
{
 
416
    if (scheme.isNull())
 
417
        return;
 
418
    schemesAllowingDatabaseAccessInPrivateBrowsing().add(scheme);
 
419
}
 
420
 
 
421
bool LegacySchemeRegistry::allowsDatabaseAccessInPrivateBrowsing(const String& scheme)
 
422
{
 
423
    return !scheme.isNull() && schemesAllowingDatabaseAccessInPrivateBrowsing().contains(scheme);
 
424
}
 
425
 
 
426
void LegacySchemeRegistry::registerURLSchemeAsCORSEnabled(const String& scheme)
 
427
{
 
428
    ASSERT(!isInNetworkProcess());
 
429
    if (scheme.isNull())
 
430
        return;
 
431
    CORSEnabledSchemes().add(scheme);
 
432
}
 
433
 
 
434
bool LegacySchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(const String& scheme)
 
435
{
 
436
    ASSERT(!isInNetworkProcess());
 
437
    return !scheme.isNull() && CORSEnabledSchemes().contains(scheme);
 
438
}
 
439
 
 
440
Vector<String> LegacySchemeRegistry::allURLSchemesRegisteredAsCORSEnabled()
 
441
{
 
442
    ASSERT(!isInNetworkProcess());
 
443
    return copyToVector(CORSEnabledSchemes());
 
444
}
 
445
 
 
446
void LegacySchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme)
 
447
{
 
448
    if (scheme.isNull())
 
449
        return;
 
450
 
 
451
    Locker<Lock> locker(schemeRegistryLock);
 
452
    ContentSecurityPolicyBypassingSchemes().add(scheme);
 
453
}
 
454
 
 
455
void LegacySchemeRegistry::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(const String& scheme)
 
456
{
 
457
    if (scheme.isNull())
 
458
        return;
 
459
 
 
460
    Locker<Lock> locker(schemeRegistryLock);
 
461
    ContentSecurityPolicyBypassingSchemes().remove(scheme);
 
462
}
 
463
 
 
464
bool LegacySchemeRegistry::schemeShouldBypassContentSecurityPolicy(const String& scheme)
 
465
{
 
466
    if (scheme.isNull())
 
467
        return false;
 
468
 
 
469
    Locker<Lock> locker(schemeRegistryLock);
 
470
    return ContentSecurityPolicyBypassingSchemes().contains(scheme);
 
471
}
 
472
 
 
473
void LegacySchemeRegistry::registerURLSchemeAsAlwaysRevalidated(const String& scheme)
 
474
{
 
475
    if (scheme.isNull())
 
476
        return;
 
477
    alwaysRevalidatedSchemes().add(scheme);
 
478
}
 
479
 
 
480
bool LegacySchemeRegistry::shouldAlwaysRevalidateURLScheme(const String& scheme)
 
481
{
 
482
    return !scheme.isNull() && alwaysRevalidatedSchemes().contains(scheme);
 
483
}
 
484
 
 
485
void LegacySchemeRegistry::registerURLSchemeAsCachePartitioned(const String& scheme)
 
486
{
 
487
    if (scheme.isNull())
 
488
        return;
 
489
 
 
490
    Locker<Lock> locker(schemeRegistryLock);
 
491
    cachePartitioningSchemes().add(scheme);
 
492
}
 
493
 
 
494
bool LegacySchemeRegistry::shouldPartitionCacheForURLScheme(const String& scheme)
 
495
{
 
496
    if (scheme.isNull())
 
497
        return false;
 
498
 
 
499
    Locker<Lock> locker(schemeRegistryLock);
 
500
    return cachePartitioningSchemes().contains(scheme);
 
501
}
 
502
 
 
503
void LegacySchemeRegistry::registerURLSchemeServiceWorkersCanHandle(const String& scheme)
 
504
{
 
505
    if (scheme.isNull())
 
506
        return;
 
507
 
 
508
    Locker<Lock> locker(schemeRegistryLock);
 
509
    serviceWorkerSchemes().add(scheme);
 
510
}
 
511
 
 
512
bool LegacySchemeRegistry::canServiceWorkersHandleURLScheme(const String& scheme)
 
513
{
 
514
    if (scheme.isNull())
 
515
        return false;
 
516
 
 
517
    if (scheme.startsWithIgnoringASCIICase("http"_s)) {
 
518
        if (scheme.length() == 4)
 
519
            return true;
 
520
        if (scheme.length() == 5 && isASCIIAlphaCaselessEqual(scheme[4], 's'))
 
521
            return true;
 
522
    }
 
523
 
 
524
    Locker<Lock> locker(schemeRegistryLock);
 
525
    return serviceWorkerSchemes().contains(scheme);
 
526
}
 
527
 
 
528
bool LegacySchemeRegistry::isServiceWorkerContainerCustomScheme(const String& scheme)
 
529
{
 
530
    Locker<Lock> locker(schemeRegistryLock);
 
531
    return !scheme.isNull() && serviceWorkerSchemes().contains(scheme);
 
532
}
 
533
 
 
534
bool LegacySchemeRegistry::isUserExtensionScheme(const String& scheme)
 
535
{
 
536
    // FIXME: Remove this once Safari has adopted WKWebViewConfiguration._corsDisablingPatterns
 
537
#if PLATFORM(MAC)
 
538
    if (scheme == "safari-extension")
 
539
        return true;
 
540
#else
 
541
    UNUSED_PARAM(scheme);
 
542
#endif
 
543
    return false;
 
544
}
 
545
 
 
546
bool LegacySchemeRegistry::isBuiltinScheme(const String& scheme)
 
547
{
 
548
    return !scheme.isNull() && (allBuiltinSchemes().contains(scheme) || WTF::URLParser::isSpecialScheme(scheme));
 
549
}
 
550
 
 
551
} // namespace WebCore