blob: b78e8c268b8841c274c8a574308167da2efa5e0f [file] [log] [blame]
[email protected]ae26b282014-05-15 16:40:161// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
vivek.vgd155cb802014-12-22 14:37:105#include "chrome/test/base/chrome_render_view_test.h"
mekcbb4b862014-09-05 03:08:226#include "content/public/renderer/render_frame.h"
7#include "content/public/test/frame_load_waiter.h"
Devlin Cronin6d96d3d2020-08-11 19:46:278#include "extensions/common/script_constants.h"
[email protected]ae26b282014-05-15 16:40:169#include "extensions/renderer/script_context.h"
Jeremy Roman1a4d9b82017-12-08 01:46:4010#include "extensions/renderer/script_context_set.h"
Dave Tapuskab9cb2532020-09-18 20:15:0711#include "third_party/blink/public/platform/web_runtime_features.h"
Blink Reformata30d4232018-04-07 15:31:0612#include "third_party/blink/public/web/web_document.h"
13#include "third_party/blink/public/web/web_local_frame.h"
[email protected]ae26b282014-05-15 16:40:1614#include "url/gurl.h"
15
Daniel Cheng971cd4522017-05-31 21:58:2216using blink::WebLocalFrame;
[email protected]ae26b282014-05-15 16:40:1617
18namespace extensions {
19namespace {
20
vivek.vgd155cb802014-12-22 14:37:1021class ScriptContextTest : public ChromeRenderViewTest {
[email protected]ae26b282014-05-15 16:40:1622 protected:
Devlin Croninb8d8fee2020-07-28 19:03:3823 GURL GetEffectiveDocumentURLForContext(WebLocalFrame* frame) {
24 return ScriptContext::GetEffectiveDocumentURLForContext(
Devlin Cronin3a7cad92020-07-30 19:43:5825 frame, frame->GetDocument().Url(), /*match_about_blank=*/true);
Devlin Croninb8d8fee2020-07-28 19:03:3826 }
Devlin Cronin6d96d3d2020-08-11 19:46:2727 GURL GetEffectiveDocumentURLForInjection(
28 WebLocalFrame* frame,
29 MatchOriginAsFallbackBehavior match_origin_as_fallback =
30 MatchOriginAsFallbackBehavior::kAlways) {
Devlin Croninb8d8fee2020-07-28 19:03:3831 return ScriptContext::GetEffectiveDocumentURLForInjection(
Devlin Cronin6d96d3d2020-08-11 19:46:2732 frame, frame->GetDocument().Url(), match_origin_as_fallback);
[email protected]ae26b282014-05-15 16:40:1633 }
34};
35
mekcbb4b862014-09-05 03:08:2236TEST_F(ScriptContextTest, GetEffectiveDocumentURL) {
[email protected]ae26b282014-05-15 16:40:1637 GURL top_url("http://example.com/");
38 GURL different_url("http://example.net/");
39 GURL blank_url("about:blank");
40 GURL srcdoc_url("about:srcdoc");
Devlin Cronin3a7cad92020-07-30 19:43:5841 GURL data_url("data:text/html,<html>Hi</html>");
[email protected]ae26b282014-05-15 16:40:1642
43 const char frame_html[] =
Devlin Croninb8d8fee2020-07-28 19:03:3844 R"(<iframe name='frame1' srcdoc="
45 <iframe name='frame1_1'></iframe>
46 <iframe name='frame1_2' sandbox=''></iframe>
47 "></iframe>
48 <iframe name='frame2' sandbox='' srcdoc="
49 <iframe name='frame2_1'></iframe>
50 "></iframe>
Devlin Cronin3a7cad92020-07-30 19:43:5851 <iframe name='frame3'></iframe>
52 <iframe name='frame4' src="data:text/html,<html>Hi</html>"></iframe>
53 <iframe name='frame5'
54 src="data:text/html,<html>Hi</html>"
Dave Tapuskab9cb2532020-09-18 20:15:0755 sandbox=''></iframe>
56 <iframe disallowdocumentaccess name='frame6'></iframe>)";
[email protected]ae26b282014-05-15 16:40:1657
Devlin Croninb8d8fee2020-07-28 19:03:3858 const char frame3_html[] =
59 R"(<iframe name='frame3_1'></iframe>
Devlin Cronin3a7cad92020-07-30 19:43:5860 <iframe name='frame3_2' sandbox=''></iframe>
61 <iframe name='frame3_3'
62 src="data:text/html,<html>Hi</html>"></iframe>)";
[email protected]ae26b282014-05-15 16:40:1663
Daniel Cheng971cd4522017-05-31 21:58:2264 WebLocalFrame* frame = GetMainFrame();
[email protected]ae26b282014-05-15 16:40:1665 ASSERT_TRUE(frame);
Dave Tapuskab9cb2532020-09-18 20:15:0766 blink::WebRuntimeFeatures::EnableDisallowDocumentAccess(true);
[email protected]ae26b282014-05-15 16:40:1667
Dmitry Gozmand96e493a82018-11-28 01:13:3368 content::RenderFrame::FromWebFrame(frame)->LoadHTMLString(
69 frame_html, top_url, "UTF-8", GURL(), false /* replace_current_item */);
mekcbb4b862014-09-05 03:08:2270 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait();
[email protected]ae26b282014-05-15 16:40:1671
Daniel Cheng971cd4522017-05-31 21:58:2272 WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1673 ASSERT_TRUE(frame1);
Blink Reformat1c4d759e2017-04-09 16:34:5474 ASSERT_EQ("frame1", frame1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2275 WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1676 ASSERT_TRUE(frame1_1);
Blink Reformat1c4d759e2017-04-09 16:34:5477 ASSERT_EQ("frame1_1", frame1_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2278 WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1679 ASSERT_TRUE(frame1_2);
Blink Reformat1c4d759e2017-04-09 16:34:5480 ASSERT_EQ("frame1_2", frame1_2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2281 WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1682 ASSERT_TRUE(frame2);
Blink Reformat1c4d759e2017-04-09 16:34:5483 ASSERT_EQ("frame2", frame2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2284 WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1685 ASSERT_TRUE(frame2_1);
Blink Reformat1c4d759e2017-04-09 16:34:5486 ASSERT_EQ("frame2_1", frame2_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2287 WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1688 ASSERT_TRUE(frame3);
Blink Reformat1c4d759e2017-04-09 16:34:5489 ASSERT_EQ("frame3", frame3->AssignedName());
Devlin Cronin3a7cad92020-07-30 19:43:5890 WebLocalFrame* frame4 = frame3->NextSibling()->ToWebLocalFrame();
91 ASSERT_TRUE(frame4);
92 ASSERT_EQ("frame4", frame4->AssignedName());
93 WebLocalFrame* frame5 = frame4->NextSibling()->ToWebLocalFrame();
94 ASSERT_TRUE(frame5);
95 ASSERT_EQ("frame5", frame5->AssignedName());
Dave Tapuskab9cb2532020-09-18 20:15:0796 WebLocalFrame* frame6 = frame5->NextSibling()->ToWebLocalFrame();
97 ASSERT_TRUE(frame6);
98 ASSERT_EQ("frame6", frame6->AssignedName());
[email protected]ae26b282014-05-15 16:40:1699
100 // Load a blank document in a frame from a different origin.
Dmitry Gozmand96e493a82018-11-28 01:13:33101 content::RenderFrame::FromWebFrame(frame3)->LoadHTMLString(
102 frame3_html, different_url, "UTF-8", GURL(),
103 false /* replace_current_item */);
mekcbb4b862014-09-05 03:08:22104 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait();
[email protected]ae26b282014-05-15 16:40:16105
Daniel Cheng971cd4522017-05-31 21:58:22106 WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:16107 ASSERT_TRUE(frame3_1);
Blink Reformat1c4d759e2017-04-09 16:34:54108 ASSERT_EQ("frame3_1", frame3_1->AssignedName());
Devlin Croninb8d8fee2020-07-28 19:03:38109 WebLocalFrame* frame3_2 = frame3_1->NextSibling()->ToWebLocalFrame();
110 ASSERT_TRUE(frame3_2);
111 ASSERT_EQ("frame3_2", frame3_2->AssignedName());
Devlin Cronin3a7cad92020-07-30 19:43:58112 WebLocalFrame* frame3_3 = frame3_2->NextSibling()->ToWebLocalFrame();
113 ASSERT_TRUE(frame3_3);
114 ASSERT_EQ("frame3_3", frame3_3->AssignedName());
[email protected]ae26b282014-05-15 16:40:16115
116 // Top-level frame
Devlin Croninb8d8fee2020-07-28 19:03:38117 EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame));
118 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame));
[email protected]ae26b282014-05-15 16:40:16119 // top -> srcdoc = inherit
Devlin Croninb8d8fee2020-07-28 19:03:38120 EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame1));
121 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1));
[email protected]ae26b282014-05-15 16:40:16122 // top -> srcdoc -> about:blank = inherit
Devlin Croninb8d8fee2020-07-28 19:03:38123 EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame1_1));
124 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1_1));
125 // top -> srcdoc -> about:blank sandboxed = same URL when classifying
126 // contexts, but inherited url when injecting scripts.
127 EXPECT_EQ(blank_url, GetEffectiveDocumentURLForContext(frame1_2));
128 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1_2));
[email protected]ae26b282014-05-15 16:40:16129
Devlin Croninb8d8fee2020-07-28 19:03:38130 // top -> srcdoc [sandboxed] = same URL when classifying contexts,
131 // but inherited url when injecting scripts.
132 EXPECT_EQ(srcdoc_url, GetEffectiveDocumentURLForContext(frame2));
133 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame2));
134 // top -> srcdoc [sandboxed] -> about:blank = same URL when classifying
135 // contexts, but inherited url when injecting scripts.
136 EXPECT_EQ(blank_url, GetEffectiveDocumentURLForContext(frame2_1));
137 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame2_1));
[email protected]ae26b282014-05-15 16:40:16138
Devlin Cronin3a7cad92020-07-30 19:43:58139 // top -> data URL = same URL when classifying contexts, but inherited when
140 // injecting scripts.
141 EXPECT_EQ(data_url, GetEffectiveDocumentURLForContext(frame4));
142 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame4));
Devlin Cronin6d96d3d2020-08-11 19:46:27143 // Sanity-check: if we only match about: schemes, the original URL should be
Devlin Cronin3a7cad92020-07-30 19:43:58144 // returned.
Devlin Cronin6d96d3d2020-08-11 19:46:27145 EXPECT_EQ(
146 data_url,
147 GetEffectiveDocumentURLForInjection(
148 frame4,
149 MatchOriginAsFallbackBehavior::kMatchForAboutSchemeAndClimbTree));
Devlin Cronin3a7cad92020-07-30 19:43:58150
151 // top -> sandboxed data URL = same URL when classifying contexts, but
152 // inherited when injecting scripts.
153 EXPECT_EQ(data_url, GetEffectiveDocumentURLForContext(frame5));
154 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame5));
Devlin Cronin6d96d3d2020-08-11 19:46:27155 // Sanity-check: if we only match about: schemes, the original URL should be
Devlin Cronin3a7cad92020-07-30 19:43:58156 // returned.
Devlin Cronin6d96d3d2020-08-11 19:46:27157 EXPECT_EQ(
158 data_url,
159 GetEffectiveDocumentURLForInjection(
160 frame5,
161 MatchOriginAsFallbackBehavior::kMatchForAboutSchemeAndClimbTree));
Devlin Cronin3a7cad92020-07-30 19:43:58162
Dave Tapuskab9cb2532020-09-18 20:15:07163 // Documents with disallowed access (disallowdocumentaccess) behave much like
164 // sandboxed documents.
165 EXPECT_EQ(blank_url, GetEffectiveDocumentURLForContext(frame6));
166 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame6));
167
[email protected]ae26b282014-05-15 16:40:16168 // top -> different origin = different origin
Devlin Croninb8d8fee2020-07-28 19:03:38169 EXPECT_EQ(different_url, GetEffectiveDocumentURLForContext(frame3));
170 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3));
171 // top -> different origin -> about:blank = inherit (from different origin)
172 EXPECT_EQ(different_url, GetEffectiveDocumentURLForContext(frame3_1));
173 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3_1));
174 // top -> different origin -> about:blank sandboxed = same URL when
175 // classifying contexts, but inherited (from different origin) url when
176 // injecting scripts.
177 EXPECT_EQ(blank_url, GetEffectiveDocumentURLForContext(frame3_2));
178 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3_2));
Devlin Cronin3a7cad92020-07-30 19:43:58179 // top -> different origin -> data URL = same URL when classifying contexts,
180 // but inherited (from different origin) url when injecting scripts.
181 EXPECT_EQ(data_url, GetEffectiveDocumentURLForContext(frame3_3));
182 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3_3));
[email protected]ae26b282014-05-15 16:40:16183}
184
Jeremy Roman1a4d9b82017-12-08 01:46:40185TEST_F(ScriptContextTest, GetMainWorldContextForFrame) {
186 // ScriptContextSet::GetMainWorldContextForFrame should work, even without an
187 // existing v8::HandleScope.
188 content::RenderFrame* render_frame =
189 content::RenderFrame::FromWebFrame(GetMainFrame());
190 ScriptContext* script_context =
191 ScriptContextSet::GetMainWorldContextForFrame(render_frame);
192 ASSERT_TRUE(script_context);
193 EXPECT_EQ(render_frame, script_context->GetRenderFrame());
194}
195
[email protected]ae26b282014-05-15 16:40:16196} // namespace
197} // namespace extensions