Skip to content

Commit

Permalink
encode URI paths with slashes
Browse files Browse the repository at this point in the history
It's fine for query parameters to have visible / in them, and it makes it easier to work with the ungit URLs
  • Loading branch information
wmertens committed May 27, 2020
1 parent 2454e95 commit a71033b
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 18 deletions.
5 changes: 3 additions & 2 deletions bin/ungit
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const config = require('../source/config');
const open = require('open');
const path = require('path');
const child_process = require('child_process');
const encodePath = require('../source/utils/encode-path');

const BugTracker = require('../source/bugtracker');
const bugtracker = new BugTracker('launcher');
Expand Down Expand Up @@ -35,9 +36,9 @@ const openUngitBrowser = (pathToNavigateTo) => {
const navigate = () => {
let url = config.urlBase + ':' + config.port;
if (config.forcedLaunchPath === undefined) {
url += '/#/repository?path=' + encodeURIComponent(process.cwd());
url += '/#/repository?path=' + encodePath(process.cwd());
} else if (config.forcedLaunchPath !== null && config.forcedLaunchPath !== '') {
url += '/#/repository?path=' + encodeURIComponent(config.forcedLaunchPath);
url += '/#/repository?path=' + encodePath(config.forcedLaunchPath);
}

if (config.launchCommand) {
Expand Down
3 changes: 2 additions & 1 deletion clicktests/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const request = require('superagent');
const mkdirp = require('mkdirp');
const util = require('util');
const rimraf = util.promisify(require('rimraf'));
const encodePath = require('../source/utils/encode-path');
const portrange = 45032;

module.exports = (config) => new Environment(config);
Expand Down Expand Up @@ -237,7 +238,7 @@ class Environment {
}

async openUngit(tempDirPath) {
await this.goto(`${this.getRootUrl()}/#/repository?path=${encodeURIComponent(tempDirPath)}`);
await this.goto(`${this.getRootUrl()}/#/repository?path=${encodePath(tempDirPath)}`);
await this.waitForElementVisible('.repository-actions');
await this.wait(1000);
}
Expand Down
3 changes: 2 additions & 1 deletion clicktests/spec.remotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const environment = require('./environment')();
const mkdirp = require('mkdirp');
const util = require('util');
const rimraf = util.promisify(require('rimraf'));
const encodePath = require('../source/utils/encode-path');
const testRepoPaths = [];

describe('[REMOTES]', () => {
Expand Down Expand Up @@ -62,7 +63,7 @@ describe('[REMOTES]', () => {
// ----------- CLONING -------------
it('navigate to empty folder path', async () => {
await environment.goto(
`${environment.getRootUrl()}/#/repository?path=${encodeURIComponent(testRepoPaths[2])}`
`${environment.getRootUrl()}/#/repository?path=${encodePath(testRepoPaths[2])}`
);
await environment.waitForElementVisible('.uninited');
});
Expand Down
5 changes: 3 additions & 2 deletions clicktests/spec.screens.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const environment = require('./environment')();
const mkdirp = require('mkdirp');
const util = require('util');
const rimraf = util.promisify(require('rimraf'));
const encodePath = require('../source/utils/encode-path');
const testRepoPaths = [];

describe('[SCREENS]', () => {
Expand All @@ -19,7 +20,7 @@ describe('[SCREENS]', () => {
testRepoPaths.push(await environment.createTempFolder());

await environment.goto(
`${environment.getRootUrl()}/#/repository?path=${encodeURIComponent(testRepoPaths[0])}`
`${environment.getRootUrl()}/#/repository?path=${encodePath(testRepoPaths[0])}`
);
await environment.waitForElementVisible('.uninited');
});
Expand Down Expand Up @@ -65,7 +66,7 @@ describe('[SCREENS]', () => {
await mkdirp(specialRepoPath);

await environment.goto(
`${environment.getRootUrl()}/#/repository?path=${encodeURIComponent(specialRepoPath)}`
`${environment.getRootUrl()}/#/repository?path=${encodePath(specialRepoPath)}`
);

await environment.waitForElementVisible('.uninited');
Expand Down
3 changes: 2 additions & 1 deletion components/header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const octicons = require('octicons');
const components = require('ungit-components');
const navigation = require('ungit-navigation');
const programEvents = require('ungit-program-events');
const encodePath = require('../../source/utils/encode-path');

components.register('header', (args) => new HeaderViewModel(args.app));

Expand All @@ -25,7 +26,7 @@ class HeaderViewModel {
}

submitPath() {
navigation.browseTo(`repository?path=${encodeURIComponent(this.path())}`);
navigation.browseTo(`repository?path=${encodePath(this.path())}`);
}

onProgramEvent(event) {
Expand Down
7 changes: 4 additions & 3 deletions components/home/home.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ko = require('knockout');
const octicons = require('octicons');
const components = require('ungit-components');
const encodePath = require('../../source/utils/encode-path');

components.register('home', (args) => new HomeViewModel(args.app));

Expand All @@ -11,7 +12,7 @@ class HomeRepositoryViewModel {
this.server = this.app.server;
this.path = path;
this.title = path;
this.link = `${ungit.config.rootPath}/#/repository?path=${encodeURIComponent(path)}`;
this.link = `${ungit.config.rootPath}/#/repository?path=${encodePath(path)}`;
this.pathRemoved = ko.observable(false);
this.remote = ko.observable('...');
this.updateState();
Expand All @@ -21,13 +22,13 @@ class HomeRepositoryViewModel {

updateState() {
this.server
.getPromise(`/fs/exists?path=${encodeURIComponent(this.path)}`)
.getPromise(`/fs/exists?path=${encodePath(this.path)}`)
.then((exists) => {
this.pathRemoved(!exists);
})
.catch((e) => this.server.unhandledRejection(e));
this.server
.getPromise(`/remotes/origin?path=${encodeURIComponent(this.path)}`)
.getPromise(`/remotes/origin?path=${encodePath(this.path)}`)
.then((remote) => {
this.remote(remote.address.replace(/\/\/.*?@/, '//***@'));
})
Expand Down
3 changes: 2 additions & 1 deletion components/imagediff/imagediff.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const encodePath = require('../../source/utils/encode-path');
const ko = require('knockout');
const octicons = require('octicons');
const components = require('ungit-components');
Expand All @@ -17,7 +18,7 @@ class ImageDiffViewModel {
if (this.isRemoved()) return 'removed';
return 'changed';
});
const gitDiffURL = `${ungit.config.rootPath}/api/diff/image?path=${encodeURIComponent(
const gitDiffURL = `${ungit.config.rootPath}/api/diff/image?path=${encodePath(
this.repoPath()
)}`;
this.oldImageSrc =
Expand Down
3 changes: 2 additions & 1 deletion components/path/path.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const encodePath = require('../../source/utils/encode-path');
const ko = require('knockout');
const components = require('ungit-components');
const addressParser = require('ungit-address-parser');
Expand Down Expand Up @@ -90,7 +91,7 @@ class PathViewModel {
destinationDir: dest,
isRecursiveSubmodule: this.isRecursiveSubmodule(),
})
.then((res) => navigation.browseTo('repository?path=' + encodeURIComponent(res.path)))
.then((res) => navigation.browseTo('repository?path=' + encodePath(res.path)))
.catch((e) => this.server.unhandledRejection(e))
.finally(() => {
programEvents.dispatch({ event: 'working-tree-changed' });
Expand Down
5 changes: 2 additions & 3 deletions components/repository/repository.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const encodePath = require('../../source/utils/encode-path');
const ko = require('knockout');
const octicons = require('octicons');
const components = require('ungit-components');
Expand Down Expand Up @@ -77,9 +78,7 @@ class RepositoryViewModel {
for (let n = 0; n < submodules.length; n++) {
if (submodules[n].path === baseName) {
this.parentModulePath(baseRepoPath.path);
this.parentModuleLink(
`/#/repository?path=${encodeURIComponent(baseRepoPath.path)}`
);
this.parentModuleLink(`/#/repository?path=${encodePath(baseRepoPath.path)}`);
return;
}
}
Expand Down
5 changes: 3 additions & 2 deletions public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var child_process = require('child_process');
var path = require('path');
// eslint-disable-next-line no-unused-vars -- Imported for side effects
var winston = require('../source/utils/winston');
const encodePath = require('../source/utils/encode-path');
var config = require('../source/config');
var BugTracker = require('../source/bugtracker');
var bugtracker = new BugTracker('electron');
Expand All @@ -24,9 +25,9 @@ function openUngitBrowser(pathToNavigateTo) {
function launch(callback) {
var url = config.urlBase + ':' + config.port;
if (config.forcedLaunchPath === undefined) {
url += '/#/repository?path=' + encodeURIComponent(process.cwd());
url += '/#/repository?path=' + encodePath(process.cwd());
} else if (config.forcedLaunchPath !== null && config.forcedLaunchPath !== '') {
url += '/#/repository?path=' + encodeURIComponent(config.forcedLaunchPath);
url += '/#/repository?path=' + encodePath(config.forcedLaunchPath);
}

if (config.launchCommand) {
Expand Down
3 changes: 2 additions & 1 deletion public/source/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var encodePath = require('../../source/utils/encode-path');
var _ = require('lodash');
var $ = require('jquery');
jQuery = $; // this is for old backward compatability of bootrap modules
Expand Down Expand Up @@ -93,7 +94,7 @@ ko.bindingHandlers.autocomplete = {
} else if (event.keyCode === 13) {
// enter key is struck, navigate to the path
event.preventDefault();
navigation.browseTo(`repository?path=${encodeURIComponent(value)}`);
navigation.browseTo(`repository?path=${encodePath(value)}`);
} else if (value === '' && storage.getItem('repositories')) {
// if path is emptied out, show save path options
const folderNames = JSON.parse(storage.getItem('repositories')).map((value) => {
Expand Down
3 changes: 3 additions & 0 deletions source/utils/encode-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = (path) => encodeURIComponent(path).replace(/%2F/g, '/');

0 comments on commit a71033b

Please sign in to comment.