using Febe.Browser.Cdp; using Febe.Browser.Targets; namespace Febe.Tests.Browser; [TestClass] public sealed class CdpPolicyTests { [TestMethod] public void AllowlistIsExactlyTheTableOfSection61() { string[] expected = [ "Page.enable", "Page.navigate", "Page.getFrameTree", "Runtime.evaluate", "Network.enable", "Network.getResponseBody", "Input.dispatchMouseEvent", "Input.dispatchKeyEvent", "Network.loadNetworkResource", "IO.read", "IO.close", "Page.startScreencast", "Page.screencastFrameAck", "Page.stopScreencast", "Page.captureScreenshot", "Network.getCookies", ]; CollectionAssert.AreEquivalent(expected, CdpCommandPolicy.Allowed.ToArray()); foreach (var method in expected) { CdpCommandPolicy.EnsureAllowed(method); } } [TestMethod] [DataRow("Network.setUserAgentOverride")] [DataRow("Network.setExtraHTTPHeaders")] [DataRow("Page.addScriptToEvaluateOnNewDocument")] [DataRow("Emulation.setTimezoneOverride")] [DataRow("Emulation.setLocaleOverride")] [DataRow("Emulation.setUserAgentOverride")] [DataRow("Fetch.enable")] [DataRow("Fetch.continueRequest")] public void ForbiddenCommandsAreRefusedExplicitly(string method) { Assert.IsTrue(CdpCommandPolicy.IsExplicitlyForbidden(method)); var ex = Assert.ThrowsExactly(() => CdpCommandPolicy.EnsureAllowed(method)); Assert.AreEqual(method, ex.Method); StringAssert.Contains(ex.Message, "vietato"); } [TestMethod] [DataRow("Target.createTarget")] [DataRow("Page.reload")] [DataRow("Input.insertText")] [DataRow("Runtime.callFunctionOn")] [DataRow("Storage.getCookies")] [DataRow("Browser.close")] public void CommandsOutsideTheAllowlistAreRefused(string method) { var ex = Assert.ThrowsExactly(() => CdpCommandPolicy.EnsureAllowed(method)); StringAssert.Contains(ex.Message, "non รจ nell'elenco"); } [TestMethod] [DataRow("document.readyState")] [DataRow("navigator.webdriver")] [DataRow("({ w: window.innerWidth, h: window.innerHeight })")] [DataRow("document.getElementById('campo').value === 'x'")] [DataRow("location.href")] [DataRow("Intl.DateTimeFormat().resolvedOptions().timeZone")] public void ReadOnlyExpressionsPass(string expression) => ReadOnlyScriptGuard.EnsureReadOnly(expression); [TestMethod] [DataRow("document.body.click()")] [DataRow("window.scrollBy(0, 400)")] [DataRow("window.scrollTo(0, 0)")] [DataRow("el.scrollIntoView()")] [DataRow("input.focus()")] [DataRow("el.dispatchEvent(new MouseEvent('click'))")] [DataRow("fetch('/api/v1/feed')")] [DataRow("new XMLHttpRequest()")] [DataRow("navigator.sendBeacon('/x')")] [DataRow("document.cookie")] [DataRow("localStorage.getItem('x')")] [DataRow("el.value = 'x'")] [DataRow("el.innerHTML += 'x'")] [DataRow("location.href = '/'")] [DataRow("location = '/'")] [DataRow("history.pushState({}, '', '/x')")] [DataRow("Object.defineProperty(navigator, 'webdriver', { get: () => false })")] [DataRow("navigator.webdriver = false")] [DataRow("el.remove()")] [DataRow("document.body.appendChild(x)")] [DataRow("el.setAttribute('a', 'b')")] [DataRow("window.open('/x')")] public void MutatingExpressionsAreRefused(string expression) { Assert.IsFalse(ReadOnlyScriptGuard.IsReadOnly(expression)); Assert.ThrowsExactly(() => ReadOnlyScriptGuard.EnsureReadOnly(expression)); } [TestMethod] public void TheTargetScriptPassesTheGuard() { var spec = new TargetSpec("x", ["button", "link"], "^avanti$", "/{username}/", 0, null); var expression = TargetResolver.BuildExpression(spec, new Dictionary { ["username"] = "alice" }); ReadOnlyScriptGuard.EnsureReadOnly(expression); } }