diff --git a/src/debugger/main.ts b/src/debugger/main.ts index 03e82a6d9..774bcda55 100644 --- a/src/debugger/main.ts +++ b/src/debugger/main.ts @@ -293,42 +293,70 @@ class RubyDebugSession extends DebugSession { return localPath.replace(/\\/g, '/'); } + private isSubPath(subPath: string): boolean { + return subPath && !subPath.startsWith('..') && !path.isAbsolute(subPath); + } + + private getPathImplementation(pathToCheck: string): any { + if (pathToCheck) { + if (pathToCheck.indexOf(path.posix.sep) >= 0) { + return path.posix; + } else if (pathToCheck.indexOf(path.win32.sep) >= 0) { + return path.win32; + } + } + + return path; + } + protected convertClientPathToDebugger(localPath: string): string { - if (this.debugMode == Mode.launch) { + if (this.debugMode === Mode.launch) { return localPath; } - if (!localPath.startsWith(this.requestArguments.cwd)) { + let relativeLocalPath = path.relative(this.requestArguments.cwd, localPath); + + if (!this.isSubPath(relativeLocalPath)) { return localPath; } - var relativePath = path.join( - this.requestArguments.remoteWorkspaceRoot, localPath.substring(this.requestArguments.cwd.length) - ); + let remoteWorkspaceRoot = + this.requestArguments.remoteWorkspaceRoot || this.requestArguments.cwd; - var sepIndex = this.requestArguments.remoteWorkspaceRoot.lastIndexOf('/'); + let remotePathImplementation = this.getPathImplementation(remoteWorkspaceRoot); + let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); - if (sepIndex !== -1) { - // *inx or darwin - relativePath = relativePath.replace(/\\/g, '/'); - } + let relativePath = remotePathImplementation.join.apply( + null, + [remoteWorkspaceRoot].concat(relativeLocalPath.split(localPathImplementation.sep)) + ); return relativePath; } - protected convertDebuggerPathToClient(serverPath: string):string{ - if (this.debugMode == Mode.launch) { + protected convertDebuggerPathToClient(serverPath: string): string { + if (this.debugMode === Mode.launch) { return serverPath; } - if (!serverPath.startsWith(this.requestArguments.remoteWorkspaceRoot)) { + let remoteWorkspaceRoot = + this.requestArguments.remoteWorkspaceRoot || this.requestArguments.cwd; + + let remotePathImplementation = this.getPathImplementation(remoteWorkspaceRoot); + let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); + + let relativeRemotePath = remotePathImplementation.relative(remoteWorkspaceRoot, serverPath); + + + if (!this.isSubPath(relativeRemotePath)) { return serverPath; } - // Path.join will convert the path using local OS preferred separator - var relativePath = path.join( - this.requestArguments.cwd, serverPath.substring(this.requestArguments.remoteWorkspaceRoot.length) + let relativePath = localPathImplementation.join.apply( + null, + [this.requestArguments.cwd].concat(relativeRemotePath.split(remotePathImplementation.sep)) ); + return relativePath; }