Skip to content

Commit

Permalink
Add additionalAllowedPackets config option
Browse files Browse the repository at this point in the history
  • Loading branch information
toberndo committed Mar 23, 2023
1 parent f72e34f commit 67d6784
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ export default {
* @property {Boolean} ignoreMalformedPackets Ignore malformed packets on parsing instead of throwing an error
*/
ignoreMalformedPackets: false,
/**
* @memberof module:config
* @property {Array} additionalAllowedPackets Allow additional packets on parsing. Defined as array of packet classes, e.g. [PublicKeyPacket]
*/
additionalAllowedPackets: [],
/**
* @memberof module:config
* @property {Boolean} showVersion Whether to include {@link module:config/config.versionString} in armored messages
Expand Down
7 changes: 5 additions & 2 deletions src/packet/packetlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import defaultConfig from '../config';
* @returns {Object} New packet object with type based on tag
* @throws {Error|UnsupportedError} for disallowed or unknown packets
*/
export function newPacketFromTag(tag, allowedPackets) {
export function newPacketFromTag(tag, allowedPackets, config = defaultConfig) {
if (config.additionalAllowedPackets && config.additionalAllowedPackets.length) {
allowedPackets = { ...allowedPackets, ...util.constructAllowedPackets(config.additionalAllowedPackets) };
}
if (!allowedPackets[tag]) {
// distinguish between disallowed packets and unknown ones
let packetType;
Expand Down Expand Up @@ -77,7 +80,7 @@ class PacketList extends Array {
// - Trust packets SHOULD be ignored outside of keyrings (unsupported): https://datatracker.ietf.org/doc/html/rfc4880#section-5.10
return;
}
const packet = newPacketFromTag(parsed.tag, allowedPackets);
const packet = newPacketFromTag(parsed.tag, allowedPackets, config);
packet.packets = new PacketList();
packet.fromStream = util.isStream(parsed.packet);
await packet.read(parsed.packet, config);
Expand Down
15 changes: 15 additions & 0 deletions test/general/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,5 +1052,20 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu
expect(parsed.length).to.equal(1);
expect(parsed[0].tag).to.equal(openpgp.enums.packet.userID);
});

it('Allow parsing of additional packets provided in `config.additionalAllowedPackets`', async function () {
const packets = new openpgp.PacketList();
packets.push(new openpgp.LiteralDataPacket());
packets.push(openpgp.UserIDPacket.fromObject({ name:'test', email:'test@a.it' }));
const bytes = packets.write();
const allowedPackets = { [openpgp.enums.packet.literalData]: openpgp.LiteralDataPacket };
await expect(openpgp.PacketList.fromBinary(bytes, allowedPackets)).to.be.rejectedWith(/Packet not allowed in this context: userID/);
const parsed = await openpgp.PacketList.fromBinary(bytes, allowedPackets, { ...openpgp.config, additionalAllowedPackets: [openpgp.UserIDPacket] });
expect(parsed.length).to.equal(1);
expect(parsed[0].constructor.tag).to.equal(openpgp.enums.packet.literalData);
const otherPackets = await stream.readToEnd(parsed.stream, _ => _);
expect(otherPackets.length).to.equal(1);
expect(otherPackets[0].constructor.tag).to.equal(openpgp.enums.packet.userID);
});
});
});

0 comments on commit 67d6784

Please sign in to comment.