rstudio / chromote

Chrome Remote Interface for R
https://rstudio.github.io/chromote/
156 stars 20 forks source link

Connecting to a website with a non valid certificate #152

Open sybrohee opened 8 months ago

sybrohee commented 8 months ago

I am usine chromote (in combination with R/Selenider) to develop automated tests for my Shiny application. I turns out that our application is hosted on a server that has a non secure SSL certificate, which causes the following error when using a very classical command :

b <- chromote::ChromoteSession$new()
b$parent$debug_messages(TRUE)
b$Page$navigate("https://my/insecure/app")
SEND {"method":"Page.navigate","params":{"url":"https://my/insecure/app"},"id":5,"sessionId":"9597ADF59FEED0D77E99487C0FAF311F"}

Error: Chromote: timed out waiting for response to command Page.navigate

RECV {"id":5,"result":{"frameId":"21946B35105245A73E9BFA130A44E0E1","loaderId":"2D2B47BE437785A50AC300CE518D5FB6","errorText":"net::ERR_CERT_AUTHORITY_INVALID"},"sessionId":"9597ADF59FEED0D77E99487C0FAF311F"}

In chrome, I can bypass this error by stating that I accept the certificate whatever error it contains. How would you do that with chromote?

I've been struggling a lot for 3 days, any insight would be much appreciated.

gadenbuie commented 8 months ago

Does it work if you navigate to http://my.insecure.app instead (using http instead of https)?

sybrohee commented 8 months ago

It would have been so nice if it had been the case. Unfortunately, I just tested it and I get the same error. Thanks for your advice, though.

gadenbuie commented 8 months ago

Bummer, but good news, I think I have a solution (or at least something to try). Chrome has an --ignore-certificate-errors flag you can use. Try this code:

library(chromote)

chrome <- Chrome$new(
  path = find_chrome(),
  args = paste(default_chrome_args(), "--ignore-certificate-errors")
)
chromote <- Chromote$new(browser = chrome)
session  <- ChromoteSession$new(parent = chromote)

session$parent$debug_messages(TRUE)
session$Page$navigate("https://my.insecure.app")
sybrohee commented 8 months ago

Hello. First of all, thank you for the time you spent looking to my problems.

Your solution helped me a lot but was not working immediately as I forgot to mention that I am working within a non privileged docker container.

When running you code. This is what I have :

Error in `with_random_port()`:
! Cannot find an available port. Please try again.
Caused by error in `startup()`:
! Failed to start chrome. Error: Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
[0320/140248.630175:FATAL:zygote_host_impl_linux.cc(202)] Check failed: . : Operation not permitted (1)
[0320/140248.636731:ERROR:file_io_posix.cc(145)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[0320/140248.636794:ERROR:file_io_posix.cc(145)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Run `rlang::last_trace()` to see where the error occurred.

Working within a privileged container seemed to deliver the expected results if

  1. I increase the timeout time of chromote (not of chrome)
  2. I do not use the default parameters. So, basically, my command is the following :
library(chromote)

chrome <- Chrome$new(
  path = find_chrome(),
  args = paste( "--ignore-certificate-errors")
)
chromote <- Chromote$new(browser = chrome)
session  <- ChromoteSession$new(parent = chromote)

session$parent$debug_messages(TRUE)
session$Page$navigate("https://my.insecure.app", timeout = 50000)

However, as I prefer not work within a privileged docker container, using the following seems the work (I know it is ugly, but after one week, this is the only solution that came to my mind)

library(chromote)
temp_file <- tempfile(pattern = "mychrome", fileext = ".sh", tmpdir = ".")
writeLines(c('#!/bin/bash', '/usr/bin/google-chrome  --ignore-certificate-errors  "$@"'), con = temp_file)
system(paste0("chmod +x ", temp_file))
chromote <- Chromote$new(browser = Chrome$new(path = temp_file)) 
session  <- ChromoteSession$new(parent = chromote)
session$parent$debug_messages(TRUE)
session$Page$navigate("https://my.insecure.app", timeout = 50000)

I am pretty sure there are other ways to deal with my issue but at least, this will allow me to continue working.