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

[🚀 Feature]: Tor Browser support? #13092

Closed
MatzFan opened this issue Nov 3, 2023 · 12 comments
Closed

[🚀 Feature]: Tor Browser support? #13092

MatzFan opened this issue Nov 3, 2023 · 12 comments

Comments

@MatzFan
Copy link
Contributor

MatzFan commented Nov 3, 2023

Feature and motivation

I plan on writing a Ruby gem to support Tor Browser with Selenium and just wanted to check before I start that there are no plans or desire to have cross-language support for Tor Browser in this project. I see this has been requested before (#4239 and #7803 at least).

My motivation for raising the issue again now is the fix I have just submitted in #13090 which allows FF to use an existing profile model. This was broken in Ruby, at least on Linux. With this change merged Tor Browser can be supported relatively easily, for example, with the tor-browser package in your home dir this works:

require 'selenium-webdriver'

tor_dir = File.expand_path '~/tor-browser'
tor_binary = File.join(tor_dir, 'Browser/firefox')
profile_dir = File.join(tor_dir, 'Browser/TorBrowser/Data/Browser/profile.default')

options = Selenium::WebDriver::Firefox::Options.new
options.binary = tor_binary
profile = Selenium::WebDriver::Firefox::Profile.new profile_dir # broken, but will work on merge of #13090
profile['torbrowser.settings.quickstart.enabled'] = true # connect to tor network on startup - this would be set in DEFAULT_SETTINGS

options.profile = profile
driver = Selenium::WebDriver.for :firefox, options: options

driver.get 'about:preferences#connection'
wait = Selenium::WebDriver::Wait.new(timeout: 10)
wait.until { driver.find_element(:id, 'torPreferences-status-tor-connect').text == "Tor Network:\nConnected" } # I expect there is a better way to do this

driver.get 'https://check.torproject.org'
puts driver.title # => "Congratulations. This browser is configured to use Tor."
driver.quit

As you can see, Tor Browser-specific preferences can be set in the same way as regular FF preferences and the connection through the tor network is handled by the SOCKS server bundled with the browser install - no need to mess with FF proxy settings. Using the default profile means the 'NoScript' extension TB uses does not need to be separately installed.

I have also checked the browser fingerprint with the EFF's test tool to ensure the Selenium-driven browser retains TB's anonymity and there is one issue there, which I expect can be fixed. If it can be, Selenium-Tor would retain the anonymity of human Tor Browser users - a key requirement from prospective users of Selenium-Tor.

Usage example

You would do something like this (in Ruby):

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :tor, options: options
...

There is a demand for Tor Browser support, though I confess its size is unknown.

Copy link

github-actions bot commented Nov 3, 2023

@MatzFan, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@titusfortner
Copy link
Member

The primary reason the previous issues were closed was that the Tor browser was not able to make use of geckodriver at that time.

I think it is good for Selenium to be able to support Tor in all of the languages, it comes down to what all code is required, and what makes sense for Selenium to do and what makes sense for 3rd parties to do. The tor automation gem might be the right way to do it, depending.

What all is needed from the profile?
Can this be set by prefs instead of needing a profile? torbrowser.settings.quickstart.enabled

@MatzFan
Copy link
Contributor Author

MatzFan commented Nov 3, 2023

@titusfortner thanks for your reply. I'll investigate whether this is possible with prefs alone. The 'NoScript' extension would need to be added, but that ought to be straightforward.

@titusfortner
Copy link
Member

users can add extensions after the browser has started in Firefox, though it is being investigated how to do extensions in capabilities — mozilla/geckodriver#1476

@MatzFan
Copy link
Contributor Author

MatzFan commented Nov 3, 2023

Thanks. So it seems tor-specific prefs can't be set through the profile[.. ..] method until they are instantiated via the UI. That said it looks like you can set all the prefs you need by interacting with TB's home page and about:preferences page, e.g. in about:preferences#connection. The above code therefore still works with a new profile and a little UI interaction:

...
profile = Selenium::WebDriver::Firefox::Profile.new
options.profile = profile

driver = Selenium::WebDriver.for :firefox, options: options
# the following are on TB's default home page
driver.find_element(:id, 'quickstartCheckbox').click # sets ['torbrowser.settings.quickstart.enabled'] = true
driver.find_element(:id, 'connectButton').click
wait = Selenium::WebDriver::Wait.new(timeout: 10)
wait.until { driver.find_elements(:class, 'title-text').empty? } # tor connection made, probably a better way to do this
... # as before

@titusfortner
Copy link
Member

And geckodriver can't set that before launch with options.prefs = {'torbrowser.settings.quickstart.enabled' => true}?

@MatzFan
Copy link
Contributor Author

MatzFan commented Nov 3, 2023

OK so yes that does set the correct preference, which I verified in about:config, but it had no effect on launch - i.e. the browser did not connect automatically and the checkbox referenced above was unticked. I don't know how FF reads user prefs let alone Tor Browser so it seems we'd be left with the UI connection method above (which could be abstracted into a method called #connect I guess).

@MatzFan
Copy link
Contributor Author

MatzFan commented Nov 17, 2023

Update: Tor Browser can be supported without using a profile and by setting user prefs only. I've built a bare-bones Ruby gem to do just that: https://gitlab.com/matzfan/selenium-tor

All that is needed is 7 TB-specific prefs to be set to make your Firefox code work with TB. The only other tweak required is to wait until the browser has connected to the network. I did that by parsing the about:preferences#connection page which is unique to TB.

Happy to close this issue if Selenium don't want to take this forward.

@titusfortner
Copy link
Member

Let me ping the other devs to see if we want to implement direct support in all of the languages.

If you want to write an article for what preferences need to be set in a language agnostic way for our Blog? I'm sure there are Java devs and Python devs who are interested in working with Tor Browser.

@MatzFan
Copy link
Contributor Author

MatzFan commented Nov 17, 2023

OK show me where that goes and I'll write something up.

Python has its own library here, dunno about Java.

@titusfortner
Copy link
Member

You could PR a post here — https://github.com/SeleniumHQ/seleniumhq.github.io/tree/trunk/website_and_docs/content/blog/2023

Either way, I'm glad you got things sorted out.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants