summaryrefslogtreecommitdiff
path: root/chromium/content/common/content_security_policy/content_security_policy_unittest.cc
blob: a7e57115ffc12d5087edd96b1865ac2d282cc910 (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/common/content_security_policy/csp_context.h"
#include "content/common/content_security_policy_header.h"
#include "content/common/navigation_params.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

namespace {
class CSPContextTest : public CSPContext {
 public:
  CSPContextTest() : CSPContext() {}

  const std::vector<CSPViolationParams>& violations() { return violations_; }

  void AddSchemeToBypassCSP(const std::string& scheme) {
    scheme_to_bypass_.push_back(scheme);
  }

  bool SchemeShouldBypassCSP(const base::StringPiece& scheme) override {
    return std::find(scheme_to_bypass_.begin(), scheme_to_bypass_.end(),
                     scheme) != scheme_to_bypass_.end();
  }

 private:
  void ReportContentSecurityPolicyViolation(
      const CSPViolationParams& violation_params) override {
    violations_.push_back(violation_params);
  }
  std::vector<CSPViolationParams> violations_;
  std::vector<std::string> scheme_to_bypass_;

  DISALLOW_COPY_AND_ASSIGN(CSPContextTest);
};

ContentSecurityPolicyHeader EmptyCspHeader() {
  return ContentSecurityPolicyHeader(
      std::string(), blink::kWebContentSecurityPolicyTypeEnforce,
      blink::kWebContentSecurityPolicySourceHTTP);
}

}  // namespace

TEST(ContentSecurityPolicy, NoDirective) {
  CSPContextTest context;
  std::vector<std::string> report_end_points;  // empty
  ContentSecurityPolicy policy(EmptyCspHeader(), std::vector<CSPDirective>(),
                               report_end_points, false);

  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FormAction, GURL("http://www.example.com"), false,
      false, &context, SourceLocation(), true));
  ASSERT_EQ(0u, context.violations().size());
}

TEST(ContentSecurityPolicy, ReportViolation) {
  CSPContextTest context;

  // source = "www.example.com"
  CSPSource source("", "www.example.com", false, url::PORT_UNSPECIFIED, false,
                   "");
  CSPSourceList source_list(false, false, false, {source});
  CSPDirective directive(CSPDirective::FormAction, source_list);
  std::vector<std::string> report_end_points;  // empty
  ContentSecurityPolicy policy(EmptyCspHeader(), {directive}, report_end_points,
                               false);

  EXPECT_FALSE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FormAction, GURL("http://www.not-example.com"),
      false, false, &context, SourceLocation(), true));

  ASSERT_EQ(1u, context.violations().size());
  const char console_message[] =
      "Refused to send form data to 'http://www.not-example.com/' because it "
      "violates the following Content Security Policy directive: \"form-action "
      "www.example.com\".\n";
  EXPECT_EQ(console_message, context.violations()[0].console_message);
}

TEST(ContentSecurityPolicy, DirectiveFallback) {
  CSPSource source_a("http", "a.com", false, url::PORT_UNSPECIFIED, false, "");
  CSPSource source_b("http", "b.com", false, url::PORT_UNSPECIFIED, false, "");
  CSPSourceList source_list_a(false, false, false, {source_a});
  CSPSourceList source_list_b(false, false, false, {source_b});

  std::vector<std::string> report_end_points;  // Empty.

  {
    CSPContextTest context;
    ContentSecurityPolicy policy(
        EmptyCspHeader(),
        {CSPDirective(CSPDirective::DefaultSrc, source_list_a)},
        report_end_points, false);
    EXPECT_FALSE(ContentSecurityPolicy::Allow(
        policy, CSPDirective::FrameSrc, GURL("http://b.com"), false, false,
        &context, SourceLocation(), false));
    ASSERT_EQ(1u, context.violations().size());
    const char console_message[] =
        "Refused to frame 'http://b.com/' because it violates "
        "the following Content Security Policy directive: \"default-src "
        "http://a.com\". Note that 'frame-src' was not explicitly "
        "set, so 'default-src' is used as a fallback.\n";
    EXPECT_EQ(console_message, context.violations()[0].console_message);
    EXPECT_TRUE(ContentSecurityPolicy::Allow(
        policy, CSPDirective::FrameSrc, GURL("http://a.com"), false, false,
        &context, SourceLocation(), false));
  }
  {
    CSPContextTest context;
    ContentSecurityPolicy policy(
        EmptyCspHeader(), {CSPDirective(CSPDirective::ChildSrc, source_list_a)},
        report_end_points, false);
    EXPECT_FALSE(ContentSecurityPolicy::Allow(
        policy, CSPDirective::FrameSrc, GURL("http://b.com"), false, false,
        &context, SourceLocation(), false));
    ASSERT_EQ(1u, context.violations().size());
    const char console_message[] =
        "Refused to frame 'http://b.com/' because it violates "
        "the following Content Security Policy directive: \"child-src "
        "http://a.com\". Note that 'frame-src' was not explicitly "
        "set, so 'child-src' is used as a fallback.\n";
    EXPECT_EQ(console_message, context.violations()[0].console_message);
    EXPECT_TRUE(ContentSecurityPolicy::Allow(
        policy, CSPDirective::FrameSrc, GURL("http://a.com"), false, false,
        &context, SourceLocation(), false));
  }
  {
    CSPContextTest context;
    CSPSourceList source_list(false, false, false, {source_a, source_b});
    ContentSecurityPolicy policy(
        EmptyCspHeader(),
        {CSPDirective(CSPDirective::FrameSrc, {source_list_a}),
         CSPDirective(CSPDirective::ChildSrc, {source_list_b})},
        report_end_points, false);
    EXPECT_TRUE(ContentSecurityPolicy::Allow(
        policy, CSPDirective::FrameSrc, GURL("http://a.com"), false, false,
        &context, SourceLocation(), false));
    EXPECT_FALSE(ContentSecurityPolicy::Allow(
        policy, CSPDirective::FrameSrc, GURL("http://b.com"), false, false,
        &context, SourceLocation(), false));
    ASSERT_EQ(1u, context.violations().size());
    const char console_message[] =
        "Refused to frame 'http://b.com/' because it violates "
        "the following Content Security Policy directive: \"frame-src "
        "http://a.com\".\n";
    EXPECT_EQ(console_message, context.violations()[0].console_message);
  }
}

TEST(ContentSecurityPolicy, RequestsAllowedWhenBypassingCSP) {
  CSPContextTest context;
  std::vector<std::string> report_end_points;  // empty
  CSPSource source("https", "example.com", false, url::PORT_UNSPECIFIED, false,
                   "");
  CSPSourceList source_list(false, false, false, {source});
  ContentSecurityPolicy policy(
      EmptyCspHeader(), {CSPDirective(CSPDirective::DefaultSrc, source_list)},
      report_end_points, false);

  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("https://example.com/"), false,
      false, &context, SourceLocation(), false));
  EXPECT_FALSE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("https://not-example.com/"), false,
      false, &context, SourceLocation(), false));

  // Register 'https' as bypassing CSP, which should now bypass is entirely.
  context.AddSchemeToBypassCSP("https");

  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("https://example.com/"), false,
      false, &context, SourceLocation(), false));
  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("https://not-example.com/"), false,
      false, &context, SourceLocation(), false));
}

TEST(ContentSecurityPolicy, FilesystemAllowedWhenBypassingCSP) {
  CSPContextTest context;
  std::vector<std::string> report_end_points;  // empty
  CSPSource source("https", "example.com", false, url::PORT_UNSPECIFIED, false,
                   "");
  CSPSourceList source_list(false, false, false, {source});
  ContentSecurityPolicy policy(
      EmptyCspHeader(), {CSPDirective(CSPDirective::DefaultSrc, source_list)},
      report_end_points, false);

  EXPECT_FALSE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc,
      GURL("filesystem:https://example.com/file.txt"), false, false, &context,
      SourceLocation(), false));
  EXPECT_FALSE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc,
      GURL("filesystem:https://not-example.com/file.txt"), false, false,
      &context, SourceLocation(), false));

  // Register 'https' as bypassing CSP, which should now bypass is entirely.
  context.AddSchemeToBypassCSP("https");

  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc,
      GURL("filesystem:https://example.com/file.txt"), false, false, &context,
      SourceLocation(), false));
  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc,
      GURL("filesystem:https://not-example.com/file.txt"), false, false,
      &context, SourceLocation(), false));
}

TEST(ContentSecurityPolicy, BlobAllowedWhenBypassingCSP) {
  CSPContextTest context;
  std::vector<std::string> report_end_points;  // empty
  CSPSource source("https", "example.com", false, url::PORT_UNSPECIFIED, false,
                   "");
  CSPSourceList source_list(false, false, false, {source});
  ContentSecurityPolicy policy(
      EmptyCspHeader(), {CSPDirective(CSPDirective::DefaultSrc, source_list)},
      report_end_points, false);

  EXPECT_FALSE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("blob:https://example.com/"), false,
      false, &context, SourceLocation(), false));
  EXPECT_FALSE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("blob:https://not-example.com/"),
      false, false, &context, SourceLocation(), false));

  // Register 'https' as bypassing CSP, which should now bypass is entirely.
  context.AddSchemeToBypassCSP("https");

  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("blob:https://example.com/"), false,
      false, &context, SourceLocation(), false));
  EXPECT_TRUE(ContentSecurityPolicy::Allow(
      policy, CSPDirective::FrameSrc, GURL("blob:https://not-example.com/"),
      false, false, &context, SourceLocation(), false));
}

