[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" |
Devlin Cronin | 6d96d3d | 2020-08-11 19:46:27 | [diff] [blame] | 8 | #include "extensions/common/script_constants.h" |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 9 | #include "extensions/renderer/script_context.h" |
Jeremy Roman | 1a4d9b8 | 2017-12-08 01:46:40 | [diff] [blame] | 10 | #include "extensions/renderer/script_context_set.h" |
Dave Tapuska | b9cb253 | 2020-09-18 20:15:07 | [diff] [blame] | 11 | #include "third_party/blink/public/platform/web_runtime_features.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 12 | #include "third_party/blink/public/web/web_document.h" |
| 13 | #include "third_party/blink/public/web/web_local_frame.h" |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 14 | #include "url/gurl.h" |
| 15 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 16 | using blink::WebLocalFrame; |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 17 | |
| 18 | namespace extensions { |
| 19 | namespace { |
| 20 | |
vivek.vg | d155cb80 | 2014-12-22 14:37:10 | [diff] [blame] | 21 | class ScriptContextTest : public ChromeRenderViewTest { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 22 | protected: |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 23 | GURL GetEffectiveDocumentURLForContext(WebLocalFrame* frame) { |
| 24 | return ScriptContext::GetEffectiveDocumentURLForContext( |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 25 | frame, frame->GetDocument().Url(), /*match_about_blank=*/true); |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 26 | } |
Devlin Cronin | 6d96d3d | 2020-08-11 19:46:27 | [diff] [blame] | 27 | GURL GetEffectiveDocumentURLForInjection( |
| 28 | WebLocalFrame* frame, |
| 29 | MatchOriginAsFallbackBehavior match_origin_as_fallback = |
| 30 | MatchOriginAsFallbackBehavior::kAlways) { |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 31 | return ScriptContext::GetEffectiveDocumentURLForInjection( |
Devlin Cronin | 6d96d3d | 2020-08-11 19:46:27 | [diff] [blame] | 32 | frame, frame->GetDocument().Url(), match_origin_as_fallback); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 33 | } |
| 34 | }; |
| 35 | |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 36 | TEST_F(ScriptContextTest, GetEffectiveDocumentURL) { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 37 | 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 Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 41 | GURL data_url("data:text/html,<html>Hi</html>"); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 42 | |
| 43 | const char frame_html[] = |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 44 | 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 Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 51 | <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 Tapuska | b9cb253 | 2020-09-18 20:15:07 | [diff] [blame] | 55 | sandbox=''></iframe> |
| 56 | <iframe disallowdocumentaccess name='frame6'></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); |
Dave Tapuska | b9cb253 | 2020-09-18 20:15:07 | [diff] [blame] | 66 | blink::WebRuntimeFeatures::EnableDisallowDocumentAccess(true); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 67 | |
Dmitry Gozman | d96e493a8 | 2018-11-28 01:13:33 | [diff] [blame] | 68 | content::RenderFrame::FromWebFrame(frame)->LoadHTMLString( |
| 69 | frame_html, top_url, "UTF-8", GURL(), false /* replace_current_item */); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 70 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 71 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 72 | WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 73 | ASSERT_TRUE(frame1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 74 | ASSERT_EQ("frame1", frame1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 75 | WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 76 | ASSERT_TRUE(frame1_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 77 | ASSERT_EQ("frame1_1", frame1_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 78 | WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 79 | ASSERT_TRUE(frame1_2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 80 | ASSERT_EQ("frame1_2", frame1_2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 81 | WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 82 | ASSERT_TRUE(frame2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 83 | ASSERT_EQ("frame2", frame2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 84 | WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 85 | ASSERT_TRUE(frame2_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 86 | ASSERT_EQ("frame2_1", frame2_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 87 | WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 88 | ASSERT_TRUE(frame3); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 89 | ASSERT_EQ("frame3", frame3->AssignedName()); |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 90 | 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 Tapuska | b9cb253 | 2020-09-18 20:15:07 | [diff] [blame] | 96 | WebLocalFrame* frame6 = frame5->NextSibling()->ToWebLocalFrame(); |
| 97 | ASSERT_TRUE(frame6); |
| 98 | ASSERT_EQ("frame6", frame6->AssignedName()); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 99 | |
| 100 | // Load a blank document in a frame from a different origin. |
Dmitry Gozman | d96e493a8 | 2018-11-28 01:13:33 | [diff] [blame] | 101 | content::RenderFrame::FromWebFrame(frame3)->LoadHTMLString( |
| 102 | frame3_html, different_url, "UTF-8", GURL(), |
| 103 | false /* replace_current_item */); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 104 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 105 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 106 | WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 107 | ASSERT_TRUE(frame3_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 108 | ASSERT_EQ("frame3_1", frame3_1->AssignedName()); |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 109 | WebLocalFrame* frame3_2 = frame3_1->NextSibling()->ToWebLocalFrame(); |
| 110 | ASSERT_TRUE(frame3_2); |
| 111 | ASSERT_EQ("frame3_2", frame3_2->AssignedName()); |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 112 | WebLocalFrame* frame3_3 = frame3_2->NextSibling()->ToWebLocalFrame(); |
| 113 | ASSERT_TRUE(frame3_3); |
| 114 | ASSERT_EQ("frame3_3", frame3_3->AssignedName()); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 115 | |
| 116 | // Top-level frame |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 117 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame)); |
| 118 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame)); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 119 | // top -> srcdoc = inherit |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 120 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForContext(frame1)); |
| 121 | EXPECT_EQ(top_url, GetEffectiveDocumentURLForInjection(frame1)); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 122 | // top -> srcdoc -> about:blank = inherit |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 123 | 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 129 | |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 130 | // 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 138 | |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 139 | // 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 Cronin | 6d96d3d | 2020-08-11 19:46:27 | [diff] [blame] | 143 | // Sanity-check: if we only match about: schemes, the original URL should be |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 144 | // returned. |
Devlin Cronin | 6d96d3d | 2020-08-11 19:46:27 | [diff] [blame] | 145 | EXPECT_EQ( |
| 146 | data_url, |
| 147 | GetEffectiveDocumentURLForInjection( |
| 148 | frame4, |
| 149 | MatchOriginAsFallbackBehavior::kMatchForAboutSchemeAndClimbTree)); |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 150 | |
| 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 Cronin | 6d96d3d | 2020-08-11 19:46:27 | [diff] [blame] | 155 | // Sanity-check: if we only match about: schemes, the original URL should be |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 156 | // returned. |
Devlin Cronin | 6d96d3d | 2020-08-11 19:46:27 | [diff] [blame] | 157 | EXPECT_EQ( |
| 158 | data_url, |
| 159 | GetEffectiveDocumentURLForInjection( |
| 160 | frame5, |
| 161 | MatchOriginAsFallbackBehavior::kMatchForAboutSchemeAndClimbTree)); |
Devlin Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 162 | |
Dave Tapuska | b9cb253 | 2020-09-18 20:15:07 | [diff] [blame] | 163 | // 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 168 | // top -> different origin = different origin |
Devlin Cronin | b8d8fee | 2020-07-28 19:03:38 | [diff] [blame] | 169 | 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 Cronin | 3a7cad9 | 2020-07-30 19:43:58 | [diff] [blame] | 179 | // 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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 183 | } |
| 184 | |
Jeremy Roman | 1a4d9b8 | 2017-12-08 01:46:40 | [diff] [blame] | 185 | TEST_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] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 196 | } // namespace |
| 197 | } // namespace extensions |