From 00b11a62a6e6c34b30ef831b03621c4508f6c4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= <1005065+DeepDiver1975@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:35:25 +0100 Subject: [PATCH 1/2] feat(domain): parse tel: links and normalise the number to dial MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TelUri reduces an RFC 3966 link to 0-9 * # with an optional leading +, dropping visual separators, the printed "(0)" and all parameters. Since whatever follows ATD is executed by the phone, a link carrying a line break, a semicolon or any other character is refused, as is one longer than 32 digits or 256 characters. DialNumber turns + into a leading 0, any other + into 00, and prepends the Amtsholung prefix from DialSettings. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com> --- src/Waehlhilfe.Domain/DialNumber.cs | 38 +++++ src/Waehlhilfe.Domain/DialSettings.cs | 16 +++ src/Waehlhilfe.Domain/TelUri.cs | 108 +++++++++++++++ .../DialNumberTests.cs | 86 ++++++++++++ tests/Waehlhilfe.Domain.Tests/TelUriTests.cs | 131 ++++++++++++++++++ 5 files changed, 379 insertions(+) create mode 100644 src/Waehlhilfe.Domain/DialNumber.cs create mode 100644 src/Waehlhilfe.Domain/DialSettings.cs create mode 100644 src/Waehlhilfe.Domain/TelUri.cs create mode 100644 tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs create mode 100644 tests/Waehlhilfe.Domain.Tests/TelUriTests.cs diff --git a/src/Waehlhilfe.Domain/DialNumber.cs b/src/Waehlhilfe.Domain/DialNumber.cs new file mode 100644 index 0000000..8b1d49d --- /dev/null +++ b/src/Waehlhilfe.Domain/DialNumber.cs @@ -0,0 +1,38 @@ +namespace Waehlhilfe.Domain; + +/// The exact string sent after ATD: only 0-9 * #. +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..5458cef --- /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(c => char.IsAsciiDigit(c) || c is '*' or '#'); +} diff --git a/src/Waehlhilfe.Domain/TelUri.cs b/src/Waehlhilfe.Domain/TelUri.cs new file mode 100644 index 0000000..df63dca --- /dev/null +++ b/src/Waehlhilfe.Domain/TelUri.cs @@ -0,0 +1,108 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text; + +namespace Waehlhilfe.Domain; + +/// +/// A tel: link (RFC 3966) reduced to the number it names. holds only +/// 0-9 * #, 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. +/// +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) || (!global && c is '*' or '#')) + { + 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..9bf1c8c --- /dev/null +++ b/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs @@ -0,0 +1,86 @@ +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); + } + + [Fact] + public void Keeps_a_service_code_as_written() + { + Assert.Equal("*31#0891234567", Dial("tel:*31#0891234567", Germany).Digits); + } + + [Theory] + [InlineData("0;ATH")] + [InlineData("0\r")] + [InlineData("12345")] + [InlineData("a")] + public void Refuses_an_outside_line_prefix_that_is_not_a_short_digit_string(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..96f0586 --- /dev/null +++ b/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs @@ -0,0 +1,131 @@ +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); + } + + [Theory] + [InlineData("tel:*31#0891234567", "*31#0891234567")] + [InlineData("tel:%2A31%230891234567", "*31#0891234567")] + public void Keeps_star_and_hash_service_codes(string link, string expected) + { + Assert.True(TelUri.TryParse(link, out var uri)); + Assert.Equal(expected, uri.Number); + } + + [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 _)); + } +} From 8529019c88190299d8e752d236319d24d332c2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= <1005065+DeepDiver1975@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:37:47 +0100 Subject: [PATCH 2/2] fix(domain): refuse * and # in tel: links MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On a landline a supplementary-service code such as *21*# sets up call forwarding at the exchange. A link carrying one would divert every call to the Feuerwehr's line, behind a confirm window that shows what looks like a number. Links, and the Amtsholung prefix, are now digits only; the attack is replayed in plain and percent-encoded form. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com> --- AGENTS.md | 5 +++-- src/Waehlhilfe.Domain/DialNumber.cs | 2 +- src/Waehlhilfe.Domain/DialSettings.cs | 2 +- src/Waehlhilfe.Domain/TelUri.cs | 8 +++++--- tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs | 10 +++------- tests/Waehlhilfe.Domain.Tests/TelUriTests.cs | 16 +++++++++++----- 6 files changed, 24 insertions(+), 19 deletions(-) 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 index 8b1d49d..5f4f3ea 100644 --- a/src/Waehlhilfe.Domain/DialNumber.cs +++ b/src/Waehlhilfe.Domain/DialNumber.cs @@ -1,6 +1,6 @@ namespace Waehlhilfe.Domain; -/// The exact string sent after ATD: only 0-9 * #. +/// The exact string sent after ATD: digits only. public sealed record DialNumber { private DialNumber(string digits) => Digits = digits; diff --git a/src/Waehlhilfe.Domain/DialSettings.cs b/src/Waehlhilfe.Domain/DialSettings.cs index 5458cef..47a3c93 100644 --- a/src/Waehlhilfe.Domain/DialSettings.cs +++ b/src/Waehlhilfe.Domain/DialSettings.cs @@ -12,5 +12,5 @@ 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(c => char.IsAsciiDigit(c) || c is '*' or '#'); + prefix is { Length: <= 4 } && prefix.All(char.IsAsciiDigit); } diff --git a/src/Waehlhilfe.Domain/TelUri.cs b/src/Waehlhilfe.Domain/TelUri.cs index df63dca..5b230b4 100644 --- a/src/Waehlhilfe.Domain/TelUri.cs +++ b/src/Waehlhilfe.Domain/TelUri.cs @@ -5,8 +5,10 @@ namespace Waehlhilfe.Domain; /// /// A tel: link (RFC 3966) reduced to the number it names. holds only -/// 0-9 * #, 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. +/// 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 { @@ -82,7 +84,7 @@ private static bool TryClean(string raw, [MaybeNullWhen(false)] out string numbe foreach (var c in raw) { - if (char.IsAsciiDigit(c) || (!global && c is '*' or '#')) + if (char.IsAsciiDigit(c)) { digits.Append(c); } diff --git a/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs b/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs index 9bf1c8c..81bc38d 100644 --- a/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs +++ b/tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs @@ -46,18 +46,14 @@ public void Prepends_the_outside_line_prefix() Assert.Equal("00891234567", Dial("tel:+49891234567", behindPbx).Digits); } - [Fact] - public void Keeps_a_service_code_as_written() - { - Assert.Equal("*31#0891234567", Dial("tel:*31#0891234567", Germany).Digits); - } - [Theory] [InlineData("0;ATH")] [InlineData("0\r")] [InlineData("12345")] [InlineData("a")] - public void Refuses_an_outside_line_prefix_that_is_not_a_short_digit_string(string prefix) + [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 })); diff --git a/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs b/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs index 96f0586..c6d4555 100644 --- a/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs +++ b/tests/Waehlhilfe.Domain.Tests/TelUriTests.cs @@ -55,13 +55,19 @@ public void Keeps_parameters_off_the_number(string link) 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:*31#0891234567", "*31#0891234567")] - [InlineData("tel:%2A31%230891234567", "*31#0891234567")] - public void Keeps_star_and_hash_service_codes(string link, string expected) + [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.True(TelUri.TryParse(link, out var uri)); - Assert.Equal(expected, uri.Number); + Assert.False(TelUri.TryParse(link, out _)); } [Theory]