TEST(ContentSecurityPolicy, ShouldUpgradeInsecureRequest) {
  std::vector<std::string> report_end_points;  // empty
  CSPSource source("https", "example.com", false, url::PORT_UNSPECIFIED, false,
                   "");
  CSPSourceList source_list(false, false, false, {source});
  ContentSecurityPolicy policy(
      EmptyCspHeader(), {CSPDirective(CSPDirective::DefaultSrc, source_list)},
      report_end_points, false);

  EXPECT_FALSE(ContentSecurityPolicy::ShouldUpgradeInsecureRequest(policy));

  policy.directives.push_back(
      CSPDirective(CSPDirective::UpgradeInsecureRequests, CSPSourceList()));
  EXPECT_TRUE(ContentSecurityPolicy::ShouldUpgradeInsecureRequest(policy));
}

TEST(ContentSecurityPolicy, NavigateToChecks) {
  CSPContextTest context;
  std::vector<std::string> report_end_points;  // empty
  CSPSource example("https", "example.test", false, url::PORT_UNSPECIFIED,
                    false, "");
  CSPSourceList none_source_list(false, false, false, {});
  CSPSourceList example_source_list(false, false, false, {example});
  CSPSourceList self_source_list(true, false, false, {});
  CSPSourceList redirects_source_list(false, false, true, {});
  CSPSourceList redirects_example_source_list(false, false, true, {example});
  context.SetSelf(example);

  struct TestCase {
    const CSPSourceList& navigate_to_list;
    const GURL& url;
    bool is_redirect;
    bool is_response_check;
    bool expected;
    bool is_form_submission;
    const CSPSourceList* form_action_list;
  } cases[] = {
      // Basic source matching.
      {none_source_list, GURL("https://example.test"), false, false, false,
       false, nullptr},
      {example_source_list, GURL("https://example.test"), false, false, true,
       false, nullptr},
      {example_source_list, GURL("https://not-example.test"), false, false,
       false, false, nullptr},
      {self_source_list, GURL("https://example.test"), false, false, true,
       false, nullptr},

      // Checking allow_redirect flag interactions.
      {redirects_source_list, GURL("https://example.test"), false, false, true,
       false, nullptr},
      {redirects_source_list, GURL("https://example.test"), true, false, true,
       false, nullptr},
      {redirects_source_list, GURL("https://example.test"), true, true, true,
       false, nullptr},
      {redirects_source_list, GURL("https://example.test"), false, true, false,
       false, nullptr},
      {redirects_example_source_list, GURL("https://example.test"), false, true,
       true, false, nullptr},

      // Interaction with form-action

      // Form submission without form-action present
      {none_source_list, GURL("https://example.test"), false, false, false,
       true, nullptr},
      {example_source_list, GURL("https://example.test"), false, false, true,
       true, nullptr},
      {example_source_list, GURL("https://not-example.test"), false, false,
       false, true, nullptr},
      {self_source_list, GURL("https://example.test"), false, false, true, true,
       nullptr},

      // Form submission with form-action present
      {none_source_list, GURL("https://example.test"), false, false, true, true,
       &example_source_list},
      {example_source_list, GURL("https://example.test"), false, false, true,
       true, &example_source_list},
      {example_source_list, GURL("https://not-example.test"), false, false,
       true, true, &example_source_list},
      {self_source_list, GURL("https://example.test"), false, false, true, true,
       &example_source_list},

  };

  for (const auto& test : cases) {
    std::vector<CSPDirective> directives;
    directives.push_back(
        CSPDirective(CSPDirective::NavigateTo, test.navigate_to_list));

    if (test.form_action_list)
      directives.push_back(
          CSPDirective(CSPDirective::FormAction, *(test.form_action_list)));

    ContentSecurityPolicy policy(EmptyCspHeader(), directives,
                                 report_end_points, false);

    EXPECT_EQ(test.expected,
              ContentSecurityPolicy::Allow(
                  policy, CSPDirective::NavigateTo, test.url, test.is_redirect,
                  test.is_response_check, &context, SourceLocation(),
                  test.is_form_submission));
  }
}

}  // namespace content