Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Guid.NewGuid() fails with Error occurred during a cryptographic operation. in Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length) with node < 19.x #77927

Closed
benjineering opened this issue Nov 4, 2022 · 9 comments · Fixed by #78696
Assignees
Milestone

Comments

@benjineering
Copy link

benjineering commented Nov 4, 2022

Description

When creating a new GUID in a .NET 7 WASM console app, I'm getting a runtime error.

Reproduction Steps

  1. Run dotnet new wasmconsole
  2. Insert var guid = Guid.NewGuid(); before the console log statement
  3. Run dotnet build -c Debug
  4. Run node .\bin\Debug\net7.0\browser-wasm\AppBundle\main.mjs

Expected behavior

No error expected.

Actual behavior

   at Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length)
   at System.Guid.NewGuid()
   at Program.<Main>$(String[] args) in C:\Users\ben\OneDrive\Desktop\wasm-error\Program.cs:line 4
   at System.Reflection.MethodInvoker.InterpretedInvoke(Object obj, Span`1 args, BindingFlags invokeAttr)
Error: Error occurred during a cryptographic operation.

Regression?

No response

Known Workarounds

No response

Configuration

  • dotnet 7.0.100-rc.2.22477.23
  • node v17.3.0
  • Windows 10 x64

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 4, 2022
@vcsjones vcsjones added arch-wasm WebAssembly architecture area-System.Runtime labels Nov 4, 2022
@ghost
Copy link

ghost commented Nov 4, 2022

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

When creating a new GUID in a .NET 7 WASM console app, I'm getting a runtime error.

Reproduction Steps

  1. Run dotnet new wasmconsole
  2. Insert var guid = Guid.NewGuid(); before the console log statement
  3. Run dotnet run -c Debug

Expected behavior

No error expected.

Actual behavior

   at Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length)
   at System.Guid.NewGuid()
   at Program.<Main>$(String[] args) in C:\Users\ben\OneDrive\Desktop\wasm-error\Program.cs:line 4
   at System.Reflection.MethodInvoker.InterpretedInvoke(Object obj, Span`1 args, BindingFlags invokeAttr)
Error: Error occurred during a cryptographic operation.

Regression?

No response

Known Workarounds

No response

Configuration

  • dotnet 7.0.100-rc.2.22477.23
  • node v17.3.0
  • Windows 10 x64

Other information

No response

Author: benjineering
Assignees: -
Labels:

arch-wasm, area-System.Runtime, untriaged

Milestone: -

@benjineering
Copy link
Author

Still happening with dotnet 7.0.100

@marek-safar marek-safar removed the untriaged New issue has not been triaged by the area owner label Nov 21, 2022
@marek-safar marek-safar added this to the 8.0.0 milestone Nov 21, 2022
@radical
Copy link
Member

radical commented Nov 21, 2022

This works fine with https://dotnet.microsoft.com/en-us/download/dotnet/7.0 .

 $ dotnet run
WasmAppHost --runtime-config /private/tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle/cc.runtimeconfig.json
Running: node main.mjs
Using working directory: /private/tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle
mono_wasm_runtime_ready fe00e07a-5519-4dfe-b35a-f867dbaf2e28
Hello, World! Greetings from node version: v19.1.0
5ef0c39c-e92c-4e63-96d2-76aae845c8cd
Hello, Console!
using System;
using System.Runtime.InteropServices.JavaScript;

Console.WriteLine ($"{Guid.NewGuid()}");
Console.WriteLine("Hello, Console!");

return 0;

public partial class MyClass
{
    [JSExport]
    internal static string Greeting()
    {
        var text = $"Hello, World! Greetings from node version: {GetNodeVersion()}";
        return text;
    }

    [JSImport("node.process.version", "main.mjs")]
    internal static partial string GetNodeVersion();
}

@benjineering can you try with 7.0? Maybe this was broken with 7.0-rc2.

@radical radical added the needs-author-action An issue or pull request that requires more info or actions from the author. label Nov 21, 2022
@ghost
Copy link

ghost commented Nov 21, 2022

This issue has been marked needs-author-action and may be missing some important information.

@benjineering
Copy link
Author

@radical I did try with 7.0.100, but got the same error. However, I noticed you are using the latest version of Node.

I had tried with Node v17.3.0 and v18.6.0 which both threw that same error, but using v19.1.0 (which you appear to have) seems to work.

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Nov 21, 2022
@radical
Copy link
Member

