From a9335ed4c7e5ad4f7f0bdfa1ddca7838c32961d1 Mon Sep 17 00:00:00 2001 From: Zach Anderson Date: Tue, 13 Aug 2019 12:26:21 -0700 Subject: [PATCH] Catch FormatException in packet decoding. --- packages/multicast_dns/CHANGELOG.md | 3 + packages/multicast_dns/lib/src/packet.dart | 3 + packages/multicast_dns/pubspec.yaml | 2 +- packages/multicast_dns/test/decode_test.dart | 259 +++++++++++++++++++ 4 files changed, 266 insertions(+), 1 deletion(-) diff --git a/packages/multicast_dns/CHANGELOG.md b/packages/multicast_dns/CHANGELOG.md index 29e2aefc1d..da11f54bb5 100644 --- a/packages/multicast_dns/CHANGELOG.md +++ b/packages/multicast_dns/CHANGELOG.md @@ -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. diff --git a/packages/multicast_dns/lib/src/packet.dart b/packages/multicast_dns/lib/src/packet.dart index eac3f6793f..7e1e5e71af 100644 --- a/packages/multicast_dns/lib/src/packet.dart +++ b/packages/multicast_dns/lib/src/packet.dart @@ -366,6 +366,9 @@ List decodeMDnsResponse(List 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; } diff --git a/packages/multicast_dns/pubspec.yaml b/packages/multicast_dns/pubspec.yaml index 8eb23b7976..2c20513227 100644 --- a/packages/multicast_dns/pubspec.yaml +++ b/packages/multicast_dns/pubspec.yaml @@ -2,7 +2,7 @@ name: multicast_dns description: Dart package for mDNS queries (e.g. Bonjour, Avahi). author: Flutter Team homepage: https://github.com/flutter/packages/tree/master/packages/multicast_dns -version: 0.2.0 +version: 0.2.1 dependencies: meta: ^1.1.6 diff --git a/packages/multicast_dns/test/decode_test.dart b/packages/multicast_dns/test/decode_test.dart index 56aa02026a..637d3a0324 100644 --- a/packages/multicast_dns/test/decode_test.dart +++ b/packages/multicast_dns/test/decode_test.dart @@ -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 packetWithoutQuestionWithAnArCount = [ 50, 51, ]; + +// Package with a text resource that is not valid utf8. +const List nonUtf8Package = [ + 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 +];