puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

Headers propagation issue for set content

Open crow-ua opened this issue 1 year ago • 2 comments

Description

I have a page content that has a link to the publicly available image and also one protected with authorization. To get past authorization I'm setting extra http auth headers. They work for browsing (GoToAsync), but when I use SetContentAsync, the protected image is not shown.

Complete minimal example reproducing the issue

// Arrange
const string inputFile = "test.html"; // html string has one 'img src' accessible without auth, and one protected with auth
const string outputFile = "screenshot.jpg";
const string authHeader = "Authorization";
const string authHeaderValue = "Basic abc12345==";
var html = File.ReadAllText(inputFile);
using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.SetExtraHttpHeadersAsync(new Dictionary<string, string> { { authHeader, authHeaderValue } });
await page.SetContentAsync(html);

// Act
await page.ScreenshotAsync(outputFile);

// Assert
// open screenshot.jpg, note first image is shown, but the second image does not

Expected behavior:

Second image that is protected with auth is also shown.

Actual behavior:

Only publicly accessible image is shown.

Versions

  • Which version of PuppeteerSharp are you using? 7.1.0
  • Which .NET runtime and version are you targeting? .NET 6.0

Additional information

The underlying html content is retrieved by an API call and represents only one section on the real site, so GoToAsync cannot be used in my case.

crow-ua avatar Jul 12 '22 12:07 crow-ua

Do you browse the image using GoToAsync or a page?

kblok avatar Jul 12 '22 13:07 kblok

Do you browse the image using GoToAsync or a page?

I don't explicitly browse the page because page content is retrieved via API call as embedded resource in the previous step, I think it's done implicitly when ScreenshotAsync is called ("public" image is there on the screenshot). I'm looking for something like --custom-header-propagation from wkhtmltopdf.

crow-ua avatar Jul 13 '22 08:07 crow-ua

It's working for me @crow-ua image

kblok avatar Oct 14 '22 13:10 kblok