File: protocol_handler_utils_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (321 lines) | stat: -rw-r--r-- 9,047 bytes parent folder | download | duplicates (5)
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
312
313
314
315
316
317
318
319
320
321
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/custom_handlers/protocol_handler_utils.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/security/protocol_handler_security_level.h"
#include "url/gurl.h"

namespace blink {

TEST(ProtocolHandlerUtilTest, ValidCustomHandlerSyntax) {
  struct {
    const char* title;
    std::string user_url;
  } test_cases[]{

      {
          "Basic case",
          "https://mydomain.com/path?=%s",
      },
      {
          "Unknown scheme",
          "scheme://mydomain.com/path?=%s",
      },
      {
          "Not Secure Context",
          "http://mydomain.com/path?=%s",
      },
      {
          "Opaque Origin",
          "data:/%s",
      }};

  for (const auto& test_case : test_cases) {
    SCOPED_TRACE(test_case.title);
    GURL full_url(test_case.user_url);
    URLSyntaxErrorCode code =
        IsValidCustomHandlerURLSyntax(full_url, test_case.user_url);
    EXPECT_EQ(code, URLSyntaxErrorCode::kNoError);
  };
}

TEST(ProtocolHandlerUtilTest, InvalidCustomHandlerSyntax) {
  struct {
    const char* title;
    std::string user_url;
    URLSyntaxErrorCode expected_error;
  } test_cases[]{
      // Invalid url
      {
          "Empty url",
          "",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "http://%s.com",
          "http://%s.com",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "http://%s.example.com",
          "http://%s.example.com",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "http://[v8.:::]//url=%s",
          "http://[v8.:::]//url=%s",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "https://test:test/",
          "https://test:test/",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "file://%s",
          "file://%s",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      // Missing token
      {
          "http://example.com/%",
          "http://example.com/%",
          URLSyntaxErrorCode::kMissingToken,
      },
      {
          "http://example.com/%a",
          "http://example.com/%a",
          URLSyntaxErrorCode::kMissingToken,
      },
      {
          "http://example.com",
          "http://example.com",
          URLSyntaxErrorCode::kMissingToken,
      },
  };

  for (const auto& test_case : test_cases) {
    SCOPED_TRACE(test_case.title);
    GURL full_url(test_case.user_url);
    URLSyntaxErrorCode code =
        IsValidCustomHandlerURLSyntax(full_url, test_case.user_url);
    EXPECT_EQ(code, test_case.expected_error);
  };
}

#if (BUILDFLAG(IS_WIN) && !defined(NDEBUG))
// Flaky on Windows
#define MAYBE_InvalidURL DISABLED_InvalidURL
#else
#define MAYBE_InvalidURL InvalidURL
#endif
TEST(ProtocolHandlerUtilTest, MAYBE_InvalidURL) {
  struct {
    const char* title;
    std::string user_url;
    URLSyntaxErrorCode expected_error;
  } test_cases[]{
      {
          "Empty url",
          "",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "http://%s.com",
          "http://%s.com",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "http://%s.example.com",
          "http://%s.example.com",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "http://[v8.:::]//url=%s",
          "http://[v8.:::]//url=%s",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "https://test:test/",
          "https://test:test/",
          URLSyntaxErrorCode::kInvalidUrl,
      },
      {
          "file://%s",
          "file://%s",
          URLSyntaxErrorCode::kInvalidUrl,
      },
  };

  for (const auto& test_case : test_cases) {
    SCOPED_TRACE(test_case.title);
    GURL full_url(test_case.user_url);
    EXPECT_FALSE(full_url.is_valid());
    URLSyntaxErrorCode code = IsValidCustomHandlerURLSyntax(full_url);
    EXPECT_EQ(code, test_case.expected_error);
  };
}

TEST(ProtocolHandlerUtilTest, ValidCustomHandlerSchemeSafeList) {
  static constexpr const char* const kProtocolSafelist[] = {
      "bitcoin", "cabal",  "dat",    "did",  "doi",  "dweb", "ethereum",
      "geo",     "hyper",  "im",     "ipfs", "ipns", "irc",  "ircs",
      "magnet",  "mailto", "matrix", "mms",  "news", "nntp", "openpgp4fpr",
      "sip",     "sms",    "smsto",  "ssb",  "ssh",  "tel",  "urn",
      "webcal",  "wtai",   "xmpp"};
  for (const auto* scheme : kProtocolSafelist) {
    SCOPED_TRACE(std::string("Testing safelist: ") + scheme);
    bool has_prefix = true;
    EXPECT_TRUE(IsValidCustomHandlerScheme(
        scheme, ProtocolHandlerSecurityLevel::kStrict, &has_prefix));
    EXPECT_FALSE(has_prefix);
  };
}

TEST(ProtocolHandlerUtilTest, CustomHandlerSchemePrefixed) {
  struct {
    const char* title;
    std::string scheme;
    ProtocolHandlerSecurityLevel security_level;
    bool is_valid;
    bool has_prefix;
  } test_cases[]{
      // Valid cases
      {
          "Basic 'web+' case",
          "web+scheme",
          ProtocolHandlerSecurityLevel::kStrict,
          true,
          true,
      },
      {
          "Basic 'ext+' case",
          "ext+scheme",
          ProtocolHandlerSecurityLevel::kExtensionFeatures,
          true,
          true,
      },
      // Prefix ext+ not allowed by the security level
      {
          "Prefixed scheme not allowed with kStrict",
          "ext+scheme",
          ProtocolHandlerSecurityLevel::kStrict,
          false,
          false,
      },
      {
          "Prefixed scheme not allowed with kSameOrigin",
          "ext+search",
          ProtocolHandlerSecurityLevel::kSameOrigin,
          false,
          false,
      },
      {
          "Prefixed scheme not allowed with kUntrustedOrigin",
          "ext+protocol",
          ProtocolHandlerSecurityLevel::kUntrustedOrigins,
          false,
      },
      {
          "Prefix ext+ must have a non-empty sufix",
          "ext+",
          ProtocolHandlerSecurityLevel::kExtensionFeatures,
          false,
          true,
      },
      {
          "Prefix web+ must have a non-empty sufix",
          "web+",
          ProtocolHandlerSecurityLevel::kExtensionFeatures,
          false,
          true,
      },
  };

  for (const auto& test_case : test_cases) {
    SCOPED_TRACE(test_case.title);
    bool has_prefix = false;
    bool is_valid = IsValidCustomHandlerScheme(
        test_case.scheme, test_case.security_level, &has_prefix);
    EXPECT_EQ(is_valid, test_case.is_valid);
    EXPECT_EQ(has_prefix, test_case.has_prefix);
  };
}

TEST(ProtocolHandlerUtilTest, CustomHandlerFTPIncludedInSafelist) {
  // The kSafelistFTPToRegisterProtocolHandler feature is enabled by default.
  static constexpr const char* const kProtocolSafelist[] = {"ftp", "ftps",
                                                            "sftp"};
  for (const auto* scheme : kProtocolSafelist) {
    SCOPED_TRACE(std::string("Testing FTP schemes are included in safelist: ") +
                 scheme);
    EXPECT_TRUE(IsValidCustomHandlerScheme(
        scheme, ProtocolHandlerSecurityLevel::kStrict));
  };
}

TEST(ProtocolHandlerUtilTest, CustomHandlerPaytoNotIncludedInSafelist) {
  // The kSafelistPaytoToRegisterProtocolHandler feature is disabled by default.
  SCOPED_TRACE("Testing 'payto' is not included in safelist.");
  EXPECT_FALSE(IsValidCustomHandlerScheme(
      "payto", ProtocolHandlerSecurityLevel::kStrict));
}

TEST(ProtocolHandlerUtilTest, CustomHandlerURLIsAllowed) {
  struct {
    const char* title;
    std::string url;
    ProtocolHandlerSecurityLevel security_level;
    bool is_allowed;
  } test_cases[]{
      // Allowed URLs
      {
          "HTTPS scheme and Trusworthy url",
          "https://example.com",
          ProtocolHandlerSecurityLevel::kStrict,
          true,
      },
      {
          "File scheme with SameOrigin security level",
          "file://path",
          ProtocolHandlerSecurityLevel::kSameOrigin,
          true,
      },
      {
          "Data scheme with SameOrigin security level",
          "data://path",
          ProtocolHandlerSecurityLevel::kSameOrigin,
          true,
      },
      // Not allowed URLs
      {
          "HTTP schemes doesn't provide a Trusworthy url",
          "http://example.com",
          ProtocolHandlerSecurityLevel::kStrict,
          false,
      },
      {
          "File scheme with Strict securoty level",
          "file://path",
          ProtocolHandlerSecurityLevel::kStrict,
          false,
      },
      {
          "Data scheme with Strict securoty level",
          "data://path",
          ProtocolHandlerSecurityLevel::kStrict,
          false,
      },
  };
  for (const auto& test_case : test_cases) {
    SCOPED_TRACE(test_case.title);
    GURL url(test_case.url);
    bool is_allowed = IsAllowedCustomHandlerURL(url, test_case.security_level);
    EXPECT_EQ(is_allowed, test_case.is_allowed);
  };
}

}  // namespace blink