Catch FormatException in packet decoding.

This commit is contained in:
Zach Anderson
2019-08-13 12:26:21 -07:00
parent caf02e0174
commit a9335ed4c7
4 changed files with 266 additions and 1 deletions

View File

@ -1,3 +1,6 @@
## 0.2.1
* Fixes the handling of packets containing non-utf8 strings.
## 0.2.0
* Allow configuration of the port and address the mdns query is performed on.

View File

@ -366,6 +366,9 @@ List<ResourceRecord> decodeMDnsResponse(List<int> packet) {
} on MDnsDecodeException {
// If decoding fails return null.
return null;
} on FormatException {
// If decoding fails on a non-utf8 packet, return null.
return null;
}
return result;
}

View File

@ -2,7 +2,7 @@ name: multicast_dns
description: Dart package for mDNS queries (e.g. Bonjour, Avahi).
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/packages/tree/master/packages/multicast_dns
version: 0.2.0
version: 0.2.1
dependencies:
meta: ^1.1.6

View File

@ -13,6 +13,7 @@ const int _kSrvHeaderSize = 6;
void main() {
testValidPackages();
testBadPackages();
testNonUtf8Packages();
// testHexDumpList();
testPTRRData();
testSRVRData();
@ -175,6 +176,12 @@ void testBadPackages() {
});
}
void testNonUtf8Packages() {
test('Returns null for non-utf8 text resource', () {
expect(decodeMDnsResponse(nonUtf8Package), isNull);
});
}
void testPTRRData() {
test('Can read FQDN from PTR data', () {
expect('sgjesse-macbookpro2 [78:31:c1:b8:55:38]._workstation._tcp.local',
@ -1178,3 +1185,255 @@ const List<int> packetWithoutQuestionWithAnArCount = <int>[
50,
51,
];
// Package with a text resource that is not valid utf8.
const List<int> nonUtf8Package = <int>[
0x00,
0x00,
0x84,
0x00,
0x00,
0x00,
0x00,
0x08,
0x00,
0x00,
0x00,
0x00,
0x1f,
0x72,
0x61,
0x73,
0x70,
0x62,
0x65,
0x72,
0x72,
0x79,
0x70,
0x69,
0x20,
0x5b,
0x62,
0x38,
0x3a,
0x32,
0x37,
0x3a,
0x65,
0x62,
0xd2,
0x30,
0x33,
0x3a,
0x39,
0x32,
0x3a,
0x34,
0x62,
0x5d,
0x0c,
0x5f,
0x77,
0x6f,
0x72,
0x6b,
0x73,
0x74,
0x61,
0x74,
0x69,
0x6f,
0x6e,
0x04,
0x5f,
0x74,
0x63,
0x70,
0x05,
0x6c,
0x6f,
0x63,
0x61,
0x6c,
0x00,
0x00,
0x10,
0x80,
0x01,
0x00,
0x00,
0x11,
0x94,
0x00,
0x01,
0x00,
0x0b,
0x5f,
0x75,
0x64,
0x69,
0x73,
0x6b,
0x73,
0x2d,
0x73,
0x73,
0x68,
0xc0,
0x39,
0x00,
0x0c,
0x00,
0x01,
0x00,
0x00,
0x11,
0x94,
0x00,
0x0e,
0x0b,
0x72,
0x61,
0x73,
0x70,
0x62,
0x65,
0x72,
0x72,
0x79,
0x70,
0x69,
0xc0,
0x50,
0xc0,
0x68,
0x00,
0x21,
0x80,
0x01,
0x00,
0x00,
0x00,
0x78,
0x00,
0x14,
0x00,
0x00,
0x00,
0x00,
0x00,
0x16,
0x0b,
0x72,
0x61,
0x73,
0x70,
0x62,
0x65,
0x72,
0x72,
0x79,
0x70,
0x69,
0xc0,
0x3e,
0xc0,
0x68,
0x00,
0x10,
0x80,
0x01,
0x00,
0x00,
0x11,
0x94,
0x00,
0x01,
0x00,
0x09,
0x5f,
0x73,
0x65,
0x72,
0x76,
0x69,
0x63,
0x65,
0x73,
0x07,
0x5f,
0x64,
0x6e,
0x73,
0x2d,
0x73,
0x64,
0x04,
0x5f,
0x75,
0x64,
0x70,
0xc0,
0x3e,
0x00,
0x0c,
0x00,
0x01,
0x00,
0x00,
0x11,
0x94,
0x00,
0x02,
0xc0,
0x50,
0xc0,
0x2c,
0x00,
0x0c,
0x00,
0x01,
0x00,
0x00,
0x11,
0x94,
0x00,
0x02,
0xc0,
0x0c,
0xc0,
0x0c,
0x00,
0x21,
0x80,
0x01,
0x00,
0x00,
0x00,
0x78,
0x00,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x09,
0xc0,
0x88,
0xc0,
0xa3,
0x00,
0x0c,
0x00,
0x01,
0x00,
0x00,
0x11,
0x94,
0x00,
0x02,
0xc0,
0x2c
];