[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 1 | // 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.vg | d155cb80 | 2014-12-22 14:37:10 | [diff] [blame] | 5 | #include "chrome/test/base/chrome_render_view_test.h" |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 6 | #include "content/public/renderer/render_frame.h" |
| 7 | #include "content/public/test/frame_load_waiter.h" |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 8 | #include "extensions/renderer/script_context.h" |
Jeremy Roman | 1a4d9b8 | 2017-12-08 01:46:40 | [diff] [blame] | 9 | #include "extensions/renderer/script_context_set.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 10 | #include "third_party/blink/public/web/web_document.h" |
| 11 | #include "third_party/blink/public/web/web_local_frame.h" |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 12 | #include "url/gurl.h" |
| 13 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 14 | using blink::WebLocalFrame; |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 15 | |
| 16 | namespace extensions { |
| 17 | namespace { |
| 18 | |
vivek.vg | d155cb80 | 2014-12-22 14:37:10 | [diff] [blame] | 19 | class ScriptContextTest : public ChromeRenderViewTest { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 20 | protected: |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 21 | GURL GetEffectiveDocumentURLForContext(WebLocalFrame* frame) { |
| 22 | return ScriptContext::GetEffectiveDocumentURLForContext( |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 23 | frame, frame->GetDocument().Url(), /*match_about_blank=*/true); |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 24 | } |
| 25 | GURL GetEffectiveDocumentURLForInjection(WebLocalFrame* frame) { |
| 26 | return ScriptContext::GetEffectiveDocumentURLForInjection( |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 27 | 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 34 | } |
| 35 | }; |
| 36 | |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 37 | TEST_F(ScriptContextTest, GetEffectiveDocumentURL) { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 38 | 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 Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 42 | GURL data_url("data:text/html,<html>Hi</html>"); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 43 | |
| 44 | const char frame_html[] = |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 45 | 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 Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 52 | <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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 57 | |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 58 | const char frame3_html[] = |
| 59 | R"(<iframe name='frame3_1'></iframe> |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 60 | <iframe name='frame3_2' sandbox=''></iframe> |
| 61 | <iframe name='frame3_3' |
| 62 | src="data:text/html,<html>Hi</html>"></iframe>)"; |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 63 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 64 | WebLocalFrame* frame = GetMainFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 65 | ASSERT_TRUE(frame); |
| 66 | |
Dmitry Gozman | d96e493a8 | 2018-11-28 01:13:33 | [diff] [blame] | 67 | content::RenderFrame::FromWebFrame(frame)->LoadHTMLString( |
| 68 | frame_html, top_url, "UTF-8", GURL(), false /* replace_current_item */); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 69 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 70 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 71 | WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 72 | ASSERT_TRUE(frame1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 73 | ASSERT_EQ("frame1", frame1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 74 | WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 75 | ASSERT_TRUE(frame1_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 76 | ASSERT_EQ("frame1_1", frame1_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 77 | WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 78 | ASSERT_TRUE(frame1_2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 79 | ASSERT_EQ("frame1_2", frame1_2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 80 | WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 81 | ASSERT_TRUE(frame2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 82 | ASSERT_EQ("frame2", frame2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 83 | WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 84 | ASSERT_TRUE(frame2_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 85 | ASSERT_EQ("frame2_1", frame2_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 86 | WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 87 | ASSERT_TRUE(frame3); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 88 | ASSERT_EQ("frame3", frame3->AssignedName()); |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 89 | 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 95 | |
| 96 | // Load a blank document in a frame from a different origin. |
Dmitry Gozman | d96e493a8 | 2018-11-28 01:13:33 | [diff] [blame] | 97 | content::RenderFrame::FromWebFrame(frame3)->LoadHTMLString( |
| 98 | frame3_html, different_url, "UTF-8", GURL(), |
| 99 | false /* replace_current_item */); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 100 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 101 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 102 | WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 103 | ASSERT_TRUE(frame3_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 104 | ASSERT_EQ("frame3_1", frame3_1->AssignedName()); |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 105 | WebLocalFrame* frame3_2 = frame3_1->NextSibling()->ToWebLocalFrame(); |
| 106 | ASSERT_TRUE(frame3_2); |
| 107 | ASSERT_EQ("frame3_2", frame3_2->AssignedName()); |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 108 | WebLocalFrame* frame3_3 = frame3_2->NextSibling()->ToWebLocalFrame(); |
| 109 | ASSERT_TRUE(frame3_3); |
| 110 | ASSERT_EQ("frame3_3", frame3_3->AssignedName()); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 111 | |
| 112 | // Top-level frame |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 113 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame)); |
| 114 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame)); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 115 | // top -> srcdoc = inherit |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 116 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame1)); |
| 117 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1)); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 118 | // top -> srcdoc -> about:blank = inherit |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 119 | 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 125 | |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 126 | // 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 134 | |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 135 | // 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 151 | // top -> different origin = different origin |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 152 | 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 Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame^] | 162 | // 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 166 | } |
| 167 | |
Jeremy Roman | 1a4d9b8 | 2017-12-08 01:46:40 | [diff] [blame] | 168 | TEST_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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 179 | } // namespace |
| 180 | } // namespace extensions |