onionshare / onionshare

Securely and anonymously share files, host websites, and chat with friends using the Tor network
https://onionshare.org/
Other
6.15k stars 635 forks source link

Crash when using the Censorship API #1588

Closed micahflee closed 2 years ago

micahflee commented 2 years ago

I came across is bug while taking screenshots for the documentation. I disconnected from the internet, opened OnionShare, and tried to connect. Once it failed, I reconnected to the internet and then clicked "Automatically determine my country from my IP address for bridge settings".

[May 30 2022 01:42:31 PM] Settings.__init__
[May 30 2022 01:42:31 PM] Settings.load
[May 30 2022 01:42:31 PM] Onion.__init__
[May 30 2022 01:42:31 PM] GuiCommon.get_tor_paths: using paths in resources
[May 30 2022 01:42:31 PM] Meek.__init__
[May 30 2022 01:42:31 PM] GuiCommon.get_tor_paths: using paths in resources
[May 30 2022 01:42:31 PM] MainWindow.__init__
[May 30 2022 01:42:31 PM] TabWidget.__init__
[May 30 2022 01:42:31 PM] EventHandlerThread.__init__
[May 30 2022 01:42:31 PM] TabWidget.open_connection_tab
[May 30 2022 01:42:31 PM] EventHandlerThread.run[May 30 2022 01:42:31 PM] AutoConnectTab.__init__

[May 30 2022 01:42:31 PM] Settings.__init__
[May 30 2022 01:42:31 PM] Settings.load
[May 30 2022 01:42:31 PM] AutoConnectFirstLaunchWidget.__init__
[May 30 2022 01:42:31 PM] AutoConnectUseBridgeWidget.__init__
[May 30 2022 01:42:31 PM] TorConnectionWidget.__init__
[May 30 2022 01:42:31 PM] TabWidget.tab_changed: Tab was changed to 0
[May 30 2022 01:42:31 PM] AutoConnectTab.autoconnect_checking
[May 30 2022 01:44:09 PM] AutoConnectTab.first_launch_widget_connect_clicked
[May 30 2022 01:44:09 PM] TorConnectionWidget.start
[May 30 2022 01:44:09 PM] TorConnectionThread.__init__
[May 30 2022 01:44:09 PM] TorConnectionThread.run
[May 30 2022 01:44:09 PM] Onion.connect: connection_type=bundled
[May 30 2022 01:44:09 PM] Onion.connect: tor_data_directory_name=/home/micah/.config/onionshare/tor_data
[May 30 2022 01:44:09 PM] Common.get_resource_path: filename=torrc_template, path=/home/micah/code/onionshare/cli/onionshare_cli/resources/torrc_template
[May 30 2022 01:44:09 PM] Onion.connect: Writing torrc template file
[May 30 2022 01:44:09 PM] Onion.connect: starting /home/micah/code/onionshare/desktop/onionshare/resources/tor/tor subprocess
[May 30 2022 01:44:09 PM] Onion.connect: tor pid: 9845
[May 30 2022 01:44:11 PM] Onion.connect: authenticating to tor controller
Connecting to the Tor network: 0% - Starting
Taking too long to connect to Tor. Maybe you aren't connected to the Internet, or have an inaccurate system clock?
[May 30 2022 01:46:09 PM] TorConnectionThread.run: caught exception: Taking too long to connect to Tor. Maybe you aren't connected to the Internet, or have an inaccurate system clock?
[May 30 2022 01:46:09 PM] TorConnectionWidget._error_connecting_to_tor
True
[May 30 2022 02:01:26 PM] AutoConnectTab.use_bridge_connect_clicked: Trying to automatically obtain bridges
[May 30 2022 02:01:26 PM] Meek.start: Starting meek client
[May 30 2022 02:01:26 PM] Meek.start: Meek running on 127.0.0.1:42495

[May 30 2022 02:01:26 PM] CensorshipCircumvention.__init__
[May 30 2022 02:01:26 PM] CensorshipCircumvention.__init__: Using Meek with CensorshipCircumvention API
[May 30 2022 02:01:28 PM] Meek.cleanup
Traceback (most recent call last):
  File "/home/micah/code/onionshare/desktop/onionshare/connection_tab.py", line 263, in use_bridge_connect_clicked
    if bridge_settings and self.censorship_circumvention.save_settings(
  File "/home/micah/code/onionshare/cli/onionshare_cli/censorship.py", line 228, in save_settings
    bridges = bridge_settings["settings"][0]["bridges"]
TypeError: 'NoneType' object is not subscriptable
micahflee commented 2 years ago

I made it log the value of bridge_settings and then reproduced the crash:

[May 30 2022 02:09:01 PM] CensorshipCircumvention.save_settings: bridge_settings: {'settings': None, 'country': 'us'}
Traceback (most recent call last):
  File "/home/micah/code/onionshare/desktop/onionshare/connection_tab.py", line 263, in use_bridge_connect_clicked
    if bridge_settings and self.censorship_circumvention.save_settings(
  File "/home/micah/code/onionshare/cli/onionshare_cli/censorship.py", line 234, in save_settings
    bridges = bridge_settings["settings"][0]["bridges"]
TypeError: 'NoneType' object is not subscriptable

It looks like if you're in the US (or other non-censored countries, presumably), the Censorship API returns None for bridge settings.

micahflee commented 2 years ago

Working on fixing this. It looks like instead of not returning a settings key, it returns {"settings": None}, so the fix it to check for both:

-            if not "settings" in result:
+            if not "settings" in result or result["settings"] is None:
mig5 commented 2 years ago

Ahh, yes, I think it was due to this upstream change: https://gitlab.torproject.org/tpo/anti-censorship/rdsys/-/issues/108#note_2800689 . They used to return just an empty {} if no bridges to offer. Now they always return a populated dict with a country key (whether or not there are bridges to offer), and therefore, they also return settings: [] in that scenario.

I actually wrote there that I didn't think it would cause a problem for us - looks like I was wrong!

Though I am still confused as I thought an empty [] would be treated as False in Python, apparently not.. at least not if it was a JSON []..?

micahflee commented 2 years ago
>>> [] == False
False
>>> [] == None
False
>>> False == None
False
>>> None == None
True
>>> if []:
...   print("true?")
... else:
...   print("false?")
...
false?
>>>

:)