Coverage Report

Created: 2026-10-28 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/SecureContexts/AbstractOperations.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/IPv4Address.h>
8
#include <AK/IPv6Address.h>
9
#include <LibURL/Origin.h>
10
#include <LibURL/URL.h>
11
#include <LibWeb/DOMURL/DOMURL.h>
12
#include <LibWeb/SecureContexts/AbstractOperations.h>
13
14
namespace Web::SecureContexts {
15
16
// https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy
17
Trustworthiness is_origin_potentially_trustworthy(URL::Origin const& origin)
18
0
{
19
    // 1. If origin is an opaque origin, return "Not Trustworthy".
20
0
    if (origin.is_opaque())
21
0
        return Trustworthiness::NotTrustworthy;
22
23
    // 2. Assert: origin is a tuple origin.
24
25
    // 3. If origin’s scheme is either "https" or "wss", return "Potentially Trustworthy".
26
    // Note: This is meant to be analog to the a priori authenticated URL concept in [MIX].
27
0
    if (origin.scheme().is_one_of("https"sv, "wss"sv))
28
0
        return Trustworthiness::PotentiallyTrustworthy;
29
30
    // 4. If origin’s host matches one of the CIDR notations 127.0.0.0/8 or ::1/128 [RFC4632], return "Potentially Trustworthy".
31
    // FIXME: This would be nicer if URL::IPv4Address and URL::IPv6Address were instances of AK::IPv4Address and AK::IPv6Address
32
0
    if (origin.host().has<URL::IPv4Address>()) {
33
0
        if ((origin.host().get<URL::IPv4Address>() & 0xff000000) != 0)
34
0
            return Trustworthiness::PotentiallyTrustworthy;
35
0
    } else if (origin.host().has<URL::IPv6Address>()) {
36
0
        auto ipv6_address = origin.host().get<URL::IPv6Address>();
37
0
        static constexpr URL::IPv6Address loopback { 0, 0, 0, 0, 0, 0, 0, 1 };
38
0
        if (ipv6_address == loopback)
39
0
            return Trustworthiness::PotentiallyTrustworthy;
40
0
    }
41
42
    // 5. If the user agent conforms to the name resolution rules in [let-localhost-be-localhost] and one of the following is true:
43
    // - origin’s host is "localhost" or "localhost."
44
    // - origin’s host ends with ".localhost" or ".localhost."
45
    // then return "Potentially Trustworthy".
46
    // Note: See § 5.2 localhost for details on the requirements here.
47
0
    if (origin.host().has<String>()) {
48
0
        auto const& host = origin.host().get<String>();
49
0
        if (host.is_one_of("localhost"sv, "localhost.")
50
0
            || host.ends_with_bytes(".localhost"sv)
51
0
            || host.ends_with_bytes(".localhost."sv)) {
52
0
            return Trustworthiness::PotentiallyTrustworthy;
53
0
        }
54
0
    }
55
56
    // 6. If origin’s scheme is "file", return "Potentially Trustworthy".
57
    // AD-HOC: Our resource:// is basically an alias to file://
58
0
    if (origin.scheme() == "file"sv || origin.scheme() == "resource"sv)
59
0
        return Trustworthiness::PotentiallyTrustworthy;
60
61
    // 7. If origin’s scheme component is one which the user agent considers to be authenticated, return "Potentially Trustworthy".
62
    // Note: See § 7.1 Packaged Applications for detail here.
63
64
    // 8. If origin has been configured as a trustworthy origin, return "Potentially Trustworthy".
65
    // Note: See § 7.2 Development Environments for detail here.
66
67
    // 9. Return "Not Trustworthy".
68
0
    return Trustworthiness::NotTrustworthy;
69
0
}
70
71
// https://w3c.github.io/webappsec-secure-contexts/#is-url-trustworthy
72
Trustworthiness is_url_potentially_trustworthy(URL::URL const& url)
73
0
{
74
    // 1. If url is "about:blank" or "about:srcdoc", return "Potentially Trustworthy".
75
0
    if (url == "about:blank"sv || url == "about:srcdoc"sv)
76
0
        return Trustworthiness::PotentiallyTrustworthy;
77
78
    // 2. If url’s scheme is "data", return "Potentially Trustworthy".
79
0
    if (url.scheme() == "data"sv)
80
0
        return Trustworthiness::PotentiallyTrustworthy;
81
82
    // 3. Return the result of executing § 3.1 Is origin potentially trustworthy? on url’s origin.
83
0
    return is_origin_potentially_trustworthy(url.origin());
84
0
}
85
86
}