blob: ce7e9c23fc3368fc275be27dfd0c791e0cc558e9 [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"
[email protected]ae26b282014-05-15 16:40:168#include "extensions/renderer/script_context.h"
Jeremy Roman1a4d9b82017-12-08 01:46:409#include "extensions/renderer/script_context_set.h"
Blink Reformata30d4232018-04-07 15:31:0610#include "third_party/blink/public/web/web_document.h"
11#include "third_party/blink/public/web/web_local_frame.h"
[email protected]ae26b282014-05-15 16:40:1612#include "url/gurl.h"
13
Daniel Cheng971cd4522017-05-31 21:58:2214using blink::WebLocalFrame;
[email protected]ae26b282014-05-15 16:40:1615
16namespace extensions {
17namespace {
18
vivek.vgd155cb802014-12-22 14:37:1019class ScriptContextTest : public ChromeRenderViewTest {
[email protected]ae26b282014-05-15 16:40:1620 protected:
Devlin Croninb8d8fee2020-07-28 19:03:3821 GURL GetEffectiveDocumentURLForContext(WebLocalFrame* frame) {
22 return ScriptContext::GetEffectiveDocumentURLForContext(
Devlin Cronin3a7cad92020-07-30 19:43:5823 frame, frame->GetDocument().Url(), /*match_about_blank=*/true);
Devlin Croninb8d8fee2020-07-28 19:03:3824 }
25 GURL GetEffectiveDocumentURLForInjection(WebLocalFrame* frame) {
26 return ScriptContext::GetEffectiveDocumentURLForInjection(
Devlin Cronin3a7cad92020-07-30 19:43:5827 frame, frame->GetDocument().Url(),
28 /*match_about_blank=*/true, /*match_data_urls=*/true);
29 }
30 GURL GetEffectiveURLForInjectionWithoutMatchingData(WebLocalFrame* frame) {
31 return ScriptContext::GetEffectiveDocumentURLForInjection(
32 frame, frame->GetDocument().Url(),
33 /*match_about_blank=*/true, /*match_data_urls=*/false);
[email protected]ae26b282014-05-15 16:40:1634 }
35};
36
mekcbb4b862014-09-05 03:08:2237TEST_F(ScriptContextTest, GetEffectiveDocumentURL) {
[email protected]ae26b282014-05-15 16:40:1638 GURL top_url("http://example.com/");
39 GURL different_url("http://example.net/");
40 GURL blank_url("about:blank");
41 GURL srcdoc_url("about:srcdoc");
Devlin Cronin3a7cad92020-07-30 19:43:5842 GURL data_url("data:text/html,<html>Hi</html>");
[email protected]ae26b282014-05-15 16:40:1643
44 const char frame_html[] =
Devlin Croninb8d8fee2020-07-28 19:03:3845 R"(<iframe name='frame1' srcdoc="
46 <iframe name='frame1_1'></iframe>
47 <iframe name='frame1_2' sandbox=''></iframe>
48 "></iframe>
49 <iframe name='frame2' sandbox='' srcdoc="
50 <iframe name='frame2_1'></iframe>
51 "></iframe>
Devlin Cronin3a7cad92020-07-30 19:43:5852 <iframe name='frame3'></iframe>
53 <iframe name='frame4' src="data:text/html,<html>Hi</html>"></iframe>
54 <iframe name='frame5'
55 src="data:text/html,<html>Hi</html>"
56 sandbox=''></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);
66
Dmitry Gozmand96e493a82018-11-28 01:13:3367 content::RenderFrame::FromWebFrame(frame)->LoadHTMLString(
68 frame_html, top_url, "UTF-8", GURL(), false /* replace_current_item */);
mekcbb4b862014-09-05 03:08:2269 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait();
[email protected]ae26b282014-05-15 16:40:1670
Daniel Cheng971cd4522017-05-31 21:58:2271 WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1672 ASSERT_TRUE(frame1);
Blink Reformat1c4d759e2017-04-09 16:34:5473 ASSERT_EQ("frame1", frame1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2274 WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1675 ASSERT_TRUE(frame1_1);
Blink Reformat1c4d759e2017-04-09 16:34:5476 ASSERT_EQ("frame1_1", frame1_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2277 WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1678 ASSERT_TRUE(frame1_2);
Blink Reformat1c4d759e2017-04-09 16:34:5479 ASSERT_EQ("frame1_2", frame1_2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2280 WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1681 ASSERT_TRUE(frame2);
Blink Reformat1c4d759e2017-04-09 16:34:5482 ASSERT_EQ("frame2", frame2->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2283 WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1684 ASSERT_TRUE(frame2_1);
Blink Reformat1c4d759e2017-04-09 16:34:5485 ASSERT_EQ("frame2_1", frame2_1->AssignedName());
Daniel Cheng971cd4522017-05-31 21:58:2286 WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:1687 ASSERT_TRUE(frame3);
Blink Reformat1c4d759e2017-04-09 16:34:5488 ASSERT_EQ("frame3", frame3->AssignedName());
Devlin Cronin3a7cad92020-07-30 19:43:5889 WebLocalFrame* frame4 = frame3->NextSibling()->ToWebLocalFrame();
90 ASSERT_TRUE(frame4);
91 ASSERT_EQ("frame4", frame4->AssignedName());
92 WebLocalFrame* frame5 = frame4->NextSibling()->ToWebLocalFrame();
93 ASSERT_TRUE(frame5);
94 ASSERT_EQ("frame5", frame5->AssignedName());
[email protected]ae26b282014-05-15 16:40:1695
96 // Load a blank document in a frame from a different origin.
Dmitry Gozmand96e493a82018-11-28 01:13:3397 content::RenderFrame::FromWebFrame(frame3)->LoadHTMLString(
98 frame3_html, different_url, "UTF-8", GURL(),
99 false /* replace_current_item */);
mekcbb4b862014-09-05 03:08:22100 content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait();
[email protected]ae26b282014-05-15 16:40:16101
Daniel Cheng971cd4522017-05-31 21:58:22102 WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame();
[email protected]ae26b282014-05-15 16:40:16103 ASSERT_TRUE(frame3_1);
Blink Reformat1c4d759e2017-04-09 16:34:54104 ASSERT_EQ("frame3_1", frame3_1->AssignedName());
Devlin Croninb8d8fee2020-07-28 19:03:38105 WebLocalFrame* frame3_2 = frame3_1->NextSibling()->ToWebLocalFrame();
106 ASSERT_TRUE(frame3_2);
107 ASSERT_EQ("frame3_2", frame3_2->AssignedName());
Devlin Cronin3a7cad92020-07-30 19:43:58108 WebLocalFrame* frame3_3 = frame3_2->NextSibling()->ToWebLocalFrame();
109 ASSERT_TRUE(frame3_3);
110 ASSERT_EQ("frame3_3", frame3_3->AssignedName());
[email protected]ae26b282014-05-15 16:40:16111
112 // Top-level frame
Devlin Croninb8d8fee2020-07-28 19:03:38113 EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame));
114 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame));
[email protected]ae26b282014-05-15 16:40:16115 // top -> srcdoc = inherit
Devlin Croninb8d8fee2020-07-28 19:03:38116 EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame1));
117 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1));
[email protected]ae26b282014-05-15 16:40:16118 // top -> srcdoc -> about:blank = inherit
Devlin Croninb8d8fee2020-07-28 19:03:38119 EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame1_1));
120 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1_1));
121 // top -> srcdoc -> about:blank sandboxed = same URL when classifying
122 // contexts, but inherited url when injecting scripts.
123 EXPECT_EQ(blank_url, GetEffectiveDocumentURLForContext(frame1_2));
124 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1_2));
[email protected]ae26b282014-05-15 16:40:16125
Devlin Croninb8d8fee2020-07-28 19:03:38126 // top -> srcdoc [sandboxed] = same URL when classifying contexts,
127 // but inherited url when injecting scripts.
128 EXPECT_EQ(srcdoc_url, GetEffectiveDocumentURLForContext(frame2));
129 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame2));
130 // top -> srcdoc [sandboxed] -> about:blank = same URL when classifying
131 // contexts, but inherited url when injecting scripts.
132 EXPECT_EQ(blank_url, GetEffectiveDocumentURLForContext(frame2_1));
133 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame2_1));
[email protected]ae26b282014-05-15 16:40:16134
Devlin Cronin3a7cad92020-07-30 19:43:58135 // top -> data URL = same URL when classifying contexts, but inherited when
136 // injecting scripts.
137 EXPECT_EQ(data_url, GetEffectiveDocumentURLForContext(frame4));
138 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame4));
139 // Sanity-check: without matching data: URLs, the original URL should be
140 // returned.
141 EXPECT_EQ(data_url, GetEffectiveURLForInjectionWithoutMatchingData(frame4));
142
143 // top -> sandboxed data URL = same URL when classifying contexts, but
144 // inherited when injecting scripts.
145 EXPECT_EQ(data_url, GetEffectiveDocumentURLForContext(frame5));
146 EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame5));
147 // Sanity-check: without matching data: URLs, the original URL should be
148 // returned.
149 EXPECT_EQ(data_url, GetEffectiveURLForInjectionWithoutMatchingData(frame5));
150
[email protected]ae26b282014-05-15 16:40:16151 // top -> different origin = different origin
Devlin Croninb8d8fee2020-07-28 19:03:38152 EXPECT_EQ(different_url, GetEffectiveDocumentURLForContext(frame3));
153 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3));
154 // top -> different origin -> about:blank = inherit (from different origin)
155 EXPECT_EQ(different_url, GetEffectiveDocumentURLForContext(frame3_1));
156 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3_1));
157 // top -> different origin -> about:blank sandboxed = same URL when
158 // classifying contexts, but inherited (from different origin) url when
159 // injecting scripts.
160 EXPECT_EQ(blank_url, GetEffectiveDocumentURLForContext(frame3_2));
161 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3_2));
Devlin Cronin3a7cad92020-07-30 19:43:58162 // top -> different origin -> data URL = same URL when classifying contexts,
163 // but inherited (from different origin) url when injecting scripts.
164 EXPECT_EQ(data_url, GetEffectiveDocumentURLForContext(frame3_3));
165 EXPECT_EQ(different_url, GetEffectiveDocumentURLForInjection(frame3_3));
[email protected]ae26b282014-05-15 16:40:16166}
167
Jeremy Roman1a4d9b82017-12-08 01:46:40168TEST_F(ScriptContextTest, GetMainWorldContextForFrame) {
169 // ScriptContextSet::GetMainWorldContextForFrame should work, even without an
170 // existing v8::HandleScope.
171 content::RenderFrame* render_frame =
172 content::RenderFrame::FromWebFrame(GetMainFrame());
173 ScriptContext* script_context =
174 ScriptContextSet::GetMainWorldContextForFrame(render_frame);
175 ASSERT_TRUE(script_context);
176 EXPECT_EQ(render_frame, script_context->GetRenderFrame());
177}
178
[email protected]ae26b282014-05-15 16:40:16179} // namespace
180} // namespace extensions