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

Made the www-frame-origin for rserver environment-configurable #148

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Note that [RStudio Server Pro](https://rstudio.com/products/rstudio-server-pro)

This extension used to proxy Shiny server as well, however that functionality has been [separated](https://github.com/ryanlovett/jupyter-shiny-proxy).



## Installation

### Pre-reqs
Expand Down Expand Up @@ -40,3 +42,12 @@ conda install -c conda-forge jupyter-rsession-proxy
### Multiuser Considerations

This extension launches an rstudio server process from the jupyter notebook server. This is fine in JupyterHub deployments where user servers are containerized since other users cannot connect to the rstudio server port. In non-containerized JupyterHub deployments, for example on multiuser systems running LocalSpawner or BatchSpawner, this not secure. Any user may connect to rstudio server and run arbitrary code.

## Configuration with Environment Variables
The following behavior can be configured with environment variables

| Environment Variable | Effect | Default Value | Notes
| RSERVER_FRAME_ORIGIN | The value of the `www-frame-origin` flag to rserver | `same` | |
| RSERVER_TIMEOUT | Idle timeout flag to rserver in minutes | 15 | must be numeric and positive |
| RSESSION_TIMEOUT | Idle timeout flag to rsession in minutes | 15 | must be numeric and positive |
| NB_USER | Name of the Notebook user | `getuser.getpass()` ||
9 changes: 9 additions & 0 deletions jupyter_rsession_proxy.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Metadata-Version: 2.1
Name: jupyter-rsession-proxy
Version: 2.2.1
Summary: Jupyter extension to proxy RStudio
Home-page: https://github.com/jupyterhub/jupyter-rsession-proxy
Author: Ryan Lovett & Yuvi Panda
Keywords: Jupyter
Classifier: Framework :: Jupyter
License-File: LICENSE
13 changes: 13 additions & 0 deletions jupyter_rsession_proxy.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LICENSE
MANIFEST.in
README.md
setup.cfg
setup.py
jupyter_rsession_proxy/__init__.py
jupyter_rsession_proxy.egg-info/PKG-INFO
jupyter_rsession_proxy.egg-info/SOURCES.txt
jupyter_rsession_proxy.egg-info/dependency_links.txt
jupyter_rsession_proxy.egg-info/entry_points.txt
jupyter_rsession_proxy.egg-info/requires.txt
jupyter_rsession_proxy.egg-info/top_level.txt
jupyter_rsession_proxy/icons/rstudio.svg
1 change: 1 addition & 0 deletions jupyter_rsession_proxy.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions jupyter_rsession_proxy.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[jupyter_serverproxy_servers]
rstudio = jupyter_rsession_proxy:setup_rserver
1 change: 1 addition & 0 deletions jupyter_rsession_proxy.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jupyter-server-proxy!=4.0.0,!=4.1.0,>=3.2.3
1 change: 1 addition & 0 deletions jupyter_rsession_proxy.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jupyter_rsession_proxy
46 changes: 37 additions & 9 deletions jupyter_rsession_proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@
from textwrap import dedent
from urllib.parse import urlparse, urlunparse

def get_env_value(env_var_name):
'''
Get the value of os.environ[env_var_name] if it is set, else return the default.
Currently, defaults provdied are:
RSERVER_FRAME_ORIGIN: 'same'
NB_USER: getpass.getuser(),
RSERVER_TIMEOUT: 15.0,
RSESSION_TIMEOUT: 15.0
Returns: the value of the environment variable if present; the default value
if not. Returns None if the environment variable is not present and
env_var_name is not a key of the default dictionary. Returns the default
if the variable is expected to be numeric and the provided value in the
environment is non-numeric
'''
env_var_defaults = {
'RSERVER_FRAME_ORIGIN': 'some',
'NB_USER': getpass.getuser(),
'RSERVER_TIMEOUT': 15.0,
'RSESSION_TIMEOUT': 15.0
}
numeric_values = {'RSERVER_TIMEOUT', 'RSESSION_TIMEOUT'}
default_value = env_var_defaults[env_var_name] if env_var_name in env_var_defaults else None
value = os.environ.get(env_var_name, default_value)
if env_var_name in numeric_values:
# Make sure we return a numeric value
try:
return float(value)
except Exception:
return default_value
else:
return value

def get_rstudio_executable(prog):
# Find prog in known locations
Expand Down Expand Up @@ -47,12 +78,14 @@ def get_system_user():
try:
user = pwd.getpwuid(os.getuid())[0]
except:
user = os.environ.get('NB_USER', getpass.getuser())
user = get_env_value('NB_USER')
return(user)

def setup_rserver():
def _get_env(port):
return dict(USER=get_system_user())



def db_config(db_dir):
'''
Expand Down Expand Up @@ -88,7 +121,7 @@ def _get_cmd(port):
cmd = [
get_rstudio_executable('rserver'),
'--auth-none=1',
'--www-frame-origin=same',
f'--www-frame-origin={get_env_value("RSERVER_FRAME_ORIGIN")}',
'--www-port=' + str(port),
'--www-verify-user-agent=0',
'--secure-cookie-key-file=' + ntf.name,
Expand All @@ -105,15 +138,10 @@ def _get_cmd(port):

return cmd

def _get_timeout(default=15):
try:
return float(os.getenv('RSERVER_TIMEOUT', default))
except Exception:
return default

server_process = {
'command': _get_cmd,
'timeout': _get_timeout(),
'timeout': get_env_value('RSERVER_TIMEOUT'),
'environment': _get_env,
'rewrite_response': rewrite_netloc,
'launcher_entry': {
Expand Down Expand Up @@ -162,7 +190,7 @@ def _get_timeout(default=15):

return {
'command': _get_cmd,
'timeout': _get_timeout(),
'timeout': get_env_value('RSESSION_TIMEOUT'),
'environment': _get_env,
'launcher_entry': {
'title': 'RStudio',
Expand Down