abbr / NodeSSPI

Server-side windows authentication for Node.js
MIT License
156 stars 19 forks source link

Crashes on Node 14 or 16 #99

Open jg76379 opened 2 years ago

jg76379 commented 2 years ago

We have been using node-sspi for years in an internal application. It was working great in Node 12 and earlier, however I was working on upgrading to Node 14 or 16 last week since the 12.X branch is now out of long-term support.

I am using the latest verion of NodeSSPI (0.2.9).

Again, this error does not occur in Node 12.22.12, but it does in both 14.19.1 and 16.15.0

The error seems to occur after refreshing the front-end page a couple of times.

Here is the truncated error message:

Uncaught Error Error: Invalid argument
    --- generic ---
    at init (node:internal/inspector_async_hook:25:19)
    at emitInitNative (node:internal/async_hooks:201:43)
    at main.authenticate (c:\applications\myapp\node_modules\node-sspi\index.js:61:11)
    at <anonymous> (c:\applications\myapp\src\app.ts:76:25)

The line in myapp from the traceback is where nodeSSPIObj.authenticate is called:

app.use(function (req: any, res: any, next: any) {
  try{
      var nodeSSPI = require('node-sspi');
      var nodeSSPIObj = new nodeSSPI();
      nodeSSPIObj.authenticate(req, res, function (err: any) {
          if(err){
              console.error(err);
          }
          res.finished || next();
      });
  }
  catch (err){
      console.error(err)
  }
});

I am unable to even catch the error so not sure if something is happening in the native code which causes Node to crash.

Update 2022-05-19

I attached to the process from Visual Studio while my node app was running in order to debug the NodeSSPI native code. I was able to break on the exception:

Exception thrown at 0x00007FFD97504F69 in node.exe: Microsoft C++ exception: Napi::Error at memory location 0x00000044792FE980.

Here is the Call Stack at the point of the exception being thrown:

    nodeSSPI.node!Napi::Object::Get(Napi::Value key) Line 1007  C++
    nodeSSPI.node!WrapUpAsyncAfterAuth(const Napi::Env env, Baton * pBaton) Line 415    C++
    nodeSSPI.node!AsyncAfterSSPIAuth(Napi::Env env, Baton * pBaton) Line 788    C++
    nodeSSPI.node!AsyncSSPIWorker::OnOK() Line 952  C++
    nodeSSPI.node!Napi::AsyncWorker::OnWorkComplete::__l5::<lambda>() Line 3733 C++
    nodeSSPI.node!Napi::details::WrapCallback<void * <lambda>(void)>(Napi::AsyncWorker::OnWorkComplete::__l5::void * <lambda>(void) callback) Line 61   C++
    nodeSSPI.node!Napi::AsyncWorker::OnWorkComplete(napi_env__ * __formal, napi_status status, void * this_pointer) Line 3730   C++
jg76379 commented 2 years ago

I'm trying to debug this more but I don't have any experience with C++ or NAPI. In Visual Studio when i attach to the process I can set breakpoints and they work but when I inspect the locals they are all in hex like _env = 0x000001dc4a99e0d0 {...}. Is there a way to inspect the actual values? I did install the node-sspi package with the debug flag (npm install --debug).

Any suggestions on how to debug this further are appreciated.

fknx commented 2 years ago

I can confirm this issue (NodeSSPI 0.2.9 with Node 16.13.2). Initially everything works fine, then after a few successful authentications the Node process crashes with the 0xC000013A exit code after writing

[Error: Invalid argument]
undefined:0

to the log.

I can't offer a solution, but node-expose-sspi seems to work fine as a replacement.

jg76379 commented 2 years ago

I can't offer a solution, but node-expose-sspi seems to work fine as a replacement.

@fknx Thanks for the reply. Glad to hear that node-expose-sspi works as an alternative at least.

ecke78 commented 1 year ago

We are seeing this issue to.

jg76379 commented 1 year ago

@ecke78 I can confirm that node-expose-sspi works well as a replacement (as @fknx suggested). No issues since switching to it.

ecke78 commented 1 year ago

I've switched over to it today and it seems to be working great, thanks.