Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Tsuyoshi Horo | cdbb490 | 2018-04-12 06:09:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_WEB_PACKAGE_SIGNED_EXCHANGE_UTILS_H_ |
| 6 | #define CONTENT_BROWSER_WEB_PACKAGE_SIGNED_EXCHANGE_UTILS_H_ |
| 7 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 8 | #include <optional> |
Tsuyoshi Horo | cdbb490 | 2018-04-12 06:09:14 | [diff] [blame] | 9 | #include <string> |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 10 | #include <string_view> |
Tsuyoshi Horo | cdbb490 | 2018-04-12 06:09:14 | [diff] [blame] | 11 | |
Kunihiko Sakamoto | b5c94d90 | 2018-09-04 04:09:02 | [diff] [blame] | 12 | #include "content/browser/web_package/signed_exchange_consts.h" |
Tsuyoshi Horo | b40c7c3 | 2018-05-31 07:32:45 | [diff] [blame] | 13 | #include "content/browser/web_package/signed_exchange_error.h" |
Tsuyoshi Horo | 06eb28f | 2019-02-21 13:52:24 | [diff] [blame] | 14 | #include "content/browser/web_package/signed_exchange_signature_verifier.h" |
Kunihiko Sakamoto | b5c94d90 | 2018-09-04 04:09:02 | [diff] [blame] | 15 | #include "content/common/content_export.h" |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 16 | #include "net/url_request/redirect_util.h" |
Lucas Furukawa Gadani | 62fcfa9 | 2019-12-05 15:38:41 | [diff] [blame] | 17 | #include "services/network/public/mojom/url_response_head.mojom.h" |
Kunihiko Sakamoto | d405c9d | 2018-12-19 02:59:22 | [diff] [blame] | 18 | #include "url/gurl.h" |
Tsuyoshi Horo | 46f5fff | 2018-05-10 12:33:35 | [diff] [blame] | 19 | |
| 20 | namespace network { |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 21 | struct ResourceRequest; |
Tsuyoshi Horo | 46f5fff | 2018-05-10 12:33:35 | [diff] [blame] | 22 | } // namespace network |
| 23 | |
Tsuyoshi Horo | cdbb490 | 2018-04-12 06:09:14 | [diff] [blame] | 24 | namespace content { |
Tsuyoshi Horo | 4801e76 | 2018-04-25 07:36:57 | [diff] [blame] | 25 | |
Clark DuVall | ab63d14 | 2019-07-23 04:24:36 | [diff] [blame] | 26 | class BrowserContext; |
Tsuyoshi Horo | 4801e76 | 2018-04-25 07:36:57 | [diff] [blame] | 27 | class SignedExchangeDevToolsProxy; |
| 28 | |
Tsuyoshi Horo | cdbb490 | 2018-04-12 06:09:14 | [diff] [blame] | 29 | namespace signed_exchange_utils { |
| 30 | |
Kunihiko Sakamoto | d405c9d | 2018-12-19 02:59:22 | [diff] [blame] | 31 | // URLWithRawString holds a parsed URL along with its raw bytes. |
| 32 | struct URLWithRawString { |
| 33 | GURL url; |
| 34 | std::string raw_string; |
| 35 | URLWithRawString() = default; |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 36 | URLWithRawString(std::string_view url_string) |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 37 | : url(url_string), raw_string(url_string) {} |
Kunihiko Sakamoto | d405c9d | 2018-12-19 02:59:22 | [diff] [blame] | 38 | }; |
| 39 | |
Kunihiko Sakamoto | 6405d0af | 2021-11-18 00:46:37 | [diff] [blame] | 40 | // Records SignedExchange.LoadResult2 UMA histogram. |
| 41 | void RecordLoadResultHistogram(SignedExchangeLoadResult result); |
| 42 | |
Tsuyoshi Horo | b40c7c3 | 2018-05-31 07:32:45 | [diff] [blame] | 43 | // Utility method to call SignedExchangeDevToolsProxy::ReportError() and |
Tsuyoshi Horo | 6361cb0 | 2018-06-04 04:36:02 | [diff] [blame] | 44 | // TRACE_EVENT_INSTANT1 to report the error to both DevTools and about:tracing. |
| 45 | // If |devtools_proxy| is nullptr, it just calls TRACE_EVENT_INSTANT1(). |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 46 | void ReportErrorAndTraceEvent(SignedExchangeDevToolsProxy* devtools_proxy, |
| 47 | const std::string& error_message, |
| 48 | std::optional<SignedExchangeError::FieldIndexPair> |
| 49 | error_field = std::nullopt); |
Tsuyoshi Horo | cdbb490 | 2018-04-12 06:09:14 | [diff] [blame] | 50 | |
Kunihiko Sakamoto | f586da6 | 2019-03-28 03:03:04 | [diff] [blame] | 51 | // Returns true when SignedHTTPExchange feature is enabled. This must be called |
Clark DuVall | a1220f7 | 2019-08-02 19:00:57 | [diff] [blame] | 52 | // on the UI thread. |
Clark DuVall | ab63d14 | 2019-07-23 04:24:36 | [diff] [blame] | 53 | CONTENT_EXPORT bool IsSignedExchangeHandlingEnabled(BrowserContext* context); |
Tsuyoshi Horo | 46f5fff | 2018-05-10 12:33:35 | [diff] [blame] | 54 | |
Tsuyoshi Horo | 4f5ce901 | 2019-02-27 01:04:45 | [diff] [blame] | 55 | // Returns true when SignedExchangeReportingForDistributors feature is enabled. |
| 56 | bool IsSignedExchangeReportingForDistributorsEnabled(); |
| 57 | |
Tsuyoshi Horo | 46f5fff | 2018-05-10 12:33:35 | [diff] [blame] | 58 | // Returns true when the response should be handled as a signed exchange by |
Kunihiko Sakamoto | f586da6 | 2019-03-28 03:03:04 | [diff] [blame] | 59 | // checking the url and the response headers. Note that the caller should also |
| 60 | // check IsSignedExchangeHandlingEnabled() before really enabling the feature. |
Tsuyoshi Horo | 46f5fff | 2018-05-10 12:33:35 | [diff] [blame] | 61 | bool ShouldHandleAsSignedHTTPExchange( |
| 62 | const GURL& request_url, |
Lucas Furukawa Gadani | d661c0d | 2019-12-02 19:58:16 | [diff] [blame] | 63 | const network::mojom::URLResponseHead& head); |
Tsuyoshi Horo | 46f5fff | 2018-05-10 12:33:35 | [diff] [blame] | 64 | |
Kunihiko Sakamoto | b5c94d90 | 2018-09-04 04:09:02 | [diff] [blame] | 65 | // Extracts the signed exchange version [1] from |content_type|, and converts it |
| 66 | // to SignedExchanveVersion. Returns nullopt if the mime type is not a variant |
| 67 | // of application/signed-exchange. Returns SignedExchangeVersion::kUnknown if an |
| 68 | // unsupported signed exchange version is found. |
| 69 | // [1] https://wicg.github.io/webpackage/loading.html#signed-exchange-version |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 70 | CONTENT_EXPORT std::optional<SignedExchangeVersion> GetSignedExchangeVersion( |
Matt Menke | 16ba457 | 2024-10-04 04:24:18 | [diff] [blame] | 71 | std::string_view content_type); |
Kunihiko Sakamoto | b5c94d90 | 2018-09-04 04:09:02 | [diff] [blame] | 72 | |
Tsuyoshi Horo | 06eb28f | 2019-02-21 13:52:24 | [diff] [blame] | 73 | // Returns the matching SignedExchangeLoadResult for the verifier's result. |
| 74 | // There is a gap between the logic of SignedExchangeSignatureVerifier and the |
| 75 | // spec of Loading Signed Exchanges [1]. This method is used to fill the gap |
| 76 | // and send a correct signed exchange report. |
| 77 | // [1] https://wicg.github.io/webpackage/loading.html |
| 78 | SignedExchangeLoadResult GetLoadResultFromSignatureVerifierResult( |
| 79 | SignedExchangeSignatureVerifier::Result verify_result); |
| 80 | |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 81 | // Creates a RedirectInfo of synthesized redirect for signed exchange loading. |
| 82 | net::RedirectInfo CreateRedirectInfo( |
| 83 | const GURL& new_url, |
| 84 | const network::ResourceRequest& outer_request, |
Lucas Furukawa Gadani | 62fcfa9 | 2019-12-05 15:38:41 | [diff] [blame] | 85 | const network::mojom::URLResponseHead& outer_response, |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 86 | bool is_fallback_redirect); |
| 87 | |
Lucas Furukawa Gadani | 62fcfa9 | 2019-12-05 15:38:41 | [diff] [blame] | 88 | // Creates a URLResponseHead of synthesized redirect for signed exchange |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 89 | // loading. |
Lucas Furukawa Gadani | 62fcfa9 | 2019-12-05 15:38:41 | [diff] [blame] | 90 | network::mojom::URLResponseHeadPtr CreateRedirectResponseHead( |
| 91 | const network::mojom::URLResponseHead& outer_response, |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 92 | bool is_fallback_redirect); |
| 93 | |
John Abd-El-Malek | d96edf3 | 2019-07-29 22:04:52 | [diff] [blame] | 94 | // Creates a new request ID for browser initiated requests. Can be called on |
| 95 | // any thread. |
| 96 | int MakeRequestID(); |
| 97 | |
Tsuyoshi Horo | af905953 | 2019-08-29 15:27:02 | [diff] [blame] | 98 | // Returns the time to be used for verifying signed exchange. Can be overridden |
| 99 | // using SetVerificationTimeForTesting(). |
| 100 | base::Time GetVerificationTime(); |
| 101 | |
| 102 | // Override the time which is used for verifying signed exchange. |
| 103 | CONTENT_EXPORT void SetVerificationTimeForTesting( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 104 | std::optional<base::Time> verification_time_for_testing); |
Tsuyoshi Horo | af905953 | 2019-08-29 15:27:02 | [diff] [blame] | 105 | |
Kunihiko Sakamoto | 6405d0af | 2021-11-18 00:46:37 | [diff] [blame] | 106 | bool IsCookielessOnlyExchange(const net::HttpResponseHeaders& inner_headers); |
| 107 | |
Kunihiko Sakamoto | b5c94d90 | 2018-09-04 04:09:02 | [diff] [blame] | 108 | } // namespace signed_exchange_utils |
Tsuyoshi Horo | cdbb490 | 2018-04-12 06:09:14 | [diff] [blame] | 109 | } // namespace content |
| 110 | |
| 111 | #endif // CONTENT_BROWSER_WEB_PACKAGE_SIGNED_EXCHANGE_UTILS_H_ |