hsakoh / switchbot-mqtt

SwitchBot MQTT Home Assistant add-on
MIT License
55 stars 1 forks source link

AspNetCore: An exception was thrown while deserializing the token #36

Closed hunterdrayman closed 3 months ago

hunterdrayman commented 3 months ago

On starting the add-on there's a warning that no XML encryptor key is configured, then on retrieving device config a failure status returned.

08:48:17 info: Microsoft.Hosting.Lifetime[0] Application is shutting down... 08:48:17 info: SwitchBotMqttApp.Services.MqttCoreService[0] stopped 08:48:17 info: SwitchBotMqttApp.Services.PollingService[0] stopped 08:48:17 info: SwitchBotMqttApp.Services.WebhookService[0] stopped 08:48:20 warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx} may be persisted to storage in unencrypted form. 08:48:20 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:8098 08:48:20 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:8099 08:48:20 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. 08:48:20 info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production 08:48:20 info: Microsoft.Hosting.Lifetime[0] Content root path: /app 08:50:17 fail: Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery[7] An exception was thrown while deserializing the token. Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery token could not be decrypted. ---> System.Security.Cryptography.CryptographicException: The key {xxxxx-xxxx-xxxxx-xxxxxx-xxxxxxxxx} was not found in the key ring. For more information go to https://aka.ms/aspnet/dataprotectionwarning at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status) at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData) at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken) --- End of inner exception stack trace --- at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken) at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.GetCookieTokenDoesNotThrow(HttpContext httpContext) 08:50:20 info: SwitchBotMqttApp.Logics.DeviceConfigurationManager[0] device configuration file found. 8,0

hsakoh commented 3 months ago

If you restart the add-on while displaying the Ingress page and then reload the page, a CryptographicException may occur. However, this error is not a significant issue. The system should continue to function even after reloading. Please disregard this error.

What exactly does "retrieving device config a failure status returned." indicate in terms of screen display or behavior? From the provided logs, I couldn't confirm a call to the GetDeviceListAPI. If there is an issue with the operation, could you increase the log level to trace and provide more detailed logs?

hunterdrayman commented 3 months ago

Thank you for the explanation. The web GUI works as expected - the failure status seems to have no effect on function but as I am trying out the add-on after a break of 1 month (to see whether the developer of the API has quietly fixed issue https://github.com/OpenWonderLabs/SwitchBotAPI/issues/326) I wanted to be sure the error is not be significant to that.

gschmidl commented 3 months ago

I have this same issue, but the UI also does not show anything anymore, with this exception

image

12:33:06 warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100] Unhandled exception rendering component: Error: No element is currently associated with component 1 System.InvalidOperationException: Error: No element is currently associated with component 1 at Microsoft.AspNetCore.Components.RenderTree.Renderer.InvokeRenderCompletedCallsAfterUpdateDisplayTask(Task updateDisplayTask, Int32[] updatedComponents) 12:33:06 fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111] Unhandled exception in circuit 'gNus9YXonUUqlUmuDd6UF5hzwjkKwMukxxWx0kEF784'. System.AggregateException: One or more errors occurred. (Error: No element is currently associated with component 1) ---> System.InvalidOperationException: Error: No element is currently associated with component 1 at Microsoft.AspNetCore.Components.RenderTree.Renderer.InvokeRenderCompletedCallsAfterUpdateDisplayTask(Task updateDisplayTask, Int32[] updatedComponents) --- End of inner exception stack trace ---

hsakoh commented 3 months ago

@gschmidl

The log "AspNetCore: An exception was thrown while deserializing the token" is not inherently problematic (it’s just the framework generating excessive logs).

The screen exception "Unhandled exception rendering component: Error: No element is currently associated with component 1 System.InvalidOperationException" seems to be caused by a different issue.

If the add-on logs do not provide additional information.(or If you have only just started using it recently...)

there may be a client-side issue where button-click events on the page are not being transmitted to the server. The Ingress page is created using BlazorServerApp, so a modern browser with capabilities like WebSocket support is required. What kind of browser are you using to access the page? Is it a modern browser like Chrome for PC?

When you open the following URL and press any buttons http://homeassistant:8123/hassio/ingress/cafb8c58_switchbot_mqtt Do you see any errors or issues in the console in the F11 developer tools?

Alternatively, are you using any reverse proxy, such as for connecting to Home Assistant via TLS? Since WebSocket is being used, a more complex reverse proxy configuration might be required. Please try opening the Ingress page via HTTP first. https://community.home-assistant.io/t/unable-to-connect-with-nginx-reverse-proxy/14875 https://github.com/home-assistant/addons/issues/1043 https://github.com/cajuncoding/HomeAssistant/wiki/HassIO-Websocket-configuration-via-LetsEncrypt-Docker-on-Unraid-(linuxserver-letsencrypt)

hsakoh commented 3 months ago

I have just released version v1.0.22. The output of the following two types of logs should be reduced.

warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx} may be persisted to storage in unencrypted form.
fail: Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery[7] An exception was thrown while deserializing the token. Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery token could not be decrypted. 

(However, the latter may still occur with each key rotation (every 90 days).)

gschmidl commented 3 months ago

The screen exception "Unhandled exception rendering component: Error: No element is currently associated with component 1 System.InvalidOperationException" seems to be caused by a different issue.

  • When did this start occurring? (If there was an update on a certain day, please provide that information as well.)
  • Did it work correctly before?
  • What operation causes this to happen?
  • Does the issue consistently reproduce when you repeat the same operation?
  • Can you set the log level to Trace and provide a broader range of logs?

If the add-on logs do not provide additional information.(or If you have only just started using it recently...)

there may be a client-side issue where button-click events on the page are not being transmitted to the server. The Ingress page is created using BlazorServerApp, so a modern browser with capabilities like WebSocket support is required. What kind of browser are you using to access the page? Is it a modern browser like Chrome for PC?

When you open the following URL and press any buttons http://homeassistant:8123/hassio/ingress/cafb8c58_switchbot_mqtt Do you see any errors or issues in the console in the F11 developer tools?

Alternatively, are you using any reverse proxy, such as for connecting to Home Assistant via TLS? Since WebSocket is being used, a more complex reverse proxy configuration might be required. Please try opening the Ingress page via HTTP first. https://community.home-assistant.io/t/unable-to-connect-with-nginx-reverse-proxy/14875 home-assistant/addons#1043 https://github.com/cajuncoding/HomeAssistant/wiki/HassIO-Websocket-configuration-via-LetsEncrypt-Docker-on-Unraid-(linuxserver-letsencrypt)

Hello! Thank you for the extensive reply. I am using the latest version of Edge with Home Assistant on a reverse proxy. I was previously able to access the configuration UI just fine and have not changed anything in how HA is accessed. The defined MQTT topics have worked fine since I initially set them up, so I've not had the need to access the configuration UI since then and only noticed yesterday that this problem was occurring.

The issue is consistent: I click on the button to open the web interface and get the aforementioned "An unhandled error has occurred. Reload" immediately. No matter what I click, nothing happens, including the reload button itself. As mentioned, this did not previously happen.

Here are the errors from the dev console (URL redacted). WSS connection seems to work fine.

image

hsakoh commented 3 months ago

@gschmidl I understand the following two points regarding the HTTPS connection via reverse proxy:

The cause of the issue might be either "a behavioral change due to a middleware version upgrade, which only affects TLS connections," or "a recent feature update that is causing the issue specifically with your device set."

To narrow it down, please check the following:

gschmidl commented 3 months ago

@hsakoh

hsakoh commented 3 months ago

@gschmidl Oh, I'm glad it got resolved!

I also tried installing that add-on and was able to reproduce the issue. It seems that there may be compatibility issues between add-ons that modify HTML via scripts and Blazor WebApp's server-side rendering.

It's good to know that the add-on generally works even with TLS through a reverse proxy. Please don't hesitate to provide feedback if you encounter anything else in the future.