Skip to content

WASM error handling in SubtleCrypto through web worker #69740

Closed

Description

@AaronRobinsonMSFT

The work to enable WASM to use SubtleCrypto, #65966, had some follow ups on how to handle error cases. Specifically, the communication channel with the web worker could be hardened to convey more errors/failures. This could be accomplished through throwing an exception or the web worker communicating a failure case explicitly.

Source hints:

  • crypto-worker.ts
    • see read_response and consider throwing an error.
  • dotnet-crypto-worker.js
    • The reading in of a request could be including in the try/catch in await_request.
    • The await_request function could return a bespoke "error occurred" message instead of an exception string.

See #69741

/cc @radical @ericstj @layomia

Activity

  1. added this to the 7.0.0 milestone on May 24, 2022
  2. radical commented on May 24, 2022

    @radical
    Member

    Another suggestion would be for the tests to confirm when running on browser that it did actually use the worker implementation.

  3. AaronRobinsonMSFT commented on May 24, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    Another suggestion would be for the tests to confirm when running on browser that it did actually use the worker implementation.

    I think this is being done. See src/tests/BuildWasmApps/Wasm.Build.Tests/NativeLibraryTests.cs. There is a check for console output. I commented on this in the JavaScript file.

  4. radical commented on May 24, 2022

    @radical
    Member

    I think this is being done. See src/tests/BuildWasmApps/Wasm.Build.Tests/NativeLibraryTests.cs. There is a check for console output. I commented on this in the JavaScript file.

    Yes, but the xharness webserver is setup in two different places for Wasm.Build.Tests, and the library tests. I was just suggesting that we add some kinda confirmation for the library tests also, so it doesn't accidentally start using the fallback, and we don't find out.

  5. eerhardt commented on Jun 7, 2022

    @eerhardt
    Member

    Note: when fixing this, we also need to ensure that the "lock" added in #70185 is cleared on error.

  6. lewing commented on Jul 25, 2022

    @lewing
    Member

    What is left to do here?

  7. radical commented on Jul 25, 2022

    @radical
    Member

    I think this is being done. See src/tests/BuildWasmApps/Wasm.Build.Tests/NativeLibraryTests.cs. There is a check for console output. I commented on this in the JavaScript file.

    Yes, but the xharness webserver is setup in two different places for Wasm.Build.Tests, and the library tests. I was just suggesting that we add some kinda confirmation for the library tests also, so it doesn't accidentally start using the fallback, and we don't find out.

    Only this, if @eerhardt thinks we should do it.

  8. eerhardt commented on Jul 26, 2022

    @eerhardt
    Member

    that we add some kinda confirmation for the library tests

    @radical - do you know how we would do this in the libraries tests?

  9. radical commented on Jul 26, 2022

    @radical
    Member

    @radical - do you know how we would do this in the libraries tests?

    One simple check would be to call Interop.BrowserCrypto.CanUseSubtleCrypto via reflection, and compare it against an environment variable which indicates whether we are expecting to use subtlecrypto. We can do this as:

    1. Add a separate test that does this
    2. Or do this is all the relevant test class' .ctor . This would catch the case where some tests do use subtlecrypto, then one causes an error, and the subsequent tests would end up using the managed implementation. This can work because, once we hit an error which disables the use of subtlecrypto, Interop.BrowserCrypto.CanUseSubtleCrypto should return false.

    worker.onerror = event => {
    console.warn(`MONO_WASM: Error in Crypto WebWorker. Cryptography digest calls will fallback to managed implementation. Error: ${event.message}`);
    mono_wasm_crypto = null;
    };

  10. eerhardt commented on Jul 26, 2022

    @eerhardt
    Member

    This can work because, once we hit an error which disables the use of subtlecrypto, Interop.BrowserCrypto.CanUseSubtleCrypto should return false.

    This is actually a problem in the current code, because of the following C# code:

    internal static readonly bool CanUseSubtleCrypto = CanUseSubtleCryptoImpl() == 1;

    This only gets called once, and then cached in the field. So if an error happens subsequently, we don't P/Invoke back into JS code to see if mono_wasm_crypto is null or not.

    How fast is the P/Invokes in WASM? Should this CanUseSubtleCrypto always call into the JS code every time it is invoked?

  11. bartonjs commented on Jul 26, 2022

    @bartonjs
    Member

    This can work because, once we hit an error which disables the use of subtlecrypto, Interop.BrowserCrypto.CanUseSubtleCrypto should return false.

    I dunno. I think (in all sincerity) we should just let all crypto operations blow up.

  12. added a commit that references this issue on Jul 26, 2022
    7fa872c
  13. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 26, 2022
  14. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 27, 2022
  15. ghost locked as resolved and limited conversation to collaborators on Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions