Skip to content

Commit

Permalink
fix: CVE-2024-21521 and any future attempts for these
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu committed Jul 10, 2024
1 parent 09822a0 commit 402a8ab
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
17 changes: 10 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@discordjs/node-pre-gyp": "^0.4.5",
"node-addon-api": "^5.0.0"
"node-addon-api": "^8.1.0"
},
"devDependencies": {
"@types/node": "^18.11.2",
Expand Down
57 changes: 57 additions & 0 deletions src/node-opus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ Object OpusEncoder::Init(Napi::Env env, Object exports) {
OpusEncoder::OpusEncoder(const CallbackInfo& args): ObjectWrap<OpusEncoder>(args) {
this->encoder = nullptr;
this->decoder = nullptr;

if (args.Length() < 2) {
Napi::RangeError::New(args.Env(), "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(args.Env(), "Expected rate and channels to be numbers").ThrowAsJavaScriptException();
return;
}

this->rate = args[0].ToNumber().Int32Value();
this->channels = args[1].ToNumber().Int32Value();
this->application = OPUS_APPLICATION_AUDIO;
Expand Down Expand Up @@ -87,6 +98,11 @@ Napi::Value OpusEncoder::Encode(const CallbackInfo& args) {
return env.Null();
}

if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return env.Null();
}

if (!args[0].IsBuffer()) {
Napi::TypeError::New(env, "Provided input needs to be a buffer").ThrowAsJavaScriptException();
return env.Null();
Expand All @@ -102,11 +118,19 @@ Napi::Value OpusEncoder::Encode(const CallbackInfo& args) {
Buffer<char> actualBuf = Buffer<char>::Copy(env, reinterpret_cast<char*>(this->outOpus), compressedLength);

if (!actualBuf.IsEmpty()) return actualBuf;

Napi::Error::New(env, "Could not encode the data").ThrowAsJavaScriptException();
return env.Null();
}

Napi::Value OpusEncoder::Decode(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return env.Null();
}

if (!args[0].IsBuffer()) {
Napi::TypeError::New(env, "Provided input needs to be a buffer").ThrowAsJavaScriptException();
return env.Null();
Expand Down Expand Up @@ -140,11 +164,24 @@ Napi::Value OpusEncoder::Decode(const CallbackInfo& args) {
Buffer<char> actualBuf = Buffer<char>::Copy(env, reinterpret_cast<char*>(this->outPcm), decodedLength);

if (!actualBuf.IsEmpty()) return actualBuf;

Napi::Error::New(env, "Could not decode the data").ThrowAsJavaScriptException();
return env.Null();
}

void OpusEncoder::ApplyEncoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 2) {
Napi::RangeError::New(env, "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(env, "Expected ctl and value to be numbers").ThrowAsJavaScriptException();
return;
}

int ctl = args[0].ToNumber().Int32Value();
int value = args[1].ToNumber().Int32Value();

Expand All @@ -162,6 +199,16 @@ void OpusEncoder::ApplyEncoderCTL(const CallbackInfo& args) {
void OpusEncoder::ApplyDecoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 2) {
Napi::RangeError::New(env, "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(env, "Expected ctl and value to be numbers").ThrowAsJavaScriptException();
return;
}

int ctl = args[0].ToNumber().Int32Value();
int value = args[1].ToNumber().Int32Value();

Expand All @@ -179,6 +226,16 @@ void OpusEncoder::ApplyDecoderCTL(const CallbackInfo& args) {
void OpusEncoder::SetBitrate(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber()) {
Napi::TypeError::New(env, "Expected bitrate to be a number").ThrowAsJavaScriptException();
return;
}

int bitrate = args[0].ToNumber().Int32Value();

if (this->EnsureEncoder() != OPUS_OK) {
Expand Down

0 comments on commit 402a8ab

Please sign in to comment.