radical commented Nov 21, 2022

You are correct. With v14.18.2 included in the wasm-tools workload:

 $ ~/dev/runtime/src/mono/wasm/emsdk/node/14.18.2_64bit/bin/node main.mjs
mono_wasm_runtime_ready fe00e07a-5519-4dfe-b35a-f867dbaf2e28
Hello, World! Greetings from node version: v14.18.2
   at Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length)
   at System.Guid.NewGuid()
   at Program.<Main>$(String[] args) in /private/tmp/guidc/Program.cs:line 4
   at System.Reflection.MethodInvoker.InterpretedInvoke(Object obj, Span`1 args, BindingFlags invokeAttr)
Error: Error occurred during a cryptographic operation.
    at jo (file:///private/tmp/guidc/bin/Debug/net7.0/browser-wasm/AppBundle/dotnet.js:5:38027)
    at Ws (file:///private/tmp/guidc/bin/Debug/net7.0/browser-wasm/AppBundle/dotnet.js:5:55819)
    at Object.b.javaScriptExports.call_entry_point (file:///private/tmp/guidc/bin/Debug/net7.0/browser-wasm/AppBundle/dotnet.js:5:58546)
    at Te (file:///private/tmp/guidc/bin/Debug/net7.0/browser-wasm/AppBundle/dotnet.js:5:2713)
    at Object.Re [as runMainAndExit] (file:///private/tmp/guidc/bin/Debug/net7.0/browser-wasm/AppBundle/dotnet.js:5:2468)
    at Wc.run (file:///private/tmp/guidc/bin/Debug/net7.0/browser-wasm/AppBundle/dotnet.js:5:108293)
    at async file:///private/tmp/guidc/bin/Debug/net7.0/browser-wasm/AppBundle/main.mjs:23:1
exiting due to exception: Error: Error occurred during a cryptographic operation.

.. and the same files with 19.1.0:

 $ node main.mjs
mono_wasm_runtime_ready fe00e07a-5519-4dfe-b35a-f867dbaf2e28
Hello, World! Greetings from node version: v19.1.0
838bbf31-0311-4d20-ab2d-81460dedc9f5

cc @pavelsavara @marek-safar

@radical radical changed the title Error when creating a new Guid in a WASM console project [wasm] Guid.NewGuid() fails with Error occurred during a cryptographic operation. in Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length) with node < 19.x Nov 21, 2022
@radical radical changed the title [wasm] Guid.NewGuid() fails with Error occurred during a cryptographic operation. in Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length) with node < 19.x [wasm] Guid.NewGuid() fails with Error occurred during a cryptographic operation. in Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length) with node < 19.x Nov 21, 2022
@radical radical changed the title [wasm] Guid.NewGuid() fails with Error occurred during a cryptographic operation. in Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length) with node < 19.x [wasm] Guid.NewGuid() fails with Error occurred during a cryptographic operation. in Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length) with node < 19.x Nov 21, 2022
@pavelsavara pavelsavara added area-System.Runtime.InteropServices.JavaScript and removed needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration area-System.Runtime.InteropServices.JavaScript area-System.Runtime labels Nov 22, 2022
@pavelsavara
Copy link
Member

pavelsavara commented Nov 22, 2022

GetCryptographicallySecureRandomBytes is implemented as dotnet_browser_entropy which rely on crypto API, which is available in NodeJS v 19.0

Secure solution for old Node would probably need some npm library with native module.
This looks promising, but it's getting old quickly https://github.com/PeculiarVentures/node-webcrypto-p11

For node above 15.0 you can do

if (typeof globalThis.crypto === 'undefined') {
    globalThis.crypto = require('node:crypto').webcrypto;
}

Should we do that ^^ on behalf of the users ? @maraf

For under NodeJS 15.0 simple and unsecure workaround is

if (typeof globalThis.crypto === 'undefined') {
    // **NOTE** this is a simple insecure polyfill
    globalThis.crypto = {
        getRandomValues: function (buffer) {
            for (let i = 0; i < buffer.length; i++)
                buffer[i] = (Math.random() * 256) | 0;
        }
    }
}

@pavelsavara
Copy link
Member

For node 14.18.2 there is globalThis.crypto.randomBytes() which could be probably used.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Nov 22, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 23, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Dec 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants