Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/voice/src/receive/VoiceReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ export class VoiceReceiver {
let packet: Buffer = this.decrypt(buffer, mode, nonce, secretKey);
if (!packet) throw new Error('Failed to parse packet');

// Strip padding (RFC3550 5.1)
const hasPadding = buffer[0] && Boolean(buffer[0] & 0b100000);
if (hasPadding) {
const paddingAmount = packet[packet.length - 1]!;
if (paddingAmount < packet.length) {
packet = packet.subarray(0, packet.length - paddingAmount);
}
}

// Strip decrypted RTP Header Extension if present
// The header is only indicated in the original data, so compare with buffer first
if (buffer.subarray(12, 14).compare(HEADER_EXTENSION_BYTE) === 0) {
Expand Down Expand Up @@ -176,6 +185,14 @@ export class VoiceReceiver {
if (!stream) return;

if (this.connectionData.encryptionMode && this.connectionData.nonceBuffer && this.connectionData.secretKey) {
// As a guard, we shouldn't parse packets that (1) aren't voice packets and (2) are not in the right RTP version
// 0x78 (120) is the default Opus payload type, the marker bit is (largely) unused here by Discord
if (msg[1] !== 0x78) return;

// Ignore packets not in RTP version 2
const rtpVersion = msg[0]! >> 6;
if (rtpVersion !== 2) return;

try {
const packet = this.parsePacket(
msg,
Expand Down
Loading