Skip to content

Commit

Permalink
BUG - JHubApp Launcher doesn't encode usernames, can lead to 404s (ne…
Browse files Browse the repository at this point in the history
…bari-dev#241)

* Fix url encoding for launching server.

* Add new build.
  • Loading branch information
jbouder authored Apr 16, 2024
1 parent 4c7ff90 commit 665db59
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 43 deletions.
52 changes: 26 additions & 26 deletions jhub_apps/static/js/index.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions ui/src/data/jupyterhub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const servicesFull: JhServiceFull[] = [
{
display: true,
info: {
name: 'Service 1',
url: 'http://service1.com/[USER]',
name: 'Environments',
url: 'http://service1.com/service1',
external: true,
},
prefix: '/services',
Expand All @@ -80,8 +80,8 @@ export const servicesFull: JhServiceFull[] = [
{
display: false,
info: {
name: 'Service 2',
url: 'http://service2.com/[USER]',
name: 'JupyterLab',
url: 'http://service2.com/service2',
external: false,
},
prefix: '/services',
Expand All @@ -96,8 +96,8 @@ export const servicesFull: JhServiceFull[] = [
{
display: true,
info: {
name: 'Service 3',
url: 'http://service3.com/[USER]',
name: 'VSCode',
url: 'http://service3.com/service3',
external: false,
},
prefix: '/services',
Expand Down
27 changes: 24 additions & 3 deletions ui/src/utils/jupyterhub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
filterAndSortApps,
getAppLogoUrl,
getApps,
getEncodedServerUrl,
getFriendlyDateStr,
getFriendlyDisplayName,
getFriendlyFrameworkName,
Expand Down Expand Up @@ -51,10 +52,10 @@ describe('JupyterHub utils', () => {
const result = getServices(servicesFull, user);
expect(result.length).toEqual(2);
expect(result[0]).toEqual({
name: 'Service 1',
url: 'http://service1.com/testUser',
name: 'Environments',
url: 'http://service1.com/service1',
external: true,
pinned: false,
pinned: true,
});
});

Expand All @@ -80,6 +81,26 @@ describe('JupyterHub utils', () => {
expect(result.length).toEqual(5);
});

test('returns a jupyterhub friendly url for JupyterLab with no encoding needed', () => {
const result = getEncodedServerUrl('testuser', 'lab');
expect(result).toBe('/hub/user/testuser/lab');
});

test('returns a jupyterhub friendly url for JupyterLab with encoding needed', () => {
const result = getEncodedServerUrl('[email protected]', 'lab');
expect(result).toBe('/hub/user/testuser%[email protected]/lab');
});

test('returns a jupyterhub friendly url for VSCode with no encoding needed', () => {
const result = getEncodedServerUrl('testuser', 'vscode');
expect(result).toBe('/hub/user/testuser/vscode');
});

test('returns a jupyterhub friendly url for VSCode with encoding needed', () => {
const result = getEncodedServerUrl('[email protected]', 'vscode');
expect(result).toBe('/hub/user/testuser%[email protected]/vscode');
});

test('returns a friendly display name with no trailing spaces', () => {
const result = getFriendlyDisplayName('Test App 1 ');
expect(result).toBe('Test App 1');
Expand Down
32 changes: 24 additions & 8 deletions ui/src/utils/jupyterhub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ export const getServices = (services: JhServiceFull[], user: string) => {
if (Object.hasOwnProperty.call(services, key)) {
const service = services[key];
if (service.display === true && service.info.name) {
jhServices.push({
name: service.info.name,
url: service.info.url?.replace('[USER]', user),
external: service.info.external,
pinned: DEFAULT_PINNED_SERVICES.includes(service.info.name),
});
const serviceInfo = service.info;
if (serviceInfo.url) {
let url = serviceInfo.url;
const name = serviceInfo.name;
if (name === 'VSCode' || name === 'JupyterLab') {
url = getEncodedServerUrl(user, name);
}
jhServices.push({
name: name,
url: url,
external: serviceInfo.external,
pinned: DEFAULT_PINNED_SERVICES.includes(name),
});
}
}
}
}
Expand Down Expand Up @@ -115,7 +123,7 @@ export const getPinnedApps = (servers: any, username: string) => {
name: 'JupyterLab',
description: 'This is your default JupyterLab server.',
framework: 'JupyterLab',
url: `/hub/user/${username}/lab`,
url: getEncodedServerUrl(username, 'lab'),
thumbnail: JUPYTER_LOGO,
username: username,
ready: defaultApp.ready,
Expand All @@ -131,13 +139,21 @@ export const getPinnedApps = (servers: any, username: string) => {
name: 'VSCode',
description: 'This is your default VSCode server.',
framework: 'VSCode',
url: `/hub/user/${username}/vscode`,
url: getEncodedServerUrl(username, 'vscode'),
thumbnail: VSCODE_LOGO,
});
}
return pinnedApps;
};

/**
* Get a server URL for the given username and server type, with required jupyterhub encoding.
*/
export const getEncodedServerUrl = (username: string, serverType: string) => {
const encodedUsername = username.replace(/\+/g, '%2B'); // Encode '+' with url encoding
return `/hub/user/${encodedUsername}/${serverType.toLowerCase()}`;
};

export const getFriendlyDisplayName = (name: string) => {
return name.replace(/\//g, '').trim();
};
Expand Down

0 comments on commit 665db59

Please sign in to comment.