Skip to content

Commit

Permalink
Ensure Page.setBypassCSP works with iFrames (#1056)
Browse files Browse the repository at this point in the history
* Ensure Page.setBypassCSP works with iFrames

* Silence exception

* ops

* Codefactor
  • Loading branch information
kblok authored Apr 5, 2019
1 parent 079309b commit 867677c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
28 changes: 27 additions & 1 deletion lib/PuppeteerSharp.Tests/PageTests/SetBypassCSPTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,38 @@ await Page.AddScriptTagAsync(new AddTagOptions
});
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("window.__injected"));

await Page.GoToAsync(TestConstants.CrossProcessUrl+ "/csp.html");
await Page.GoToAsync(TestConstants.CrossProcessUrl + "/csp.html");
await Page.AddScriptTagAsync(new AddTagOptions
{
Content = "window.__injected = 42;"
});
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("window.__injected"));
}

[Fact]
public async Task ShouldBypassCSPInIframesAsWell()
{
await Page.GoToAsync(TestConstants.EmptyPage);

// Make sure CSP prohibits addScriptTag in an iframe.
var frame = await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.ServerUrl + "/csp.html");
await frame.AddScriptTagAsync(new AddTagOptions
{
Content = "window.__injected = 42;"
}).ContinueWith(_ => Task.CompletedTask);
Assert.Null(await frame.EvaluateFunctionAsync<int?>("() => window.__injected"));

// By-pass CSP and try one more time.
await Page.SetBypassCSPAsync(true);
await Page.ReloadAsync();

// Make sure CSP prohibits addScriptTag in an iframe.
frame = await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.ServerUrl + "/csp.html");
await frame.AddScriptTagAsync(new AddTagOptions
{
Content = "window.__injected = 42;"
}).ContinueWith(_ => Task.CompletedTask);
Assert.Equal(42, await frame.EvaluateFunctionAsync<int?>("() => window.__injected"));
}
}
}
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/DOMWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ await EvaluateFunctionAsync(@"html => {
await watcherTask.ConfigureAwait(false);
}

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

internal async Task<ElementHandle> AddStyleTag(AddTagOptions options)
internal async Task<ElementHandle> AddStyleTagAsync(AddTagOptions options)
{
const string addStyleUrl = @"async function addStyleUrl(url) {
const link = document.createElement('link');
Expand Down
28 changes: 24 additions & 4 deletions lib/PuppeteerSharp/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,37 @@ public Task<JSHandle> WaitForExpressionAsync(string script, WaitForFunctionOptio
/// <param name="options">add style tag options</param>
/// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>
/// <seealso cref="Page.AddStyleTagAsync(AddTagOptions)"/>
/// <seealso cref="Page.AddStyleTagAsync(string)"/>
public Task<ElementHandle> AddStyleTag(AddTagOptions options) => MainWorld.AddStyleTag(options);
/// <seealso cref="Page.AddStyleTagAsync(string)"/>
[Obsolete("Use AddStyleTagAsync instead")]
public Task<ElementHandle> AddStyleTag(AddTagOptions options) => MainWorld.AddStyleTagAsync(options);

/// <summary>
/// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
/// </summary>
/// <param name="options">add script tag options</param>
/// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
/// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
/// <seealso cref="Page.AddScriptTagAsync(string)"/>
public Task<ElementHandle> AddScriptTag(AddTagOptions options) => MainWorld.AddScriptTag(options);
/// <seealso cref="Page.AddScriptTagAsync(string)"/>
[Obsolete("Use AddScriptTagAsync instead")]
public Task<ElementHandle> AddScriptTag(AddTagOptions options) => MainWorld.AddScriptTagAsync(options);

/// <summary>
/// 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
/// </summary>
/// <param name="options">add style tag options</param>
/// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>
/// <seealso cref="Page.AddStyleTagAsync(AddTagOptions)"/>
/// <seealso cref="Page.AddStyleTagAsync(string)"/>
public Task<ElementHandle> AddStyleTagAsync(AddTagOptions options) => MainWorld.AddStyleTagAsync(options);

/// <summary>
/// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
/// </summary>
/// <param name="options">add script tag options</param>
/// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
/// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
/// <seealso cref="Page.AddScriptTagAsync(string)"/>
public Task<ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainWorld.AddScriptTagAsync(options);

/// <summary>
/// Gets the full HTML contents of the page, including the doctype.
Expand Down
6 changes: 3 additions & 3 deletions lib/PuppeteerSharp/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ public async Task DeleteCookieAsync(params CookieParam[] cookies)
/// Shortcut for <c>page.MainFrame.AddScriptTagAsync(options)</c>
/// </remarks>
/// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
/// <seealso cref="Frame.AddScriptTag(AddTagOptions)"/>
public Task<ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainFrame.AddScriptTag(options);
/// <seealso cref="Frame.AddScriptTagAsync(AddTagOptions)"/>
public Task<ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainFrame.AddScriptTagAsync(options);

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

/// <summary>
/// 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
Expand Down

0 comments on commit 867677c

Please sign in to comment.