From db772316a9706864474c0366841a827060c57822 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Mon, 19 May 2025 20:02:00 +0200 Subject: [PATCH] Write UnparseablePacket object for non-critical unknown packets Instead of ignoring non-critical unknown packets entirely, write an UnparseablePacket object to the packet stream. --- src/packet/packetlist.js | 21 ++++++++++----------- test/general/packet.js | 6 +++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/packet/packetlist.js b/src/packet/packetlist.js index cf034861..da2c2a15 100644 --- a/src/packet/packetlist.js +++ b/src/packet/packetlist.js @@ -100,18 +100,17 @@ class PacketList extends Array { // If an implementation encounters a critical packet where the packet type is unknown in a packet sequence, // it MUST reject the whole packet sequence. On the other hand, an unknown non-critical packet MUST be ignored. // Packet Tags from 0 to 39 are critical. Packet Tags from 40 to 63 are non-critical. - if (e instanceof UnknownPacketError) { - if (parsed.tag <= 39) { - await writer.abort(e); - } else { - return; - } - } - - const throwUnsupportedError = !config.ignoreUnsupportedPackets && e instanceof UnsupportedError; - const throwMalformedError = !config.ignoreMalformedPackets && !(e instanceof UnsupportedError); + const throwUnknownPacketError = e instanceof UnknownPacketError && parsed.tag <= 39; + const throwUnsupportedError = e instanceof UnsupportedError && !(e instanceof UnknownPacketError) && !config.ignoreUnsupportedPackets; + const throwMalformedError = !(e instanceof UnsupportedError) && !config.ignoreMalformedPackets; const throwGrammarError = e instanceof GrammarError; - if (throwUnsupportedError || throwMalformedError || throwGrammarError || supportsStreaming(parsed.tag)) { + if ( + throwUnknownPacketError || + throwUnsupportedError || + throwMalformedError || + throwGrammarError || + supportsStreaming(parsed.tag) + ) { // The packets that support streaming are the ones that contain message data. // Those are also the ones we want to be more strict about and throw on parse errors // (since we likely cannot process the message without these packets anyway). diff --git a/test/general/packet.js b/test/general/packet.js index 76982462..bf58466e 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -1335,8 +1335,8 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu it('Ignores non-critical packet even with tolerant mode disabled', async function() { const unknownPacketTag63 = util.hexToUint8Array('ff0a750064bf943d6e756c6c'); // non-critical tag - await expect(openpgp.PacketList.fromBinary(unknownPacketTag63, allAllowedPackets, { ...openpgp.config, ignoreUnsupportedPackets: false, ignoreMalformedPackets: false })).to.eventually.have.length(0); - await expect(openpgp.PacketList.fromBinary(unknownPacketTag63, allAllowedPackets, { ...openpgp.config, ignoreUnsupportedPackets: true, ignoreMalformedPackets: true })).to.eventually.have.length(0); + await expect(openpgp.PacketList.fromBinary(unknownPacketTag63, allAllowedPackets, { ...openpgp.config, ignoreUnsupportedPackets: false, ignoreMalformedPackets: false })).to.eventually.have.length(1); + await expect(openpgp.PacketList.fromBinary(unknownPacketTag63, allAllowedPackets, { ...openpgp.config, ignoreUnsupportedPackets: true, ignoreMalformedPackets: true })).to.eventually.have.length(1); }); it('Throws on disallowed packet even with tolerant mode enabled', async function() { @@ -1403,7 +1403,7 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu it('accepts unknown packets', async () => { const unknownPacketTag63 = util.hexToUint8Array('ff0a750064bf943d6e756c6c'); // non-critical tag const parsed = await openpgp.PacketList.fromBinary(unknownPacketTag63, allAllowedPackets, openpgp.config, getMessageGrammarValidator({ delayReporting: false })); - expect(parsed.length).to.equal(0); + expect(parsed.length).to.equal(1); }); it('delay reporting', () => {