|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using NUnit.Framework; |
| 5 | +using PuppeteerSharp.Nunit; |
| 6 | + |
| 7 | +namespace PuppeteerSharp.Tests.NetworkRestrictionTests; |
| 8 | + |
| 9 | +public class NetworkRestrictionsTests : PuppeteerBaseTest |
| 10 | +{ |
| 11 | + [Test, PuppeteerTest("network_restrictions.spec", "Network Restrictions", "should block page.goto when the destination is in the blocklist")] |
| 12 | + public async Task ShouldBlockPageGotoWhenDestinationIsInBlocklist() |
| 13 | + { |
| 14 | + var options = TestConstants.DefaultBrowserOptions(); |
| 15 | + options.BlockList = ["*://*:*/empty.html"]; |
| 16 | + |
| 17 | + await using var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory); |
| 18 | + await using var page = await browser.NewPageAsync(); |
| 19 | + |
| 20 | + var allowedUrl = TestConstants.ServerUrl + "/title.html"; |
| 21 | + var blockedUrl = TestConstants.ServerUrl + "/empty.html"; |
| 22 | + |
| 23 | + await page.GoToAsync(allowedUrl); |
| 24 | + |
| 25 | + Exception error = null; |
| 26 | + await page.GoToAsync(blockedUrl).ContinueWith(t => |
| 27 | + { |
| 28 | + if (t.IsFaulted) |
| 29 | + { |
| 30 | + error = t.Exception?.InnerException ?? t.Exception; |
| 31 | + } |
| 32 | + |
| 33 | + return t; |
| 34 | + }); |
| 35 | + |
| 36 | + Assert.That(error, Is.Not.Null); |
| 37 | + Assert.That(error.Message, Does.Contain("net::ERR_INTERNET_DISCONNECTED")); |
| 38 | + } |
| 39 | + |
| 40 | + [Test, PuppeteerTest("network_restrictions.spec", "Network Restrictions", "should block window.location.href navigation to URLs in the blocklist")] |
| 41 | + public async Task ShouldBlockWindowLocationHrefNavigationToUrlsInBlocklist() |
| 42 | + { |
| 43 | + var options = TestConstants.DefaultBrowserOptions(); |
| 44 | + options.BlockList = ["*://*:*/empty.html"]; |
| 45 | + |
| 46 | + await using var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory); |
| 47 | + await using var page = await browser.NewPageAsync(); |
| 48 | + |
| 49 | + var allowedUrl = TestConstants.ServerUrl + "/title.html"; |
| 50 | + var blockedUrl = TestConstants.ServerUrl + "/empty.html"; |
| 51 | + |
| 52 | + await page.GoToAsync(allowedUrl); |
| 53 | + |
| 54 | + var navTask = page.WaitForNavigationAsync(new NavigationOptions { Timeout = 2000 }).ContinueWith(t => t.IsFaulted ? null : t.Result); |
| 55 | + await page.EvaluateFunctionAsync("url => { window.location.href = url; }", blockedUrl); |
| 56 | + await navTask; |
| 57 | + |
| 58 | + var finalUrl = page.Url; |
| 59 | + Assert.That(finalUrl, Is.Not.EqualTo(blockedUrl)); |
| 60 | + } |
| 61 | + |
| 62 | + [Test, PuppeteerTest("network_restrictions.spec", "Network Restrictions", "should fail fetch requests to URLs in the blocklist")] |
| 63 | + public async Task ShouldFailFetchRequestsToUrlsInBlocklist() |
| 64 | + { |
| 65 | + var options = TestConstants.DefaultBrowserOptions(); |
| 66 | + options.BlockList = ["*://*:*/empty.html"]; |
| 67 | + |
| 68 | + await using var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory); |
| 69 | + await using var page = await browser.NewPageAsync(); |
| 70 | + |
| 71 | + var allowedUrl = TestConstants.ServerUrl + "/title.html"; |
| 72 | + var blockedUrl = TestConstants.ServerUrl + "/empty.html"; |
| 73 | + |
| 74 | + await page.GoToAsync(allowedUrl); |
| 75 | + |
| 76 | + var fetchError = await page.EvaluateFunctionAsync<string>( |
| 77 | + @"async (url) => { |
| 78 | + try { |
| 79 | + await fetch(url); |
| 80 | + return null; |
| 81 | + } catch (e) { |
| 82 | + return e.message; |
| 83 | + } |
| 84 | + }", |
| 85 | + blockedUrl); |
| 86 | + |
| 87 | + Assert.That(fetchError, Is.Not.Null.And.Not.Empty); |
| 88 | + Assert.That(fetchError, Does.Contain("Failed to fetch")); |
| 89 | + } |
| 90 | + |
| 91 | + [Test, PuppeteerTest("network_restrictions.spec", "Network Restrictions", "should prevent loading of blocklisted subresources (e.g., images)")] |
| 92 | + public async Task ShouldPreventLoadingOfBlocklistedSubresources() |
| 93 | + { |
| 94 | + var options = TestConstants.DefaultBrowserOptions(); |
| 95 | + options.BlockList = ["*://*:*/pptr.png"]; |
| 96 | + |
| 97 | + await using var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory); |
| 98 | + await using var page = await browser.NewPageAsync(); |
| 99 | + |
| 100 | + var allowedUrl = TestConstants.ServerUrl + "/one-style.css"; |
| 101 | + var blockedUrl = TestConstants.ServerUrl + "/pptr.png"; |
| 102 | + |
| 103 | + var failedRequests = new Dictionary<string, string>(); |
| 104 | + var finishedRequests = new HashSet<string>(); |
| 105 | + |
| 106 | + page.RequestFailed += (_, e) => |
| 107 | + { |
| 108 | + failedRequests[e.Request.Url] = e.Request.FailureText; |
| 109 | + }; |
| 110 | + page.RequestFinished += (_, e) => |
| 111 | + { |
| 112 | + finishedRequests.Add(e.Request.Url); |
| 113 | + }; |
| 114 | + |
| 115 | + await page.GoToAsync(TestConstants.EmptyPage); |
| 116 | + |
| 117 | + await page.SetContentAsync( |
| 118 | + $@"<img src=""{blockedUrl}"" /> |
| 119 | + <link rel=""stylesheet"" href=""{allowedUrl}"" />", |
| 120 | + new NavigationOptions { WaitUntil = [WaitUntilNavigation.Networkidle0] }); |
| 121 | + |
| 122 | + Assert.That(failedRequests.ContainsKey(blockedUrl), Is.True); |
| 123 | + Assert.That(failedRequests[blockedUrl], Does.Contain("net::ERR_INTERNET_DISCONNECTED")); |
| 124 | + Assert.That(finishedRequests.Contains(allowedUrl), Is.True); |
| 125 | + } |
| 126 | + |
| 127 | + [Test, PuppeteerTest("network_restrictions.spec", "Network Restrictions", "should detach from targets violating blocklist when connecting to a running browser")] |
| 128 | + public async Task ShouldDetachFromTargetsViolatingBlocklistWhenConnectingToRunningBrowser() |
| 129 | + { |
| 130 | + await using var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.LoggerFactory); |
| 131 | + var blockedUrl = TestConstants.ServerUrl + "/empty.html"; |
| 132 | + |
| 133 | + var page = await originalBrowser.NewPageAsync(); |
| 134 | + await page.GoToAsync(blockedUrl); |
| 135 | + |
| 136 | + var wsEndpoint = originalBrowser.WebSocketEndpoint; |
| 137 | + |
| 138 | + IBrowser connectedBrowser = null; |
| 139 | + try |
| 140 | + { |
| 141 | + connectedBrowser = await Puppeteer.ConnectAsync(new ConnectOptions |
| 142 | + { |
| 143 | + BrowserWSEndpoint = wsEndpoint, |
| 144 | + BlockList = ["*://*:*/empty.html"], |
| 145 | + }); |
| 146 | + |
| 147 | + var targets = connectedBrowser.Targets(); |
| 148 | + var blockedTarget = Array.Find(targets, t => t.Url == blockedUrl); |
| 149 | + |
| 150 | + Assert.That(blockedTarget, Is.Null); |
| 151 | + } |
| 152 | + finally |
| 153 | + { |
| 154 | + connectedBrowser?.Disconnect(); |
| 155 | + await page.CloseAsync(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + [Test, PuppeteerTest("network_restrictions.spec", "Network Restrictions", "should not block chrome://version/ even if it matches blocklist")] |
| 160 | + public async Task ShouldNotBlockChromeVersionEvenIfItMatchesBlocklist() |
| 161 | + { |
| 162 | + const string chromeUrl = "chrome://version/"; |
| 163 | + var options = TestConstants.DefaultBrowserOptions(); |
| 164 | + options.BlockList = [chromeUrl]; |
| 165 | + |
| 166 | + await using var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory); |
| 167 | + await using var page = await browser.NewPageAsync(); |
| 168 | + |
| 169 | + await page.GoToAsync(chromeUrl); |
| 170 | + |
| 171 | + // Navigation should succeed as chrome:// URLs usually bypass the network |
| 172 | + Assert.That(page.Url, Is.EqualTo(chromeUrl)); |
| 173 | + } |
| 174 | +} |
0 commit comments