Skip to content

Commit

Permalink
Don't leave bytes of mask field uninitialized for client-to-server fr…
Browse files Browse the repository at this point in the history
…ames of zero-length payload. Closes Issue #178.
  • Loading branch information
theturtle32 committed Jan 16, 2015
1 parent 3a9daf0 commit 3036c73
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions lib/WebSocketFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,29 +252,18 @@ WebSocketFrame.prototype.toBuffer = function(nullMask) {
outputPos += 8;
}

if (this.length > 0) {
if (this.mask) {
if (!nullMask) {
// Generate a mask key
maskKey = parseInt(Math.random()*0xFFFFFFFF);
}
else {
maskKey = 0x00000000;
}
this.maskBytes.writeUInt32BE(maskKey, 0, true);

// write the mask key
this.maskBytes.copy(output, outputPos);
outputPos += 4;
if (this.mask) {
maskKey = nullMask ? 0 : (Math.random()*0xFFFFFFFF) | 0;
this.maskBytes.writeUInt32BE(maskKey, 0, true);

// write the mask key
this.maskBytes.copy(output, outputPos);
outputPos += 4;

data.copy(output, outputPos);
var dataSegment = output.slice(outputPos);
bufferUtil.mask(dataSegment, this.maskBytes, dataSegment, 0, this.length);
// xor(output.slice(outputPos), this.maskBytes, 0);
}
else {
data.copy(output, outputPos);
}
bufferUtil.mask(data, this.maskBytes, output, outputPos, this.length);
}
else {
data.copy(output, outputPos);
}

return output;
Expand Down

0 comments on commit 3036c73

Please sign in to comment.