automation-stack / node-machine-id

Unique machine (desktop) id (no admin privileges required)
MIT License
577 stars 59 forks source link

Command failed: Registry editing has been disabled by your administrator. #56

Open pateljoel opened 3 years ago

pateljoel commented 3 years ago

I am having lots of errors on Windows (App not loading properly) and after some digging, the error happens to be from this library that relies on running regedit in the background.

If I run this code:

const machine = require('node-machine-id');

machine.machineId().then((id) => {
    console.log(id);
}).catch((err) => {
    console.log(err);
})

I get the following error on Windows:

C:\Users\j\machineId> node index.js
Error: Error while obtaining machine id: Error: Command failed: %windir%\System32\REG.exe QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid
ERROR: Registry editing has been disabled by your administrator.

    at ChildProcess.exithandler (child_process.js:308:12)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Socket.<anonymous> (internal/child_process.js:443:11)
    at Socket.emit (events.js:314:20)
    at Pipe.<anonymous> (net.js:676:12)
    at C:\Users\j\machineId\node_modules\node-machine-id\dist\index.js:1:7964
    at ChildProcess.exithandler (child_process.js:315:5)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Socket.<anonymous> (internal/child_process.js:443:11)
    at Socket.emit (events.js:314:20)
    at Pipe.<anonymous> (net.js:676:12)

The error hints that this user does not have admin privileges to run regedit, thus is unable to obtain the unique machine ID. Is there a way of resolving this issue?

Also, thank you for building this library!

pateljoel commented 3 years ago

Hello, is there an update on this? I thought that this library does not need elevated rights for the Machine IDs (GUID) but found out that this library silently needs elevated access to regedit.

And from the features page:

...And No elevated rights required

But from my tests it needs elevated access so it is a bit misleading here, potential usecase of this failing is getting the Machine IDs of work computers.

Is there a workaround for this? Thanks.

Nokel81 commented 3 years ago

@pateljoel I have submitted a PR to fix this issue (though it might be a bit big).

semireg commented 3 years ago

@Nokel81, I am in the process of testing your patch on Windows, but heads up that your work breaks builds on macOS. Here's what I'm seeing:

An unhandled error occurred inside electron-rebuild
internal/modules/cjs/loader.js:796
    throw err;
    ^

Error: Cannot find module 'node-addon-api'
Require stack:

and... if I try to satisfy that dependency:

Error:   CC(target) Release/obj.target/nothing/../node-addon-api/nothing.o
  LIBTOOL-STATIC Release/nothing.a
warning: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: archive library: Release/nothing.a the table of contents is empty (no object file members in the library define global symbols)
  CXX(target) Release/obj.target/reg/reg.o
In file included from ../reg.cc:1:
/Users/caylan/local-dev/label-live/app/LabelLIVE-App/app/node_modules/node-addon-api/napi.h:32:6: error: Exception support not detected.       Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS.
    #error Exception support not detected. \
     ^
../reg.cc:4:10: fatal error: 'windows.h' file not found
#include <windows.h>
         ^~~~~~~~~~~
2 errors generated.
Nokel81 commented 3 years ago

Ah I am sorry about that, maybe a dynamic import for only windows would be better?

semireg commented 3 years ago

@Nokel81, I had a windows user test my app's build with your PR and it worked. So that's good news!

I'm not in a good position to modify this codebase, but if you want to take a stab at a solution (dynamic import) I'm happy to test it across macOS and Windows.

Nokel81 commented 3 years ago

Will do

Nokel81 commented 3 years ago

@semireg done

semireg commented 3 years ago

@Nokel81, did you see the latest comment over at native-reg? He's modified the gypfile to minimize complexity with bundling/packaging.

https://github.com/simonbuchan/native-reg/issues/23#issuecomment-888821548

Give this a try in @next tag / 0.3.6 - if it's all good i'll release it to latest.