diff --git a/DECISIONS.md b/DECISIONS.md index 5ebdc50..f9a10e9 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -545,3 +545,12 @@ published by hand by the owner. skipped, so it must refuse them; a journey without blocks still prints version 1 byte for byte as before. - **Not exported to agent-browser**, which runs every line. +## Read-only runs + +- **The guard enforces it, a journey doesn't declare it.** `NetworkGuardOptions.ReadOnly` refuses on the request's + method in `Fetch.requestPaused`, where the page cannot get around it; a journey marked as read-only could still + write by mistake. The TS runner does the same in `page.route`. +- **Writes are judged after the allow list**, so a refused third-party beacon stays a reported request and only a + write to an allowed host fails the step. A page that writes on its own (telemetry) needs `WritableUrls`. +- **`BlockedRequest.RefusedWrite` is `null` for allow-list refusals**, so existing `BlockedRequest` equality is + unchanged. diff --git a/docs/cli/index.md b/docs/cli/index.md index e979a31..6e18866 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -33,6 +33,8 @@ cdp run site/journeys/*.journey --base-url ./site --artifacts .artifacts/journey | `-a, --allow` | another host pattern the run may reach (repeatable) | | `--feature` | a feature flag that is on (repeatable), for `feature` blocks | | `--build-type`, `--env` | the build type and the environment's name, for `build-type:` and `env:` conditions | +| `--read-only` | refuse every request that is not GET, HEAD or OPTIONS, so the run cannot change a live site; see [safety](../journeys/safety.md#read-only-runs) | +| `--writable` | a URL glob a read-only run may write to anyway (repeatable), such as a telemetry endpoint | | `--file-root` | the folder `file://` URLs may point into, when the base URL is not a folder | | `--artifacts` | the folder `screenshot` and `network har stop` write into; off unless given | | `--timeout` | seconds an action waits for its element (default 5) | diff --git a/docs/journeys/safety.md b/docs/journeys/safety.md index 4aa597b..6bccfc3 100644 --- a/docs/journeys/safety.md +++ b/docs/journeys/safety.md @@ -20,6 +20,22 @@ pixel does not break a journey. `JourneyOptions.GuardNetwork = false` turns the list off, and lets `open` go anywhere. +## Read-only runs + +A run against a live site should not change it. With `JourneyOptions.ReadOnly` (the CLI's `--read-only`, `readOnly` in +`@nullean/journeys`) every request whose method is not `GET`, `HEAD` or `OPTIONS` is refused before it leaves the +browser: form posts, `fetch`, `XMLHttpRequest` and `sendBeacon` alike. The step that caused it fails: + +```text +Blocked POST https://shop.test/api/basket: this run is read-only, so only GET, HEAD and OPTIONS requests are sent +(JourneyOptions.WritableUrls, or --writable on the CLI, allows a URL). +``` + +A page often writes on its own, for telemetry. `WritableUrls` (`--writable`, `writableUrls`) lists URL globs a +read-only run may write to anyway: `**` is anything, `*` anything but `/`, as in `wait --url`. Requests the allow list +already refuses stay refused and are reported, not failed, as before. A write is judged only after the allow list lets +it through, so a third-party beacon is a refused request and not a failed step. + ## Files `file://` URLs stay inside one folder: the base URL's folder, or `JourneyOptions.FileRoot` (the CLI's `--file-root`). diff --git a/js/journeys/src/run.ts b/js/journeys/src/run.ts index 70cf6ba..135ad3e 100644 --- a/js/journeys/src/run.ts +++ b/js/journeys/src/run.ts @@ -9,7 +9,7 @@ import type { BrowserContext, Locator, Page } from 'playwright-core'; import { joinLine } from './commandline.ts'; import type { Command, Journey, Step, Target } from './model.ts'; import { isSecretReference, requirementMet } from './parse.ts'; -import { describeTarget, runExpect } from './expect.ts'; +import { describeTarget, runExpect, urlGlob } from './expect.ts'; import type { PlaywrightExpect } from './expect.ts'; export interface RunOptions { @@ -23,6 +23,13 @@ export interface RunOptions { buildType?: string; /** The environment's name (`prod`, `edge`), for `env:` conditions. */ env?: string; + /** + * Refuse every request that is not GET, HEAD or OPTIONS, so a run against a live site cannot change it. A refused + * write fails the step that caused it. + */ + readOnly?: boolean; + /** URL globs (`**` anything, `*` anything but `/`) a read-only run may write to anyway, such as a telemetry endpoint. */ + writableUrls?: string[]; /** Resolves `$env:NAME`; the process environment by default. */ secrets?: (name: string) => string | undefined; /** Where `screenshot` writes. Without it, screenshot steps are skipped with a warning. */ @@ -160,6 +167,8 @@ export class JourneyRun { private actionDocuments = 0; private readonly blockedNavigations: string[] = []; private readonly skippedBlocks = new Set(); + private readonly blockedWrites: string[] = []; + private readonly writable: RegExp[]; private readonly consoleLog: string[] = []; private readonly errorLog: string[] = []; private readonly warned = new Set(); @@ -184,6 +193,7 @@ export class JourneyRun { else if (base.protocol === 'file:') fileRoot = dirname(decodeURIComponent(base.pathname.endsWith('/') ? base.pathname + 'x' : base.pathname)); } this.allow = new AllowList(hosts, fileRoot); + this.writable = (options.writableUrls ?? []).map(urlGlob); } /** Starts a run: the network guard, and the recordings the journey's steps need. */ @@ -214,7 +224,13 @@ export class JourneyRun { } catch { return route.fallback(); } - if (url.protocol === 'data:' || url.protocol === 'blob:' || url.protocol === 'about:' || this.allow.refusal(url) === undefined) return route.fallback(); + if (url.protocol === 'data:' || url.protocol === 'blob:' || url.protocol === 'about:') return route.fallback(); + if (this.allow.refusal(url) === undefined) { + const method = request.method().toUpperCase(); + if (!this.options.readOnly || method === 'GET' || method === 'HEAD' || method === 'OPTIONS' || this.writable.some((g) => g.test(url.href))) return route.fallback(); + this.blockedWrites.push(`Blocked ${method} ${request.url()}: this run is read-only, so only GET, HEAD and OPTIONS requests are sent (RunOptions.writableUrls, or --writable on the CLI, allows a URL).`); + return route.abort('blockedbyclient'); + } if (request.isNavigationRequest()) this.blockedNavigations.push(`The page navigated to ${request.url()}, which is refused: ${this.allow.refusal(url)}.`); return route.abort('blockedbyclient'); }; @@ -270,10 +286,10 @@ export class JourneyRun { output = await this.run(step.command, step); // A navigation starts from the page it lands on. if (step.command.type === 'open' || step.command.type === 'history') this.actionDocuments = this.documents; - const refused = this.blockedNavigations.splice(0); + const refused = [...this.blockedNavigations.splice(0), ...this.blockedWrites.splice(0)]; if (refused.length > 0) throw new StepError(refused[0]); } catch (e) { - const refused = this.blockedNavigations.splice(0); + const refused = [...this.blockedNavigations.splice(0), ...this.blockedWrites.splice(0)]; const message = refused[0] ?? (e instanceof Error ? e.message : String(e)); throw new StepError(this.secrets.redact(message)); } diff --git a/js/journeys/test/readonly.test.ts b/js/journeys/test/readonly.test.ts new file mode 100644 index 0000000..aa9746d --- /dev/null +++ b/js/journeys/test/readonly.test.ts @@ -0,0 +1,86 @@ +// A read-only run refuses every write before it leaves the browser, and fails the step that made it. Skips when +// Chrome is missing; CDP_BROWSER_REQUIRED=1 makes that a failure. + +import assert from 'node:assert/strict'; +import { createServer } from 'node:http'; +import type { AddressInfo } from 'node:net'; +import { after, before, test } from 'node:test'; +import type { Browser } from 'playwright-core'; +import { parseJourney, runJourney } from '../src/index.ts'; +import { launchChrome } from './site.ts'; + +const page = `Writes + + +
+Next page`; + +let browser: Browser | undefined; +let skip: string | false = false; +let origin = ''; +let writes = 0; +let stop = () => {}; + +before(async () => { + const server = createServer((req, res) => { + if (req.method !== 'GET') writes++; + const next = req.url === '/next'; + res.writeHead(200, { 'content-type': 'text/html' }).end(next ? 'Next

Next

' : req.method === 'GET' ? page : 'Saved'); + }); + await new Promise((done) => server.listen(0, '127.0.0.1', done)); + origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; + stop = () => server.close(); + ({ browser, skip } = await launchChrome()); +}); + +after(async () => { + await browser?.close(); + stop(); +}); + +async function run(text: string, options: { readOnly?: boolean; writableUrls?: string[] } = { readOnly: true }) { + writes = 0; + const p = await (await browser!.newContext()).newPage(); + const result = await runJourney(p, parseJourney(text, 'read-only.journey'), { baseUrl: origin + '/', actionTimeout: 3000, warn: () => {}, ...options }); + await new Promise((r) => setTimeout(r, 100)); + return result; +} + +test('a fetch POST fails the step and never reaches the server', async (t) => { + if (skip) return t.skip(skip); + const result = await run('open /\nfind role button click --name "Save with fetch"\nwait 300\nget title'); + assert.ok(result.failure, 'the run should fail'); + assert.match(result.failure.message, /^Blocked POST http:\/\/127\.0\.0\.1:\d+\/write: this run is read-only/); + assert.equal(writes, 0); +}); + +test('a form POST fails the step and never reaches the server', async (t) => { + if (skip) return t.skip(skip); + const result = await run('open /\nfind role button click --name "Save with form"\nget title'); + assert.match(result.failure?.message ?? '', /^Blocked POST .*\/form: this run is read-only/); + assert.equal(writes, 0); +}); + +test('reads still work', async (t) => { + if (skip) return t.skip(skip); + const result = await run('open /\nfind role link click --name "Next page"\nwait --url **/next\nget title'); + assert.equal(result.failure, undefined); + assert.equal(writes, 0); +}); + +test('a writable URL lets its write through, and no other', async (t) => { + if (skip) return t.skip(skip); + const ok = await run('open /\nfind role button click --name "Send telemetry"\nwait 300\nget title', { readOnly: true, writableUrls: ['**/telemetry'] }); + assert.equal(ok.failure, undefined); + assert.equal(writes, 1); + const refused = await run('open /\nfind role button click --name "Save with fetch"\nwait 300\nget title', { readOnly: true, writableUrls: ['**/telemetry'] }); + assert.ok(refused.failure); + assert.equal(writes, 0); +}); + +test('without readOnly the write goes through', async (t) => { + if (skip) return t.skip(skip); + const result = await run('open /\nfind role button click --name "Save with fetch"\nwait 300\nget title', {}); + assert.equal(result.failure, undefined); + assert.equal(writes, 1); +}); diff --git a/src/Nullean.Cdp.Cli/CdpCli.cs b/src/Nullean.Cdp.Cli/CdpCli.cs index 9c96c44..a0b798e 100644 --- a/src/Nullean.Cdp.Cli/CdpCli.cs +++ b/src/Nullean.Cdp.Cli/CdpCli.cs @@ -95,7 +95,9 @@ public async Task RunAsync( string? artifacts = null, IReadOnlyList? features = null, string? buildType = null, - string? env = null + string? env = null, + bool readOnly = false, + IReadOnlyList? writable = null ) { var (ok, root) = ParseBase(baseUrl); @@ -114,6 +116,8 @@ public async Task RunAsync( Features = new HashSet(features ?? [], StringComparer.Ordinal), BuildType = buildType, Env = env, + ReadOnly = readOnly, + WritableUrls = writable ?? [], FileRoot = fileRoot is null ? null : Path.GetFullPath(fileRoot), ArtifactsDirectory = artifacts is null ? null : Path.GetFullPath(artifacts), Secrets = environment, diff --git a/src/Nullean.Cdp.Cli/Program.cs b/src/Nullean.Cdp.Cli/Program.cs index b72b9a2..124cec4 100644 --- a/src/Nullean.Cdp.Cli/Program.cs +++ b/src/Nullean.Cdp.Cli/Program.cs @@ -25,6 +25,8 @@ internal sealed class CdpCommands /// A feature flag that is on (repeatable), for 'feature' blocks. /// The build type, for build-type: conditions. /// The environment's name, for env: conditions. + /// Refuse every request that is not GET, HEAD or OPTIONS, so the run cannot change a live site. + /// URL glob a read-only run may write to anyway (repeatable): ** anything, * anything but /. /// Show the browser window. /// Print JSON; everything under a "page" key came from the page. /// Characters per checkpoint before it is cut (0 for no limit). @@ -39,12 +41,14 @@ public static Task Run( string[]? feature = null, string? buildType = null, string? env = null, + bool readOnly = false, + string[]? writable = null, bool headed = false, bool json = false, int maxOutput = CdpCli.DefaultMaxOutput, double? timeout = null, CancellationToken cancellationToken = default - ) => Cli().RunAsync(files, baseUrl, allow ?? [], fileRoot, headed, json, maxOutput, timeout, cancellationToken, artifacts, feature, buildType, env); + ) => Cli().RunAsync(files, baseUrl, allow ?? [], fileRoot, headed, json, maxOutput, timeout, cancellationToken, artifacts, feature, buildType, env, readOnly, writable); /// Parse journeys without running them, and report every error with its line. /// Journey files. diff --git a/src/Nullean.Cdp.Journeys/JourneyRunner.cs b/src/Nullean.Cdp.Journeys/JourneyRunner.cs index 0f866a5..a54027e 100644 --- a/src/Nullean.Cdp.Journeys/JourneyRunner.cs +++ b/src/Nullean.Cdp.Journeys/JourneyRunner.cs @@ -57,6 +57,24 @@ public sealed class JourneyOptions /// What feature blocks and @requires conditions are judged against. public JourneyContext Context => new(Features, BuildType, Env); + /// + /// Refuse every request that is not a read (anything but GET, HEAD and OPTIONS), so a run against a live site cannot + /// change it. A refused write fails the step that caused it. Not tied to . + /// + public bool ReadOnly { get; init; } + + /// + /// URL globs (: ** anything, * anything but /) a read-only run may write + /// to anyway, such as a telemetry endpoint. + /// + public IReadOnlyList WritableUrls { get; init; } = []; + + internal NetworkGuardOptions GuardOptions() + { + var writable = WritableUrls.Select(Page.UrlGlob).ToArray(); + return new NetworkGuardOptions { ReadOnly = ReadOnly, AllowWrites = writable.Length == 0 ? null : uri => writable.Any(g => g.IsMatch(uri.AbsoluteUri)) }; + } + internal JourneyAllowList AllowList(Journey journey) { var hosts = journey.AllowedHosts.Concat(AllowedHosts); @@ -125,8 +143,8 @@ public static async Task RunAsync(Journey journey, Page page, Jou var checkpoints = new List(); var blocked = new List(); // Without the guard, a guard that allows everything is still started when a step needs one (credentials). - await using var guard = options.GuardNetwork || journey.Steps.Any(s => s.Command is SetCommand { Setting: "credentials" }) - ? await page.GuardNetworkAsync(options.GuardNetwork ? run.AllowList.Allows : _ => true, cancellationToken).ConfigureAwait(false) + await using var guard = options.GuardNetwork || options.ReadOnly || journey.Steps.Any(s => s.Command is SetCommand { Setting: "credentials" }) + ? await page.GuardNetworkAsync(options.GuardNetwork ? run.AllowList.Allows : _ => true, options.GuardOptions(), cancellationToken).ConfigureAwait(false) : null; run.Guard = guard; // The network is recorded only for journeys that read it. @@ -204,6 +222,12 @@ private sealed record Run(Journey Journey, Page Page, JourneyOptions Options, Jo string? failure = null; foreach (var request in guard.TakeBlocked()) { + if (request.RefusedWrite is { } method) + { + failure ??= $"Blocked {method} {request.Url}: this run is read-only, so only GET, HEAD and OPTIONS requests are sent (JourneyOptions.WritableUrls, or --writable on the CLI, allows a URL)."; + continue; + } + if (!request.IsNavigation) { blocked.Add(request); diff --git a/src/Nullean.Cdp/NetworkGuard.cs b/src/Nullean.Cdp/NetworkGuard.cs index 460c0b4..452eda2 100644 --- a/src/Nullean.Cdp/NetworkGuard.cs +++ b/src/Nullean.Cdp/NetworkGuard.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Text.Json; using Nullean.Cdp.Protocol; namespace Nullean.Cdp; @@ -9,10 +10,30 @@ namespace Nullean.Cdp; /// Whether it was a request of a page's top frame (the page or a popup), not of a frame in it. public sealed record BlockedRequest(string Url, string ResourceType, bool TopLevel) { + /// + /// The HTTP method of a request refused only because the guard is read-only (); + /// null for a request refused by the allow predicate. + /// + public string? RefusedWrite { get; init; } + /// Whether the request was a page loading a new document: the page or a popup navigating. public bool IsNavigation => TopLevel && ResourceType == "Document"; } +/// How a treats requests that change state. +public sealed class NetworkGuardOptions +{ + /// + /// Refuse every request whose method is not GET, HEAD or OPTIONS, form posts and + /// fetch or sendBeacon calls included, unless allows its URL. Requests the + /// allow predicate refuses are refused either way. + /// + public bool ReadOnly { get; init; } + + /// URLs a read-only guard lets write to, such as a telemetry endpoint; null for none. + public Func? AllowWrites { get; init; } +} + /// /// Holds every request of one page until a predicate allows it, through the protocol's Fetch domain: /// navigations, frames and sub-resources, file:// included. Popups (attached paused by the browser) and frames @@ -24,6 +45,7 @@ public sealed class NetworkGuard : IAsyncDisposable { private readonly Page _page; private readonly Func _allow; + private readonly NetworkGuardOptions _options; private readonly ConcurrentQueue _blocked = new(); // Every guarded session and the target (top frame) it belongs to; its listener, to stop on dispose. private readonly ConcurrentDictionary _sessions = new(StringComparer.Ordinal); @@ -41,15 +63,16 @@ public void UseCredentials(string user, string password) _credentials = (user, password); } - private NetworkGuard(Page page, Func allow) + private NetworkGuard(Page page, Func allow, NetworkGuardOptions options) { _page = page; _allow = allow; + _options = options; } - internal static async Task StartAsync(Page page, Func allow, CancellationToken cancellationToken) + internal static async Task StartAsync(Page page, Func allow, NetworkGuardOptions options, CancellationToken cancellationToken) { - var guard = new NetworkGuard(page, allow); + var guard = new NetworkGuard(page, allow, options); guard.Watch(page.SessionId, page.TargetId); page.Browser.OnPopup(page.TargetId, guard.GuardPopupAsync); await GuardSessionAsync(page.Connection, page.SessionId, cancellationToken).ConfigureAwait(false); @@ -129,8 +152,15 @@ private void Decide(string sessionId, string targetId, string requestId, CdpEven { var url = e.Params.TryGetProperty("request", out var request) ? request.StringOrNull("url") ?? string.Empty : string.Empty; var allowed = Uri.TryCreate(url, UriKind.Absolute, out var uri) && (uri.Scheme is "data" or "blob" or "about" || _allow(uri)); + var resourceType = e.Params.StringOrNull("resourceType") ?? string.Empty; + var topLevel = e.Params.StringOrNull("frameId") == targetId; if (!allowed) - _blocked.Enqueue(new BlockedRequest(url, e.Params.StringOrNull("resourceType") ?? string.Empty, e.Params.StringOrNull("frameId") == targetId)); + _blocked.Enqueue(new BlockedRequest(url, resourceType, topLevel)); + else if (request.ValueKind == JsonValueKind.Object && RefusedWrite(request, uri!) is { } method) + { + allowed = false; + _blocked.Enqueue(new BlockedRequest(url, resourceType, topLevel) { RefusedWrite = method }); + } // The handler runs on the receive loop and must not wait for the reply; failures only mean the target is gone. Observe(allowed @@ -142,6 +172,15 @@ private void Decide(string sessionId, string targetId, string requestId, CdpEven }, sessionId)); } + // The method of a request a read-only guard refuses, or null. + private string? RefusedWrite(JsonElement request, Uri uri) + { + if (!_options.ReadOnly || uri.Scheme is "data" or "blob" or "about") + return null; + var method = request.StringOrNull("method")?.ToUpperInvariant() ?? "GET"; + return method is "GET" or "HEAD" or "OPTIONS" || _options.AllowWrites?.Invoke(uri) == true ? null : method; + } + private void Authenticate(string sessionId, string requestId, CdpEvent e) { var url = e.Params.TryGetProperty("request", out var request) ? request.StringOrNull("url") ?? string.Empty : string.Empty; diff --git a/src/Nullean.Cdp/Page.cs b/src/Nullean.Cdp/Page.cs index 676aa56..e21607d 100644 --- a/src/Nullean.Cdp/Page.cs +++ b/src/Nullean.Cdp/Page.cs @@ -136,10 +136,18 @@ public Locator Locate(LocatorKind kind, string value, string? name = null, bool /// /// Refuses every request of this page that rejects, from now until the guard is disposed. /// - public Task GuardNetworkAsync(Func allow, CancellationToken cancellationToken = default) + public Task GuardNetworkAsync(Func allow, CancellationToken cancellationToken = default) => + GuardNetworkAsync(allow, new NetworkGuardOptions(), cancellationToken); + + /// + /// Refuses every request of this page that rejects and, with + /// , every request that is not a read, from now until the guard is disposed. + /// + public Task GuardNetworkAsync(Func allow, NetworkGuardOptions options, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(allow); - return NetworkGuard.StartAsync(this, allow, cancellationToken); + ArgumentNullException.ThrowIfNull(options); + return NetworkGuard.StartAsync(this, allow, options, cancellationToken); } /// Presses a key or chord on whatever has focus, then waits for any navigation it starts. diff --git a/tests/Nullean.Cdp.Tests/Live/ReadOnlyTests.cs b/tests/Nullean.Cdp.Tests/Live/ReadOnlyTests.cs new file mode 100644 index 0000000..7d407e0 --- /dev/null +++ b/tests/Nullean.Cdp.Tests/Live/ReadOnlyTests.cs @@ -0,0 +1,80 @@ +using AwesomeAssertions; +using Nullean.Cdp.Journeys; + +namespace Nullean.Cdp.Tests.Live; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class ReadOnlyTests(ChromeFixture chrome) +{ + private async Task<(JourneyResult Result, int Writes)> Run(string journey, bool readOnly = true, params string[] writable) + { + await using var server = new WriteServer(); + var result = await JourneyRunner.RunAsync(Journey.Parse(journey, "read-only.journey"), chrome.Browser, new JourneyOptions + { + BaseUrl = new Uri(server.Url), + ReadOnly = readOnly, + WritableUrls = writable.Select(w => server.Url + w).ToArray(), + ActionTimeout = TimeSpan.FromSeconds(3), + }); + await Task.Delay(100); + return (result, server.Writes); + } + + [Test] + public async Task AFetchPostFailsTheStepAndNeverReachesTheServer() + { + var (result, writes) = await Run("open /\nfind role button click --name \"Save with fetch\"\nwait 300\nget title"); + + result.Failure.Should().NotBeNull(); + result.Failure!.Line.Should().BeOneOf(2, 3); + result.Failure.Message.Should().StartWith("Blocked POST http://localhost:").And.EndWith("/write: this run is read-only, so only GET, HEAD and OPTIONS requests are sent (JourneyOptions.WritableUrls, or --writable on the CLI, allows a URL)."); + writes.Should().Be(0); + } + + [Test] + public async Task AFormPostFailsTheStepAndNeverReachesTheServer() + { + var (result, writes) = await Run("open /\nfind role button click --name \"Save with form\"\nget title"); + + result.Failure.Should().NotBeNull(); + result.Failure!.Message.Should().StartWith("Blocked POST http://localhost:").And.Contain("/form: this run is read-only"); + writes.Should().Be(0); + } + + [Test] + public async Task ReadsStillWork() + { + var (result, writes) = await Run("open /\nfind role link click --name \"Next page\"\nwait --url **/next\nget title"); + + result.Failure.Should().BeNull(); + result.Checkpoints.Single().Output.Should().Be("Next"); + writes.Should().Be(0); + } + + [Test] + public async Task AWritableUrlLetsItThrough() + { + var (result, writes) = await Run("open /\nfind role button click --name \"Send telemetry\"\nwait 300\nget title", true, "telemetry"); + + result.Failure.Should().BeNull(); + writes.Should().Be(1); + } + + [Test] + public async Task WritableUrlsDoNotLetOtherWritesThrough() + { + var (result, writes) = await Run("open /\nfind role button click --name \"Save with fetch\"\nwait 300\nget title", true, "telemetry"); + + result.Failure.Should().NotBeNull(); + writes.Should().Be(0); + } + + [Test] + public async Task WithoutReadOnlyTheWriteGoesThrough() + { + var (result, writes) = await Run("open /\nfind role button click --name \"Save with fetch\"\nwait 300\nget title", readOnly: false); + + result.Failure.Should().BeNull(); + writes.Should().Be(1); + } +} diff --git a/tests/Nullean.Cdp.Tests/Live/WriteServer.cs b/tests/Nullean.Cdp.Tests/Live/WriteServer.cs new file mode 100644 index 0000000..ec6cb29 --- /dev/null +++ b/tests/Nullean.Cdp.Tests/Live/WriteServer.cs @@ -0,0 +1,81 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Nullean.Cdp.Tests.Live; + +/// +/// A local HTTP server with a page that can write three ways (a fetch, a form post, a beacon) and counts what reached +/// it. file:// pages cannot show a refused POST: nothing answers it either way. +/// +internal sealed class WriteServer : IAsyncDisposable +{ + private const string Page = """ + Writes + + +
+ Next page + """; + + private readonly HttpListener _listener = new(); + private readonly Task _serving; + private int _writes; + + public WriteServer() + { + Url = $"http://localhost:{FreePort()}/"; + _listener.Prefixes.Add(Url); + _listener.Start(); + _serving = Task.Run(ServeAsync); + } + + public string Url { get; } + + /// The requests that reached the server with a method other than GET. + public int Writes => Volatile.Read(ref _writes); + + private async Task ServeAsync() + { + while (_listener.IsListening) + { + HttpListenerContext context; + try + { + context = await _listener.GetContextAsync(); + } + catch (Exception e) when (e is HttpListenerException or ObjectDisposedException or InvalidOperationException or ArgumentException) + { + return; + } + + var response = context.Response; + try + { + if (context.Request.HttpMethod != "GET") + Interlocked.Increment(ref _writes); + response.ContentType = "text/html; charset=utf-8"; + var title = context.Request.Url!.AbsolutePath == "/next" ? "Next

Next

" : context.Request.HttpMethod == "GET" ? Page : "Saved

Saved

"; + await response.OutputStream.WriteAsync(Encoding.UTF8.GetBytes(title)); + response.Close(); + } + catch (Exception e) when (e is HttpListenerException or ObjectDisposedException or IOException) + { + } + } + } + + private static int FreePort() + { + using var probe = new TcpListener(IPAddress.Loopback, 0); + probe.Start(); + return ((IPEndPoint)probe.LocalEndpoint).Port; + } + + public async ValueTask DisposeAsync() + { + _listener.Stop(); + _listener.Close(); + await _serving; + } +}