Skip to content

Commit

Permalink
Fix: Encode paths as URI components when opening a folder or workspace (
Browse files Browse the repository at this point in the history
#182398)

* Encode folder paths as URI components.

This should prevent issues when trying to open folder paths that contain
special characters like `+` or `&`.

* 💄

---------

Co-authored-by: Benjamin Pasero <benjamin.pasero@gmail.com>
  • Loading branch information
dyedgreen and bpasero committed May 15, 2023
1 parent c4ef9df commit 29bf616
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/vs/code/browser/workbench/workbench.ts
Expand Up @@ -404,34 +404,13 @@ class WorkspaceProvider implements IWorkspaceProvider {

// Folder
else if (isFolderToOpen(workspace)) {
let queryParamFolder: string;
if (this.config.remoteAuthority && workspace.folderUri.scheme === Schemas.vscodeRemote) {
// when connected to a remote and having a folder
// for that remote, only use the path as query
// value to form shorter, nicer URLs.
// ensure paths are absolute (begin with `/`)
// clipboard: ltrim(workspace.folderUri.path, posix.sep)
queryParamFolder = `${posix.sep}${ltrim(workspace.folderUri.path, posix.sep)}`;
} else {
queryParamFolder = encodeURIComponent(workspace.folderUri.toString(true));
}

const queryParamFolder = this.encodeWorkspacePath(workspace.folderUri);
targetHref = `${document.location.origin}${document.location.pathname}?${WorkspaceProvider.QUERY_PARAM_FOLDER}=${queryParamFolder}`;
}

// Workspace
else if (isWorkspaceToOpen(workspace)) {
let queryParamWorkspace: string;
if (this.config.remoteAuthority && workspace.workspaceUri.scheme === Schemas.vscodeRemote) {
// when connected to a remote and having a workspace
// for that remote, only use the path as query
// value to form shorter, nicer URLs.
// ensure paths are absolute (begin with `/`)
queryParamWorkspace = `${posix.sep}${ltrim(workspace.workspaceUri.path, posix.sep)}`;
} else {
queryParamWorkspace = encodeURIComponent(workspace.workspaceUri.toString(true));
}

const queryParamWorkspace = this.encodeWorkspacePath(workspace.workspaceUri);
targetHref = `${document.location.origin}${document.location.pathname}?${WorkspaceProvider.QUERY_PARAM_WORKSPACE}=${queryParamWorkspace}`;
}

Expand All @@ -443,6 +422,22 @@ class WorkspaceProvider implements IWorkspaceProvider {
return targetHref;
}

private encodeWorkspacePath(uri: URI): string {
if (this.config.remoteAuthority && uri.scheme === Schemas.vscodeRemote) {

// when connected to a remote and having a folder
// or workspace for that remote, only use the path
// as query value to form shorter, nicer URLs.
// however, we still need to `encodeURIComponent`
// to ensure to preserve special characters, such
// as `+` in the path.

return encodeURIComponent(`${posix.sep}${ltrim(uri.path, posix.sep)}`).replaceAll('%2F', '/');
}

return encodeURIComponent(uri.toString(true));
}

private isSame(workspaceA: IWorkspace, workspaceB: IWorkspace): boolean {
if (!workspaceA || !workspaceB) {
return workspaceA === workspaceB; // both empty
Expand Down

0 comments on commit 29bf616

Please sign in to comment.