Write UnparseablePacket object for non-critical unknown packets

Instead of ignoring non-critical unknown packets entirely, write an
UnparseablePacket object to the packet stream.
This commit is contained in:
Daniel Huigens 2025-05-19 20:02:00 +02:00
parent 88cd1810a3
commit db772316a9
No known key found for this signature in database
GPG Key ID: CB064A128FA90686
2 changed files with 13 additions and 14 deletions

View File

@ -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, // 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. // 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. // Packet Tags from 0 to 39 are critical. Packet Tags from 40 to 63 are non-critical.
if (e instanceof UnknownPacketError) { const throwUnknownPacketError = e instanceof UnknownPacketError && parsed.tag <= 39;
if (parsed.tag <= 39) { const throwUnsupportedError = e instanceof UnsupportedError && !(e instanceof UnknownPacketError) && !config.ignoreUnsupportedPackets;
await writer.abort(e); const throwMalformedError = !(e instanceof UnsupportedError) && !config.ignoreMalformedPackets;
} else {
return;
}
}
const throwUnsupportedError = !config.ignoreUnsupportedPackets && e instanceof UnsupportedError;
const throwMalformedError = !config.ignoreMalformedPackets && !(e instanceof UnsupportedError);
const throwGrammarError = e instanceof GrammarError; 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. // 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 // 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). // (since we likely cannot process the message without these packets anyway).

View File

@ -1335,8 +1335,8 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu
it('Ignores non-critical packet even with tolerant mode disabled', async function() { it('Ignores non-critical packet even with tolerant mode disabled', async function() {
const unknownPacketTag63 = util.hexToUint8Array('ff0a750064bf943d6e756c6c'); // non-critical tag 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: 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(0); 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() { it('Throws on disallowed packet even with tolerant mode enabled', async function() {
@ -1403,7 +1403,7 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu
it('accepts unknown packets', async () => { it('accepts unknown packets', async () => {
const unknownPacketTag63 = util.hexToUint8Array('ff0a750064bf943d6e756c6c'); // non-critical tag const unknownPacketTag63 = util.hexToUint8Array('ff0a750064bf943d6e756c6c'); // non-critical tag
const parsed = await openpgp.PacketList.fromBinary(unknownPacketTag63, allAllowedPackets, openpgp.config, getMessageGrammarValidator({ delayReporting: false })); 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', () => { it('delay reporting', () => {