diff --git a/skills/e2e-dotnet/references/debugging.md b/skills/e2e-dotnet/references/debugging.md index 2d4053099..cbeff1c4a 100644 --- a/skills/e2e-dotnet/references/debugging.md +++ b/skills/e2e-dotnet/references/debugging.md @@ -103,7 +103,7 @@ no viewer. It tells, in this order: an xUnit `InitializeAsync` or `DisposeAsync`), and the offset into the video (`, video 0:12`) when one is recorded. A `Browser` or `Device` call is a step too, named for the call (`browser.goto` with the URL, `browser.route` - with the pattern, `device.openLink` with the link before its query, + with the pattern, `device.openLink` with the link without its userinfo, query, or fragment (`` when it does not parse), `device.setNetwork` with `offline`), so a failure there points at the call, and `browser.waitForURL` and `locator.waitFor` count as a verification. `ClickAsync` and `DblClickAsync` are `locator.click` and `locator.dblclick` steps, not diff --git a/src/E2E.Mobile/Device.cs b/src/E2E.Mobile/Device.cs index d8c532887..f8dcbba53 100644 --- a/src/E2E.Mobile/Device.cs +++ b/src/E2E.Mobile/Device.cs @@ -308,7 +308,7 @@ public Task InstallAppAsync(string? appPath = null, InstallAppOpti /// foreground-only open. /// public Task OpenAppAsync(string app, OpenAppOptions? options = null, CancellationToken cancellationToken = default) => - RunAsync("device.openApp", DeviceLinks.LinkLabel(app), () => _session.OpenAppAsync(app, options, cancellationToken)); + RunAsync("device.openApp", DeviceLinks.AppLabel(app), () => _session.OpenAppAsync(app, options, cancellationToken)); /// /// Opens a deep link (myapp://orders/42) or a web link (https://...) into (default: @@ -316,7 +316,8 @@ public Task OpenAppAsync(string app, OpenAppOptions? options = null, Cancellatio /// A web link the app does not claim goes to Safari, and the next observation brings the app back. Android starts the /// link on that package. Without an app, Android lets the OS route the link. iOS needs one (INVALID_ARGUMENT /// without). file:, data:, javascript:, view-source:, blob:, and filesystem: - /// links are POLICY_DENIED. + /// links are POLICY_DENIED. The report shows the link without its userinfo (user:password@ or token@), + /// query, or fragment, where a login or a magic link's token rides. A string that does not parse shows as <link>, never as typed. /// public Task OpenLinkAsync(string url, string? app = null, CancellationToken cancellationToken = default) => RunAsync("device.openLink", DeviceLinks.LinkLabel(url), () => _session.OpenLinkAsync(DeviceLinks.LinkTarget(url), app, cancellationToken)); diff --git a/src/E2E.Mobile/DeviceLinks.cs b/src/E2E.Mobile/DeviceLinks.cs index b0f6e215b..31bf5bc6b 100644 --- a/src/E2E.Mobile/DeviceLinks.cs +++ b/src/E2E.Mobile/DeviceLinks.cs @@ -2,6 +2,7 @@ // Modified by Dario Kondratiuk. // SPDX-License-Identifier: Apache-2.0 +using System.Globalization; using System.Text.RegularExpressions; namespace E2E.Mobile; @@ -22,6 +23,11 @@ public static partial class DeviceLinks /// The C0 controls and the space, which a browser trims off both ends of a URL. private static readonly char[] EdgeCharacters = [.. Enumerable.Range(0, 0x21).Select(code => (char)code)]; + /// What the report shows for a link that did not parse, or an app id that looks like a link. + private const string LinkPlaceholder = ""; + + private static readonly Dictionary DefaultPorts = new() { ["http"] = 80, ["https"] = 443, ["ftp"] = 21, ["ws"] = 80, ["wss"] = 443 }; + private static readonly HashSet ForbiddenProtocols = ["file:", "data:", "javascript:", "view-source:", "blob:", "filesystem:"]; /// @@ -35,6 +41,9 @@ public static partial class DeviceLinks [GeneratedRegex(@"^[A-Za-z][A-Za-z0-9+.-]*:")] private static partial Regex SchemePrefix(); + [GeneratedRegex(@"^([A-Za-z][A-Za-z0-9+.-]*):(.*)\z", RegexOptions.Singleline)] + private static partial Regex SchemeSplit(); + /// Whether agent-device would open this string as a URL rather than resolve it as an app. public static bool IsLink(string value) => LinkShape().IsMatch(value.Trim()); @@ -82,13 +91,112 @@ public static void AssertAppId(string app) } /// - /// The link as the report shows it, cut before its query and fragment. A magic link carries its - /// one-time token there, and a step label is kept for as long as the report is. + /// The link as the report shows it: read as a URL parser reads it, then without its userinfo, query, or + /// fragment, any of which may carry a credential (a magic link's one-time token, a user:password@ + /// login). A step label is kept for as long as the report is. A string that does not parse is never + /// echoed, as never echoes it, and the label is a placeholder. /// public static string LinkLabel(string input) { - var cut = input.AsSpan().IndexOfAny('?', '#'); - return cut < 0 ? input : input[..cut]; + // As a URL parser reads it: tabs and line breaks go anywhere, and edge whitespace and control characters go. + var text = new string(input.Where(character => character is not ('\t' or '\n' or '\r')).ToArray()).Trim(EdgeCharacters); + var scheme = SchemeSplit().Match(text); + if (!scheme.Success) + { + return LinkPlaceholder; + } + + var name = scheme.Groups[1].Value.ToLowerInvariant(); + var rest = scheme.Groups[2].Value; + if (!DefaultPorts.TryGetValue(name, out var defaultPort) && name != "file") + { + return NonSpecialLabel(name, rest); + } + + // A special scheme reads a backslash as a slash and takes any number of slashes before the authority. + rest = rest.Replace('\\', '/'); + var start = 0; + while (start < rest.Length && rest[start] == '/') + { + start++; + } + + rest = rest[start..]; + var end = rest.AsSpan().IndexOfAny('/', '?', '#'); + var authority = end < 0 ? rest : rest[..end]; + var path = end < 0 ? string.Empty : rest[end..]; + var host = Authority(authority, out var port, lowercase: true); + if (host is null || (host.Length == 0 && name != "file")) + { + return LinkPlaceholder; + } + + var portText = port is null || port == defaultPort ? string.Empty : ":" + port.Value.ToString(CultureInfo.InvariantCulture); + return name + "://" + host + portText + PathLabel(path, special: true); + } + + /// + /// The label of an openApp step: the app id as it is. refuses a link, + /// but the step is labeled before it runs, so a string shaped like a link gets the placeholder instead + /// of whatever userinfo it carries. + /// + public static string AppLabel(string app) => IsLink(app) ? LinkPlaceholder : app; + + private static string NonSpecialLabel(string scheme, string rest) + { + if (!rest.StartsWith("//", StringComparison.Ordinal)) + { + // No authority: the path (or an opaque path) is all there is, and an @ in it is no userinfo. + return scheme + ":" + PathLabel(rest, special: false); + } + + rest = rest[2..]; + var end = rest.AsSpan().IndexOfAny('/', '?', '#'); + var authority = end < 0 ? rest : rest[..end]; + var path = end < 0 ? string.Empty : rest[end..]; + var host = Authority(authority, out var port, lowercase: false); + if (host is null) + { + return LinkPlaceholder; + } + + return scheme + "://" + host + (port is null ? string.Empty : ":" + port.Value.ToString(CultureInfo.InvariantCulture)) + PathLabel(path, special: false); + } + + /// The host and port of an authority, without its userinfo; null when it has a host or port no URL parser accepts. + private static string? Authority(string authority, out int? port, bool lowercase) + { + port = null; + var host = authority[(authority.LastIndexOf('@') + 1)..]; + var colon = host.StartsWith('[') ? host.IndexOf("]:", StringComparison.Ordinal) + 1 : host.LastIndexOf(':'); + if (colon > 0) + { + var digits = host[(colon + 1)..]; + host = host[..colon]; + if (digits.Length > 0) + { + if (!digits.All(char.IsAsciiDigit) || !int.TryParse(digits, NumberStyles.None, CultureInfo.InvariantCulture, out var number) || number > 65535) + { + return null; + } + + port = number; + } + } + + if (host.Any(character => character <= ' ' || character is '#' or '/' or '<' or '>' or '?' or '@' or '\\' or '^' or '|' or '%' or '\u007f')) + { + return null; + } + + return lowercase ? host.ToLowerInvariant() : host; + } + + private static string PathLabel(string rest, bool special) + { + var cut = rest.AsSpan().IndexOfAny('?', '#'); + var path = (cut < 0 ? rest : rest[..cut]).Replace(" ", "%20", StringComparison.Ordinal); + return special && path.Length == 0 ? "/" : path; } private static void DenyForbiddenScheme(string protocol) diff --git a/tests/E2E.Mobile.Tests/Engine/DeviceFixtureTests.cs b/tests/E2E.Mobile.Tests/Engine/DeviceFixtureTests.cs index 8f48f475a..f789a939e 100644 --- a/tests/E2E.Mobile.Tests/Engine/DeviceFixtureTests.cs +++ b/tests/E2E.Mobile.Tests/Engine/DeviceFixtureTests.cs @@ -667,10 +667,11 @@ async Task LabelOf(Func call, string? refusedAs = null) return screen.Steps!.All()[^1].Label; } - Assert.Equal("https://app.example.com/magic", await LabelOf(() => device.OpenAppAsync("https://app.example.com/magic?token=s3cret#frag"), "INVALID_ARGUMENT")); + Assert.Equal("", await LabelOf(() => device.OpenAppAsync("https://user:pw@app.example.com/magic?token=s3cret#frag"), "INVALID_ARGUMENT")); Assert.Equal("com.apple.Preferences", await LabelOf(() => device.OpenAppAsync("com.apple.Preferences"))); Assert.Equal("myapp://orders/42", await LabelOf(() => device.OpenLinkAsync("myapp://orders/42?ref=mail"))); - Assert.Equal("verify", await LabelOf(() => device.OpenLinkAsync("verify?token=s3cret"), "INVALID_ARGUMENT")); + Assert.Equal("https://app.example.com/magic", await LabelOf(() => device.OpenLinkAsync("https://user:pw@app.example.com/magic?token=s3cret#frag"))); + Assert.Equal("", await LabelOf(() => device.OpenLinkAsync("verify?token=s3cret"), "INVALID_ARGUMENT")); // Clipboard text is often a code or token the test copied, never a configured secret the ledger would redact. Assert.Equal("6 chars", await LabelOf(() => device.SetClipboardAsync("482913"))); } diff --git a/tests/E2E.Mobile.Tests/Links/LinksTests.cs b/tests/E2E.Mobile.Tests/Links/LinksTests.cs index 462401c87..e19910ac1 100644 --- a/tests/E2E.Mobile.Tests/Links/LinksTests.cs +++ b/tests/E2E.Mobile.Tests/Links/LinksTests.cs @@ -78,4 +78,55 @@ public void Refuses_a_malformed_link_without_echoing_it_wherever_a_token_may_sit Assert.DoesNotContain("s3cret", refused.Message, StringComparison.Ordinal); } } + + [Fact] + public void Labels_a_link_without_its_userinfo_query_or_fragment() + { + Assert.Equal("myapp://orders/42", DeviceLinks.LinkLabel("myapp://user:secret@orders/42?code=1")); + Assert.Equal("https://example.com/verify", DeviceLinks.LinkLabel("https://user:pass@example.com/verify")); + Assert.Equal("https://example.com/verify", DeviceLinks.LinkLabel("https:/user:pass@example.com/verify")); + Assert.Equal("https://example.com/path", DeviceLinks.LinkLabel("https:user:pass@example.com/path")); + Assert.Equal("https://example.com/verify", DeviceLinks.LinkLabel("HTTPS:\\\\user:pass@example.com\\verify")); + Assert.Equal("https://example.com/x", DeviceLinks.LinkLabel("https://a@b:c@example.com/x?token=s3cret#frag")); + Assert.Equal("myapp://orders/42", DeviceLinks.LinkLabel("myapp://orders/42#user:secret@x")); + Assert.Equal("https://example.com/x", DeviceLinks.LinkLabel("ht\ttps://user:pass@example.com/x")); + Assert.Equal("https://example.com/x", DeviceLinks.LinkLabel("https:/\t/user:pass@example.com/x")); + Assert.Equal("ftp://example.com/x", DeviceLinks.LinkLabel("ftp://user:pass@example.com/x")); + Assert.Equal("wss://example.com/x", DeviceLinks.LinkLabel("wss://user:pass@example.com/x")); + } + + [Fact] + public void Keeps_an_at_sign_outside_the_authority() + { + Assert.Equal("https://example.com/users/@me", DeviceLinks.LinkLabel("https://example.com/users/@me")); + Assert.Equal("https://example.com/invite", DeviceLinks.LinkLabel("https://example.com/invite?email=a@b.com")); + Assert.Equal("mailto:someone@example.com", DeviceLinks.LinkLabel("mailto:someone@example.com")); + Assert.Equal("myapp:/invite@friend", DeviceLinks.LinkLabel("myapp:/invite@friend")); + } + + [Fact] + public void Never_echoes_a_link_that_does_not_parse() + { + string[] malformed = + [ + "https://user:p/w@host/x", + "https://user:p?w@host/x", + "https://user:s3cret@exa mple.com/verify", + "not-a-url?token=s3cret", + "://user@x", + ]; + foreach (var link in malformed) + { + Assert.Equal("", DeviceLinks.LinkLabel(link)); + } + } + + [Fact] + public void Labels_an_app_id_as_it_is_and_a_link_shaped_one_with_the_placeholder() + { + Assert.Equal("com.example.app", DeviceLinks.AppLabel("com.example.app")); + Assert.Equal("Notes: Pro", DeviceLinks.AppLabel("Notes: Pro")); + Assert.Equal("", DeviceLinks.AppLabel("https://user:pass@example.com/x?t=1")); + Assert.Equal("", DeviceLinks.AppLabel("myapp://orders/42")); + } }