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
5 changes: 3 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*<number>#`) at the exchange. A security fix gets a test that replays the attack. Dialling always goes through the
confirm window; nothing dials silently.

## Tests
Expand Down
38 changes: 38 additions & 0 deletions src/Waehlhilfe.Domain/DialNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Waehlhilfe.Domain;

/// <summary>The exact string sent after <c>ATD</c>: digits only.</summary>
public sealed record DialNumber
{
private DialNumber(string digits) => Digits = digits;

public string Digits { get; }

/// <exception cref="ArgumentException">The settings carry an invalid country code or prefix.</exception>
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;
}
16 changes: 16 additions & 0 deletions src/Waehlhilfe.Domain/DialSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Waehlhilfe.Domain;

/// <summary>How numbers are dialled and through which port.</summary>
/// <param name="PortName">Serial port of the phone's Bluetooth SPP service; empty until chosen.</param>
/// <param name="HomeCountryCode">Country code that a global number is dialled nationally for.</param>
/// <param name="OutsideLinePrefix">Amtsholung, dialled before every number behind a PBX; usually empty.</param>
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);
}
110 changes: 110 additions & 0 deletions src/Waehlhilfe.Domain/TelUri.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace Waehlhilfe.Domain;

/// <summary>
/// A <c>tel:</c> link (RFC 3966) reduced to the number it names. <see cref="Number"/> holds only
/// digits, with a leading <c>+</c> for a global number: the link comes from whatever page or
/// document the user clicked, and whatever reaches the phone after <c>ATD</c> is executed by it.
/// That rules out <c>*</c> and <c>#</c> too — on a landline <c>*21*&lt;number&gt;#</c> sets up call
/// forwarding at the exchange.
/// </summary>
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 '/';
}
82 changes: 82 additions & 0 deletions tests/Waehlhilfe.Domain.Tests/DialNumberTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentException>(() => 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<ArgumentException>(() => 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);
}
}
Loading
Loading