Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion skills/e2e-dotnet/references/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<link>` 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
Expand Down
5 changes: 3 additions & 2 deletions src/E2E.Mobile/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,16 @@ public Task<InstalledApp> InstallAppAsync(string? appPath = null, InstallAppOpti
/// foreground-only open.
/// </summary>
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));

/// <summary>
/// Opens a deep link (<c>myapp://orders/42</c>) or a web link (<c>https://...</c>) into <paramref name="app"/> (default:
/// the pinned app), which the session observes afterwards. iOS launches the app for a web link and then opens the URL.
/// 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 (<c>INVALID_ARGUMENT</c>
/// without). <c>file:</c>, <c>data:</c>, <c>javascript:</c>, <c>view-source:</c>, <c>blob:</c>, and <c>filesystem:</c>
/// links are <c>POLICY_DENIED</c>.
/// links are <c>POLICY_DENIED</c>. The report shows the link without its userinfo (<c>user:password@</c> or <c>token@</c>),
/// query, or fragment, where a login or a magic link's token rides. A string that does not parse shows as <c>&lt;link&gt;</c>, never as typed.
/// </summary>
public Task OpenLinkAsync(string url, string? app = null, CancellationToken cancellationToken = default) =>
RunAsync("device.openLink", DeviceLinks.LinkLabel(url), () => _session.OpenLinkAsync(DeviceLinks.LinkTarget(url), app, cancellationToken));
Expand Down
116 changes: 112 additions & 4 deletions src/E2E.Mobile/DeviceLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Modified by Dario Kondratiuk.
// SPDX-License-Identifier: Apache-2.0

using System.Globalization;
using System.Text.RegularExpressions;

namespace E2E.Mobile;
Expand All @@ -22,6 +23,11 @@ public static partial class DeviceLinks
/// <summary>The C0 controls and the space, which a browser trims off both ends of a URL.</summary>
private static readonly char[] EdgeCharacters = [.. Enumerable.Range(0, 0x21).Select(code => (char)code)];

/// <summary>What the report shows for a link that did not parse, or an app id that looks like a link.</summary>
private const string LinkPlaceholder = "<link>";

private static readonly Dictionary<string, int> DefaultPorts = new() { ["http"] = 80, ["https"] = 443, ["ftp"] = 21, ["ws"] = 80, ["wss"] = 443 };

private static readonly HashSet<string> ForbiddenProtocols = ["file:", "data:", "javascript:", "view-source:", "blob:", "filesystem:"];

/// <summary>
Expand All @@ -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();

/// <summary>Whether agent-device would open this string as a URL rather than resolve it as an app.</summary>
public static bool IsLink(string value) => LinkShape().IsMatch(value.Trim());

Expand Down Expand Up @@ -82,13 +91,112 @@ public static void AssertAppId(string app)
}

/// <summary>
/// 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 <c>user:password@</c>
/// login). A step label is kept for as long as the report is. A string that does not parse is never
/// echoed, as <see cref="LinkTarget"/> never echoes it, and the label is a placeholder.
/// </summary>
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);
}

/// <summary>
/// The label of an <c>openApp</c> step: the app id as it is. <see cref="AssertAppId"/> 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.
/// </summary>
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);
}

/// <summary>The host and port of an authority, without its userinfo; <c>null</c> when it has a host or port no URL parser accepts.</summary>
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)
Expand Down
5 changes: 3 additions & 2 deletions tests/E2E.Mobile.Tests/Engine/DeviceFixtureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,11 @@ async Task<string> LabelOf(Func<Task> 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("<link>", 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("<link>", 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")));
}
Expand Down
51 changes: 51 additions & 0 deletions tests/E2E.Mobile.Tests/Links/LinksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<link>", 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("<link>", DeviceLinks.AppLabel("https://user:pass@example.com/x?t=1"));
Assert.Equal("<link>", DeviceLinks.AppLabel("myapp://orders/42"));
}
}
Loading