Skip to content

Commit 867677c

Browse files
authored
Ensure Page.setBypassCSP works with iFrames (#1056)
* Ensure Page.setBypassCSP works with iFrames * Silence exception * ops * Codefactor
1 parent 079309b commit 867677c

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

lib/PuppeteerSharp.Tests/PageTests/SetBypassCSPTests.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,38 @@ await Page.AddScriptTagAsync(new AddTagOptions
6565
});
6666
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("window.__injected"));
6767

68-
await Page.GoToAsync(TestConstants.CrossProcessUrl+ "/csp.html");
68+
await Page.GoToAsync(TestConstants.CrossProcessUrl + "/csp.html");
6969
await Page.AddScriptTagAsync(new AddTagOptions
7070
{
7171
Content = "window.__injected = 42;"
7272
});
7373
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("window.__injected"));
7474
}
75+
76+
[Fact]
77+
public async Task ShouldBypassCSPInIframesAsWell()
78+
{
79+
await Page.GoToAsync(TestConstants.EmptyPage);
80+
81+
// Make sure CSP prohibits addScriptTag in an iframe.
82+
var frame = await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.ServerUrl + "/csp.html");
83+
await frame.AddScriptTagAsync(new AddTagOptions
84+
{
85+
Content = "window.__injected = 42;"
86+
}).ContinueWith(_ => Task.CompletedTask);
87+
Assert.Null(await frame.EvaluateFunctionAsync<int?>("() => window.__injected"));
88+
89+
// By-pass CSP and try one more time.
90+
await Page.SetBypassCSPAsync(true);
91+
await Page.ReloadAsync();
92+
93+
// Make sure CSP prohibits addScriptTag in an iframe.
94+
frame = await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.ServerUrl + "/csp.html");
95+
await frame.AddScriptTagAsync(new AddTagOptions
96+
{
97+
Content = "window.__injected = 42;"
98+
}).ContinueWith(_ => Task.CompletedTask);
99+
Assert.Equal(42, await frame.EvaluateFunctionAsync<int?>("() => window.__injected"));
100+
}
75101
}
76102
}

lib/PuppeteerSharp/DOMWorld.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ await EvaluateFunctionAsync(@"html => {
150150
await watcherTask.ConfigureAwait(false);
151151
}
152152

153-
internal async Task<ElementHandle> AddScriptTag(AddTagOptions options)
153+
internal async Task<ElementHandle> AddScriptTagAsync(AddTagOptions options)
154154
{
155155
const string addScriptUrl = @"async function addScriptUrl(url, type) {
156156
const script = document.createElement('script');
@@ -213,7 +213,7 @@ async Task<ElementHandle> AddScriptTagPrivate(string script, string urlOrContent
213213
throw new ArgumentException("Provide options with a `Url`, `Path` or `Content` property");
214214
}
215215

216-
internal async Task<ElementHandle> AddStyleTag(AddTagOptions options)
216+
internal async Task<ElementHandle> AddStyleTagAsync(AddTagOptions options)
217217
{
218218
const string addStyleUrl = @"async function addStyleUrl(url) {
219219
const link = document.createElement('link');

lib/PuppeteerSharp/Frame.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,17 +374,37 @@ public Task<JSHandle> WaitForExpressionAsync(string script, WaitForFunctionOptio
374374
/// <param name="options">add style tag options</param>
375375
/// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>
376376
/// <seealso cref="Page.AddStyleTagAsync(AddTagOptions)"/>
377-
/// <seealso cref="Page.AddStyleTagAsync(string)"/>
378-
public Task<ElementHandle> AddStyleTag(AddTagOptions options) => MainWorld.AddStyleTag(options);
377+
/// <seealso cref="Page.AddStyleTagAsync(string)"/>
378+
[Obsolete("Use AddStyleTagAsync instead")]
379+
public Task<ElementHandle> AddStyleTag(AddTagOptions options) => MainWorld.AddStyleTagAsync(options);
379380

380381
/// <summary>
381382
/// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
382383
/// </summary>
383384
/// <param name="options">add script tag options</param>
384385
/// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
385386
/// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
386-
/// <seealso cref="Page.AddScriptTagAsync(string)"/>
387-
public Task<ElementHandle> AddScriptTag(AddTagOptions options) => MainWorld.AddScriptTag(options);
387+
/// <seealso cref="Page.AddScriptTagAsync(string)"/>
388+
[Obsolete("Use AddScriptTagAsync instead")]
389+
public Task<ElementHandle> AddScriptTag(AddTagOptions options) => MainWorld.AddScriptTagAsync(options);
390+
391+
/// <summary>
392+
/// Adds a <c><![CDATA[<link rel="stylesheet">]]></c> tag into the page with the desired url or a <c><![CDATA[<link rel="stylesheet">]]></c> tag with the content
393+
/// </summary>
394+
/// <param name="options">add style tag options</param>
395+
/// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>
396+
/// <seealso cref="Page.AddStyleTagAsync(AddTagOptions)"/>
397+
/// <seealso cref="Page.AddStyleTagAsync(string)"/>
398+
public Task<ElementHandle> AddStyleTagAsync(AddTagOptions options) => MainWorld.AddStyleTagAsync(options);
399+
400+
/// <summary>
401+
/// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
402+
/// </summary>
403+
/// <param name="options">add script tag options</param>
404+
/// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
405+
/// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
406+
/// <seealso cref="Page.AddScriptTagAsync(string)"/>
407+
public Task<ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainWorld.AddScriptTagAsync(options);
388408

389409
/// <summary>
390410
/// Gets the full HTML contents of the page, including the doctype.

lib/PuppeteerSharp/Page.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,8 @@ public async Task DeleteCookieAsync(params CookieParam[] cookies)
601601
/// Shortcut for <c>page.MainFrame.AddScriptTagAsync(options)</c>
602602
/// </remarks>
603603
/// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
604-
/// <seealso cref="Frame.AddScriptTag(AddTagOptions)"/>
605-
public Task<ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainFrame.AddScriptTag(options);
604+
/// <seealso cref="Frame.AddScriptTagAsync(AddTagOptions)"/>
605+
public Task<ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainFrame.AddScriptTagAsync(options);
606606

607607
/// <summary>
608608
/// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
@@ -623,7 +623,7 @@ public async Task DeleteCookieAsync(params CookieParam[] cookies)
623623
/// </remarks>
624624
/// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>
625625
/// <seealso cref="Frame.AddStyleTag(AddTagOptions)"/>
626-
public Task<ElementHandle> AddStyleTagAsync(AddTagOptions options) => MainFrame.AddStyleTag(options);
626+
public Task<ElementHandle> AddStyleTagAsync(AddTagOptions options) => MainFrame.AddStyleTagAsync(options);
627627

628628
/// <summary>
629629
/// Adds a <c><![CDATA[<link rel="stylesheet">]]></c> tag into the page with the desired url or a <c><![CDATA[<link rel="stylesheet">]]></c> tag with the content

0 commit comments

Comments
 (0)