Skip to content

Commit 29bf616

Browse files
dyedgreenbpasero
andauthored
Fix: Encode paths as URI components when opening a folder or workspace (microsoft#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 <[email protected]>
1 parent c4ef9df commit 29bf616

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/vs/code/browser/workbench/workbench.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -404,34 +404,13 @@ class WorkspaceProvider implements IWorkspaceProvider {
404404

405405
// Folder
406406
else if (isFolderToOpen(workspace)) {
407-
let queryParamFolder: string;
408-
if (this.config.remoteAuthority && workspace.folderUri.scheme === Schemas.vscodeRemote) {
409-
// when connected to a remote and having a folder
410-
// for that remote, only use the path as query
411-
// value to form shorter, nicer URLs.
412-
// ensure paths are absolute (begin with `/`)
413-
// clipboard: ltrim(workspace.folderUri.path, posix.sep)
414-
queryParamFolder = `${posix.sep}${ltrim(workspace.folderUri.path, posix.sep)}`;
415-
} else {
416-
queryParamFolder = encodeURIComponent(workspace.folderUri.toString(true));
417-
}
418-
407+
const queryParamFolder = this.encodeWorkspacePath(workspace.folderUri);
419408
targetHref = `${document.location.origin}${document.location.pathname}?${WorkspaceProvider.QUERY_PARAM_FOLDER}=${queryParamFolder}`;
420409
}
421410

422411
// Workspace
423412
else if (isWorkspaceToOpen(workspace)) {
424-
let queryParamWorkspace: string;
425-
if (this.config.remoteAuthority && workspace.workspaceUri.scheme === Schemas.vscodeRemote) {
426-
// when connected to a remote and having a workspace
427-
// for that remote, only use the path as query
428-
// value to form shorter, nicer URLs.
429-
// ensure paths are absolute (begin with `/`)
430-
queryParamWorkspace = `${posix.sep}${ltrim(workspace.workspaceUri.path, posix.sep)}`;
431-
} else {
432-
queryParamWorkspace = encodeURIComponent(workspace.workspaceUri.toString(true));
433-
}
434-
413+
const queryParamWorkspace = this.encodeWorkspacePath(workspace.workspaceUri);
435414
targetHref = `${document.location.origin}${document.location.pathname}?${WorkspaceProvider.QUERY_PARAM_WORKSPACE}=${queryParamWorkspace}`;
436415
}
437416

@@ -443,6 +422,22 @@ class WorkspaceProvider implements IWorkspaceProvider {
443422
return targetHref;
444423
}
445424

425+
private encodeWorkspacePath(uri: URI): string {
426+
if (this.config.remoteAuthority && uri.scheme === Schemas.vscodeRemote) {
427+
428+
// when connected to a remote and having a folder
429+
// or workspace for that remote, only use the path
430+
// as query value to form shorter, nicer URLs.
431+
// however, we still need to `encodeURIComponent`
432+
// to ensure to preserve special characters, such
433+
// as `+` in the path.
434+
435+
return encodeURIComponent(`${posix.sep}${ltrim(uri.path, posix.sep)}`).replaceAll('%2F', '/');
436+
}
437+
438+
return encodeURIComponent(uri.toString(true));
439+
}
440+
446441
private isSame(workspaceA: IWorkspace, workspaceB: IWorkspace): boolean {
447442
if (!workspaceA || !workspaceB) {
448443
return workspaceA === workspaceB; // both empty

0 commit comments

Comments
 (0)