diff --git a/AGENTS.md b/AGENTS.md index 10df90c..55652a8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -99,9 +99,10 @@ Avalonia 12. Use the `avalonia-docs` MCP before reaching for an API from memory. ## Security The number arrives from a `tel:` link, which any web page or document can produce: it is -**hostile input**. It reaches the phone only after `TelUri` has reduced it to `0-9 * #` and a +**hostile input**. It reaches the phone only after `TelUri` has reduced it to digits and a length cap — never a `;`, `\r`, `\n` or letter, which would let a link append its own AT -commands. A security fix gets a test that replays the attack. Dialling always goes through the +commands, and never `*` or `#`, which on a landline would let it set up call forwarding +(`*21*#`) at the exchange. A security fix gets a test that replays the attack. Dialling always goes through the confirm window; nothing dials silently. ## Tests diff --git a/src/Waehlhilfe.Domain/DialNumber.cs b/src/Waehlhilfe.Domain/DialNumber.cs new file mode 100644 index 0000000..5f4f3ea --- /dev/null +++ b/src/Waehlhilfe.Domain/DialNumber.cs @@ -0,0 +1,38 @@ +namespace Waehlhilfe.Domain; + +/// The exact string sent after ATD: digits only. +public sealed record DialNumber +{ + private DialNumber(string digits) => Digits = digits; + + public string Digits { get; } + + /// The settings carry an invalid country code or prefix. + public static DialNumber From(TelUri uri, DialSettings settings) + { + ArgumentNullException.ThrowIfNull(uri); + ArgumentNullException.ThrowIfNull(settings); + if (!DialSettings.IsValidHomeCountryCode(settings.HomeCountryCode)) + { + throw new ArgumentException("The home country code must be one to three digits.", nameof(settings)); + } + + if (!DialSettings.IsValidOutsideLinePrefix(settings.OutsideLinePrefix)) + { + throw new ArgumentException("The outside line prefix must be at most four digits.", nameof(settings)); + } + + var number = uri.Number; + if (uri.IsGlobal) + { + var national = number[1..]; + number = national.StartsWith(settings.HomeCountryCode, StringComparison.Ordinal) + ? "0" + national[settings.HomeCountryCode.Length..] + : "00" + national; + } + + return new DialNumber(settings.OutsideLinePrefix + number); + } + + public override string ToString() => Digits; +} diff --git a/src/Waehlhilfe.Domain/DialSettings.cs b/src/Waehlhilfe.Domain/DialSettings.cs new file mode 100644 index 0000000..47a3c93 --- /dev/null +++ b/src/Waehlhilfe.Domain/DialSettings.cs @@ -0,0 +1,16 @@ +namespace Waehlhilfe.Domain; + +/// How numbers are dialled and through which port. +/// Serial port of the phone's Bluetooth SPP service; empty until chosen. +/// Country code that a global number is dialled nationally for. +/// Amtsholung, dialled before every number behind a PBX; usually empty. +public sealed record DialSettings(string PortName, string HomeCountryCode, string OutsideLinePrefix) +{ + public static DialSettings Default { get; } = new(string.Empty, "49", string.Empty); + + public static bool IsValidHomeCountryCode(string? code) => + code is { Length: >= 1 and <= 3 } && code[0] != '0' && code.All(char.IsAsciiDigit); + + public static bool IsValidOutsideLinePrefix(string? prefix) => + prefix is { Length: <= 4 } && prefix.All(char.IsAsciiDigit); +} diff --git a/src/Waehlhilfe.Domain/TelUri.cs b/src/Waehlhilfe.Domain/TelUri.cs new file mode 100644 index 0000000..5b230b4 --- /dev/null +++ b/src/Waehlhilfe.Domain/TelUri.cs @@ -0,0 +1,110 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text; + +namespace Waehlhilfe.Domain; + +/// +/// A tel: link (RFC 3966) reduced to the number it names. holds only +/// digits, with a leading + for a global number: the link comes from whatever page or +/// document the user clicked, and whatever reaches the phone after ATD is executed by it. +/// That rules out * and # too — on a landline *21*<number># sets up call +/// forwarding at the exchange. +/// +public sealed record TelUri +{ + public const int MaxDigits = 32; + + // Far above any real link; checked before decoding, so a huge argument costs nothing. + private const int MaxLinkLength = 256; + + private const string Scheme = "tel:"; + + private TelUri(string number) => Number = number; + + public string Number { get; } + + public bool IsGlobal => Number.StartsWith('+'); + + public static bool TryParse(string? link, [MaybeNullWhen(false)] out TelUri uri) + { + uri = null; + if (link is null || link.Length > MaxLinkLength) + { + return false; + } + + var text = link.Trim(); + if (!text.StartsWith(Scheme, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + text = text[Scheme.Length..]; + if (text.StartsWith("//", StringComparison.Ordinal)) + { + text = text[2..]; + } + + var separator = text.IndexOf(';', StringComparison.Ordinal); + var numberPart = separator < 0 ? text : text[..separator]; + if (separator >= 0 && !AreParametersClean(text[(separator + 1)..])) + { + return false; + } + + if (!TryClean(Uri.UnescapeDataString(numberPart), out var number)) + { + return false; + } + + uri = new TelUri(number); + return true; + } + + // Parameters (ext, isub, phone-context, …) never reach the phone, but a link carrying a + // control character anywhere is not one a person wrote, so it is refused as a whole. + private static bool AreParametersClean(string parameters) => + !Uri.UnescapeDataString(parameters).Any(char.IsControl); + + private static bool TryClean(string raw, [MaybeNullWhen(false)] out string number) + { + number = null; + var global = raw.StartsWith('+'); + if (global) + { + // "+49 (0)89 …" is how German numbers are often printed; the (0) is not dialled. + raw = raw[1..].Replace("(0)", string.Empty, StringComparison.Ordinal); + } + + var digits = new StringBuilder(raw.Length + 1); + if (global) + { + digits.Append('+'); + } + + foreach (var c in raw) + { + if (char.IsAsciiDigit(c)) + { + digits.Append(c); + } + else if (!IsVisualSeparator(c)) + { + return false; + } + } + + var count = digits.Length - (global ? 1 : 0); + if (count is 0 or > MaxDigits) + { + return false; + } + + number = digits.ToString(); + return true; + } + + // RFC 3966's visual separators, plus the space, no-break space and slash that German + // numbers are printed with ("089 / 1234567"). Never \r, \n or any other control character. + private static bool IsVisualSeparator(char c) => c is '-' or '.' or '(' or ')' or ' ' or ' ' or '/'; +} diff --git a/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs b/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs new file mode 100644 index 0000000..81bc38d --- /dev/null +++ b/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs @@ -0,0 +1,82 @@ +namespace Waehlhilfe.Domain.Tests; + +public sealed class DialNumberTests +{ + private static readonly DialSettings Germany = DialSettings.Default; + + private static DialNumber Dial(string link, DialSettings settings) + { + Assert.True(TelUri.TryParse(link, out var uri)); + return DialNumber.From(uri, settings); + } + + [Fact] + public void Dials_a_national_number_as_written() + { + Assert.Equal("0891234567", Dial("tel:0891234567", Germany).Digits); + } + + [Fact] + public void Turns_plus_49_into_a_leading_zero() + { + Assert.Equal("0891234567", Dial("tel:+49891234567", Germany).Digits); + } + + [Fact] + public void Turns_a_foreign_plus_into_double_zero() + { + Assert.Equal("0043121234", Dial("tel:+43121234", Germany).Digits); + } + + [Fact] + public void Uses_the_configured_home_country() + { + var austria = Germany with { HomeCountryCode = "43" }; + + Assert.Equal("0121234", Dial("tel:+43121234", austria).Digits); + Assert.Equal("0049891234567", Dial("tel:+49891234567", austria).Digits); + } + + [Fact] + public void Prepends_the_outside_line_prefix() + { + var behindPbx = Germany with { OutsideLinePrefix = "0" }; + + Assert.Equal("00891234567", Dial("tel:0891234567", behindPbx).Digits); + Assert.Equal("00891234567", Dial("tel:+49891234567", behindPbx).Digits); + } + + [Theory] + [InlineData("0;ATH")] + [InlineData("0\r")] + [InlineData("12345")] + [InlineData("a")] + [InlineData("*")] + [InlineData("#")] + public void Refuses_an_outside_line_prefix_that_is_not_up_to_four_digits(string prefix) + { + Assert.False(DialSettings.IsValidOutsideLinePrefix(prefix)); + Assert.Throws(() => Dial("tel:0891234567", Germany with { OutsideLinePrefix = prefix })); + } + + [Theory] + [InlineData("")] + [InlineData("0")] + [InlineData("04")] + [InlineData("1234")] + [InlineData("+49")] + [InlineData("4;")] + public void Refuses_a_home_country_code_that_is_not_one_to_three_digits(string code) + { + Assert.False(DialSettings.IsValidHomeCountryCode(code)); + Assert.Throws(() => Dial("tel:+49891234567", Germany with { HomeCountryCode = code })); + } + + [Fact] + public void Default_settings_are_for_germany_without_an_outside_line() + { + Assert.Equal("49", DialSettings.Default.HomeCountryCode); + Assert.Equal(string.Empty, DialSettings.Default.OutsideLinePrefix); + Assert.Equal(string.Empty, DialSettings.Default.PortName); + } +} diff --git a/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs b/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs new file mode 100644 index 0000000..c6d4555 --- /dev/null +++ b/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs @@ -0,0 +1,137 @@ +namespace Waehlhilfe.Domain.Tests; + +public sealed class TelUriTests +{ + [Theory] + [InlineData("tel:0891234567", "0891234567")] + [InlineData("TEL:0891234567", "0891234567")] + [InlineData(" tel:0891234567 ", "0891234567")] + [InlineData("tel://0891234567", "0891234567")] + public void Parses_a_plain_tel_link(string link, string expected) + { + Assert.True(TelUri.TryParse(link, out var uri)); + Assert.Equal(expected, uri.Number); + Assert.False(uri.IsGlobal); + } + + [Theory] + [InlineData("tel:089-123-4567")] + [InlineData("tel:089.123.4567")] + [InlineData("tel:(089) 1234567")] + [InlineData("tel:089%201234567")] + [InlineData("tel:089 / 1234567")] + [InlineData("tel:089 1234567")] + public void Drops_visual_separators(string link) + { + Assert.True(TelUri.TryParse(link, out var uri)); + Assert.Equal("0891234567", uri.Number); + } + + [Theory] + [InlineData("tel:+49891234567", "+49891234567")] + [InlineData("tel:+49-89-1234567", "+49891234567")] + [InlineData("tel:%2B49891234567", "+49891234567")] + public void Parses_a_global_number(string link, string expected) + { + Assert.True(TelUri.TryParse(link, out var uri)); + Assert.Equal(expected, uri.Number); + Assert.True(uri.IsGlobal); + } + + [Fact] + public void Drops_the_bracketed_zero_of_a_global_number() + { + Assert.True(TelUri.TryParse("tel:+49 (0)89 1234567", out var uri)); + Assert.Equal("+49891234567", uri.Number); + } + + [Theory] + [InlineData("tel:0891234567;ext=12")] + [InlineData("tel:0891234567;isub=12")] + [InlineData("tel:0891234567;phone-context=+49")] + public void Keeps_parameters_off_the_number(string link) + { + Assert.True(TelUri.TryParse(link, out var uri)); + Assert.Equal("0891234567", uri.Number); + } + + // On a landline a code such as *21*# sets up call forwarding at the exchange: a + // link carrying one would divert every call to the Feuerwehr's line to a number of its + // choosing, behind a confirm window that shows something that looks like a number. + [Theory] + [InlineData("tel:*21*015112345678%23")] + [InlineData("tel:%2A21%2A015112345678%23")] + [InlineData("tel:##002#")] + [InlineData("tel:*31#0891234567")] + [InlineData("tel:0891234567#")] + [InlineData("tel:089*1234567")] + public void Rejects_a_service_code_that_would_reconfigure_the_line(string link) + { + Assert.False(TelUri.TryParse(link, out _)); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("tel:")] + [InlineData("tel:+")] + [InlineData("0891234567")] + [InlineData("mailto:0891234567")] + [InlineData("https://example.org/?tel:0891234567")] + [InlineData("tel:089abc")] + [InlineData("tel:+49*31")] + [InlineData("tel:08+91234567")] + public void Rejects_what_is_not_a_dialable_tel_link(string? link) + { + Assert.False(TelUri.TryParse(link, out var uri)); + Assert.Null(uri); + } + + // A tel: link is produced by whatever web page or document the user clicked. Anything that + // reaches the phone after `ATD` is executed by it, so a smuggled line break or semicolon + // would let a link run AT commands of its own. + [Theory] + [InlineData("tel:0891234567%0D%0AATH")] + [InlineData("tel:0891234567%0AATZ")] + [InlineData("tel:0891234567\r\nATH")] + [InlineData("tel:0891234567%0D")] + [InlineData("tel:0891234567%00")] + public void Rejects_a_line_break_smuggled_in_by_percent_encoding(string link) + { + Assert.False(TelUri.TryParse(link, out _)); + } + + [Theory] + [InlineData("tel:0891234567%3BATH")] + [InlineData("tel:0891234567%3B")] + public void Rejects_a_semicolon_command(string link) + { + Assert.False(TelUri.TryParse(link, out _)); + } + + [Theory] + [InlineData("tel:0891234567;ext=1%0D%0AATH")] + [InlineData("tel:0891234567;foo\nbar")] + public void Rejects_a_line_break_in_a_parameter(string link) + { + Assert.False(TelUri.TryParse(link, out _)); + } + + [Fact] + public void Accepts_thirty_two_digits() + { + Assert.True(TelUri.TryParse("tel:+" + new string('1', 32), out _)); + } + + [Fact] + public void Rejects_more_than_thirty_two_digits() + { + Assert.False(TelUri.TryParse("tel:" + new string('1', 33), out _)); + } + + [Fact] + public void Rejects_an_oversized_link_before_decoding_it() + { + Assert.False(TelUri.TryParse("tel:1" + new string('-', 10_000), out _)); + } +}