fix(#1289): tighten share-link parsing from review

Three gaps found in review:

- A link in brackets, "(meshcore://...)", took the closing bracket with
  it and was reported as invalid. The bracket is now left out unless
  the link opened one itself.
- The legacy advert form had no upper length, so a huge link from
  another app was decoded in full. An advert fits in one radio packet,
  so anything past 255 bytes is refused.
- A channel link with a blank name and a secret was accepted, and
  would add a channel with no visible name. It is refused.

Regression tests, each failing against the code before this commit:
- meshcore_linkifier_test: a closing bracket is left out of the link.
- link_router_test: a 256-byte advert link is refused.
- channel_share_uri_test: a blank name is refused even with a secret.

Epic: #1285
Feature: #1280

Agent: StormyElk (session 088ce922)
pull/1308/head
diffwireauto 2 days ago
parent 16a1e62f57
commit 22815ee404

@ -30,7 +30,14 @@ class MeshcoreLinkifier extends Linkifier {
var last = 0;
for (final match in _link.allMatches(text)) {
// A sentence ending right after a link keeps its full stop.
final url = match[0]!.replaceFirst(_trailingPunctuation, '');
var url = match[0]!.replaceFirst(_trailingPunctuation, '');
// "(meshcore://...)": the closing bracket is the sentence's, unless
// the link opened one itself.
if (url.endsWith(')') && !url.contains('(')) {
url = url
.substring(0, url.length - 1)
.replaceFirst(_trailingPunctuation, '');
}
final end = match.start + url.length;
if (match.start > last) {
out.add(TextElement(text.substring(last, match.start)));

@ -125,7 +125,7 @@ class Channel {
if (parsed.path.replaceAll('/', '') != 'add') return null;
final name = parsed.queryParameters['name'];
final secret = parsed.queryParameters['secret'];
if (name == null) return null;
if (name == null || name.trim().isEmpty) return null;
if (secret == null) return _fromHashtagName(name, index);
try {
return Channel(index: index, name: name, psk: parsePskHex(secret));

@ -45,8 +45,9 @@ class AdvertShareLink extends ShareLink {
String? get displayName => null;
}
// An advert is at least 98 bytes: key, timestamp, signature, flags.
final _advertLink = RegExp(r'^meshcore://((?:[0-9a-fA-F]{2}){98,})$');
// An advert is at least 98 bytes: key, timestamp, signature, flags. It fits
// in one radio packet, so anything past 255 bytes is not one.
final _advertLink = RegExp(r'^meshcore://((?:[0-9a-fA-F]{2}){98,255})$');
/// Parses [raw] into a link Offband can add, or null if it is not one.
ShareLink? parseShareLink(String raw) {

@ -49,6 +49,17 @@ void main() {
]);
});
test('leaves a closing bracket around the link out of it', () {
final elements = _parse('(see $_link).');
expect(elements.whereType<UrlElement>().single.url, _link);
expect((elements.last as TextElement).text, ').');
});
test('keeps a bracket the link itself opened', () {
const named = 'meshcore://channel/add?name=%23a(b)';
expect(_parse('join $named').whereType<UrlElement>().single.url, named);
});
test('plain text is left alone', () {
final elements = _parse('no links here');
expect(elements, hasLength(1));

@ -89,5 +89,15 @@ void main() {
);
expect(Channel.fromShareUri('meshcore://channel/add'), isNull);
});
test('a blank name is refused, even with a secret', () {
expect(
Channel.fromShareUri(
'meshcore://channel/add?name=%20%20'
'&secret=00112233445566778899aabbccddeeff',
),
isNull,
);
});
});
}

@ -43,6 +43,8 @@ void main() {
'meshcore://contact/add?name=Bob',
'meshcore://${'ab' * 10}',
'meshcore://${'ab' * 98}a',
// Longer than any radio packet: not an advert.
'meshcore://${'ab' * 256}',
'meshcore://zz',
'',
]) {

Loading…
Cancel
Save

Powered by TurnKey Linux.