From e6896bb5f2f23ef48f6362b0cfba036f9a162b65 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Wed, 27 Apr 2022 23:06:46 +0100 Subject: [PATCH 1/5] Escape colon in JupyterHub link to repo I've run into a problem using The Littlest JupyterHub with current `sphinx-book-theme`. At the moment the generated URLs for JupyterHub are of form: ``` /hub/user-redirect/git-pull?repo=https://&urlpath=tree/ ``` These work fine on a modern Kubernetes JupyterHub, I guess because of a recent version of JupyterHub responding to the query. But, on current The Littlest JupyterHub, still on JupyterHub 1.5.0, this causes Nbgitpuller to stall if the user is not already logged in, with log messages of form: ``` Disallowing redirect outside JupyterHub: '/hub/user-redirect/git-pull?repo=https://github.com/&urlpath=tree/'. ``` Once logged in, retrying the same link works correctly. This can be fixed by escaping the `:` in the Nbgitpuller URL with a `%3A`, in this PR. --- src/sphinx_book_theme/header_buttons/launch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sphinx_book_theme/header_buttons/launch.py b/src/sphinx_book_theme/header_buttons/launch.py index 51c692b2..8a71091a 100644 --- a/src/sphinx_book_theme/header_buttons/launch.py +++ b/src/sphinx_book_theme/header_buttons/launch.py @@ -118,9 +118,10 @@ def add_launch_buttons( ) if jupyterhub_url: + repo_esc = repo_url.replace(':', '%3A') url = ( f"{jupyterhub_url}/hub/user-redirect/git-pull?" - f"repo={repo_url}&urlpath={ui_pre}/{repo}/{path_rel_repo}&branch={branch}" + f"repo={repo_esc}&urlpath={ui_pre}/{repo}/{path_rel_repo}&branch={branch}" ) launch_buttons_list.append( { From 910bd9782a341143878ec6acfd544ea47da83f5c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Apr 2022 09:59:31 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sphinx_book_theme/header_buttons/launch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sphinx_book_theme/header_buttons/launch.py b/src/sphinx_book_theme/header_buttons/launch.py index 8a71091a..430fe7ab 100644 --- a/src/sphinx_book_theme/header_buttons/launch.py +++ b/src/sphinx_book_theme/header_buttons/launch.py @@ -118,7 +118,7 @@ def add_launch_buttons( ) if jupyterhub_url: - repo_esc = repo_url.replace(':', '%3A') + repo_esc = repo_url.replace(":", "%3A") url = ( f"{jupyterhub_url}/hub/user-redirect/git-pull?" f"repo={repo_esc}&urlpath={ui_pre}/{repo}/{path_rel_repo}&branch={branch}" From b367eed3258185de15bab2afe43bef96ca19e6cc Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Thu, 28 Apr 2022 11:25:01 +0100 Subject: [PATCH 3/5] Adapt tests to URL escape Replace : with %3A in test files, to match escaping behavior. Note: the file `test_topbar_launchbtns.html` does not appear to have tests that check the URL. --- tests/test_build/build__header-article.html | 2 +- tests/test_build/test_header_launchbtns.html | 2 +- tests/test_build/test_topbar_launchbtns.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_build/build__header-article.html b/tests/test_build/build__header-article.html index 153bf568..74bc3326 100644 --- a/tests/test_build/build__header-article.html +++ b/tests/test_build/build__header-article.html @@ -27,7 +27,7 @@
  • - + diff --git a/tests/test_build/test_header_launchbtns.html b/tests/test_build/test_header_launchbtns.html index d6e8d12d..6ab4a501 100644 --- a/tests/test_build/test_header_launchbtns.html +++ b/tests/test_build/test_header_launchbtns.html @@ -16,7 +16,7 @@
  • - + diff --git a/tests/test_build/test_topbar_launchbtns.html b/tests/test_build/test_topbar_launchbtns.html index d6e8d12d..6ab4a501 100644 --- a/tests/test_build/test_topbar_launchbtns.html +++ b/tests/test_build/test_topbar_launchbtns.html @@ -16,7 +16,7 @@
  • - + From b859ff983de9822ef898d09c35eac28af623fa90 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Mon, 2 May 2022 10:41:17 +0100 Subject: [PATCH 4/5] Use urlencode from suggestion by MinRK Generalize URL encoding using urlencode function. --- src/sphinx_book_theme/header_buttons/launch.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/sphinx_book_theme/header_buttons/launch.py b/src/sphinx_book_theme/header_buttons/launch.py index 430fe7ab..95665f15 100644 --- a/src/sphinx_book_theme/header_buttons/launch.py +++ b/src/sphinx_book_theme/header_buttons/launch.py @@ -1,5 +1,6 @@ from pathlib import Path from typing import Any, Dict, Optional +from urllib.parse import urlencode from docutils.nodes import document from sphinx.application import Sphinx @@ -118,11 +119,11 @@ def add_launch_buttons( ) if jupyterhub_url: - repo_esc = repo_url.replace(":", "%3A") - url = ( - f"{jupyterhub_url}/hub/user-redirect/git-pull?" - f"repo={repo_esc}&urlpath={ui_pre}/{repo}/{path_rel_repo}&branch={branch}" - ) + url_params = urlencode(dict( + repo=repo_url, + urlpath=f"{ui_pre}/{repo}/{path_rel_repo}", + branch=branch), safe="/") + url = f"{jupyterhub_url}/hub/user-redirect/git-pull?{url_params}" launch_buttons_list.append( { "type": "link", From 823e1a40a461958fd67d7f75f41aec8bbd00464b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 May 2022 09:46:50 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sphinx_book_theme/header_buttons/launch.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/sphinx_book_theme/header_buttons/launch.py b/src/sphinx_book_theme/header_buttons/launch.py index 95665f15..c5b220b0 100644 --- a/src/sphinx_book_theme/header_buttons/launch.py +++ b/src/sphinx_book_theme/header_buttons/launch.py @@ -119,10 +119,12 @@ def add_launch_buttons( ) if jupyterhub_url: - url_params = urlencode(dict( - repo=repo_url, - urlpath=f"{ui_pre}/{repo}/{path_rel_repo}", - branch=branch), safe="/") + url_params = urlencode( + dict( + repo=repo_url, urlpath=f"{ui_pre}/{repo}/{path_rel_repo}", branch=branch + ), + safe="/", + ) url = f"{jupyterhub_url}/hub/user-redirect/git-pull?{url_params}" launch_buttons_list.append( {