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 error handling in SubtleCrypto through web worker #69740

Closed
Tracked by #40074
AaronRobinsonMSFT opened this issue May 24, 2022 · 11 comments · Fixed by #72892
Closed
Tracked by #40074

WASM error handling in SubtleCrypto through web worker #69740

AaronRobinsonMSFT opened this issue May 24, 2022 · 11 comments · Fixed by #72892
Labels
arch-wasm WebAssembly architecture area-System.Security
Milestone

Comments

@AaronRobinsonMSFT
Copy link
Member

AaronRobinsonMSFT commented May 24, 2022

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

@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 7.0.0 milestone May 24, 2022
@ghost
Copy link

ghost commented May 24, 2022

Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

/cc @radical @ericstj @layomia

Author: AaronRobinsonMSFT
Assignees: -
Labels:

arch-wasm, area-System.Security

Milestone: 7.0.0

@radical
Copy link
Member

radical commented May 24, 2022

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

@AaronRobinsonMSFT
Copy link
Member Author

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.

@radical
Copy link
Member

radical commented May 24, 2022

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.

@eerhardt
Copy link
Member

eerhardt commented Jun 7, 2022

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

eerhardt added a commit to eerhardt/runtime that referenced this issue Jul 5, 2022
Handle exceptions from SubtleCrypto by catching and logging exceptions coming from the crypto stack.

Contributes to dotnet#69740
eerhardt added a commit to eerhardt/runtime that referenced this issue Jul 6, 2022
Handle exceptions from SubtleCrypto by catching and logging exceptions coming from the crypto stack.

Reset web worker when a request fails.

Also, fix race conditions where the web worker can read its own response as part of the next request.

Contributes to dotnet#69740
eerhardt added a commit that referenced this issue Jul 7, 2022
* Better error handling in SubtleCrypto workers

Handle exceptions from SubtleCrypto by catching and logging exceptions coming from the crypto stack.

Reset web worker when a request fails.

Also, fix race conditions where the web worker can read its own response as part of the next request.

Contributes to #69740
@lewing
Copy link
Member

lewing commented Jul 25, 2022

What is left to do here?

@radical
Copy link
Member

radical commented Jul 25, 2022

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.

@eerhardt
Copy link
Member

that we add some kinda confirmation for the library tests

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

@radical
Copy link
Member

radical commented Jul 26, 2022

@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;
};

@eerhardt
Copy link
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?

@bartonjs
Copy link
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.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 26, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 27, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-System.Security
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants