|
| 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 <LibWeb/HTML/Origin.h> |
| 10 | +#include <LibWeb/SecureContexts/AbstractOperations.h> |
| 11 | +#include <LibWeb/URL/URL.h> |
| 12 | + |
| 13 | +namespace Web::SecureContexts { |
| 14 | + |
| 15 | +// https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy |
| 16 | +Trustworthiness is_origin_potentially_trustworthy(HTML::Origin const& origin) |
| 17 | +{ |
| 18 | + // 1. If origin is an opaque origin, return "Not Trustworthy". |
| 19 | + if (origin.is_opaque()) |
| 20 | + return Trustworthiness::NotTrustworthy; |
| 21 | + |
| 22 | + // 2. Assert: origin is a tuple origin. |
| 23 | + |
| 24 | + // 3. If origin’s scheme is either "https" or "wss", return "Potentially Trustworthy". |
| 25 | + // Note: This is meant to be analog to the a priori authenticated URL concept in [MIX]. |
| 26 | + if (origin.scheme().is_one_of("https"sv, "wss"sv)) |
| 27 | + return Trustworthiness::PotentiallyTrustworthy; |
| 28 | + |
| 29 | + // 4. If origin’s host matches one of the CIDR notations 127.0.0.0/8 or ::1/128 [RFC4632], return "Potentially Trustworthy". |
| 30 | + if (auto ipv4_address = IPv4Address::from_string(origin.host()); ipv4_address.has_value() && (ipv4_address->to_u32() & 0xff000000) != 0) |
| 31 | + return Trustworthiness::PotentiallyTrustworthy; |
| 32 | + if (auto ipv6_address = IPv6Address::from_string(origin.host()); ipv6_address.has_value() && ipv6_address->to_string() == "::1") |
| 33 | + return Trustworthiness::PotentiallyTrustworthy; |
| 34 | + |
| 35 | + // 5. If the user agent conforms to the name resolution rules in [let-localhost-be-localhost] and one of the following is true: |
| 36 | + // - origin’s host is "localhost" or "localhost." |
| 37 | + // - origin’s host ends with ".localhost" or ".localhost." |
| 38 | + // then return "Potentially Trustworthy". |
| 39 | + // Note: See § 5.2 localhost for details on the requirements here. |
| 40 | + if (origin.host().is_one_of("localhost"sv, "localhost.") |
| 41 | + || origin.host().ends_with(".localhost"sv) |
| 42 | + || origin.host().ends_with(".localhost."sv)) { |
| 43 | + return Trustworthiness::PotentiallyTrustworthy; |
| 44 | + } |
| 45 | + |
| 46 | + // 6. If origin’s scheme is "file", return "Potentially Trustworthy". |
| 47 | + if (origin.scheme() == "file"sv) |
| 48 | + return Trustworthiness::PotentiallyTrustworthy; |
| 49 | + |
| 50 | + // 7. If origin’s scheme component is one which the user agent considers to be authenticated, return "Potentially Trustworthy". |
| 51 | + // Note: See § 7.1 Packaged Applications for detail here. |
| 52 | + |
| 53 | + // 8. If origin has been configured as a trustworthy origin, return "Potentially Trustworthy". |
| 54 | + // Note: See § 7.2 Development Environments for detail here. |
| 55 | + |
| 56 | + // 9. Return "Not Trustworthy". |
| 57 | + return Trustworthiness::NotTrustworthy; |
| 58 | +} |
| 59 | + |
| 60 | +} |
0 commit comments