Skip to content

Commit 637e58f

Browse files
committed
Prevent opening invalid paths
1 parent 6135630 commit 637e58f

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/server.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -531,30 +531,45 @@ export class MainServer extends Server {
531531
util.promisify(fs.readFile)(filePath, "utf8"),
532532
this.servicesPromise,
533533
]);
534+
534535
const logger = this.services.get(ILogService) as ILogService;
535536
logger.info("request.url", `"${request.url}"`);
536-
const environment = this.services.get(IEnvironmentService) as IEnvironmentService;
537-
const locale = environment.args.locale || await getLocaleFromConfig(environment.userDataPath);
537+
538538
const cwd = process.env.VSCODE_CWD || process.cwd();
539-
const workspacePath = parsedUrl.query.workspace as string | undefined;
540-
const folderPath = !workspacePath ? parsedUrl.query.folder as string | undefined || this.options.folderUri : undefined;
539+
541540
const remoteAuthority = request.headers.host as string;
542541
const transformer = getUriTransformer(remoteAuthority);
542+
const validatePath = async (filePath: string[] | string | undefined, isDirectory: boolean, unsetFallback?: string): Promise<UriComponents | undefined> => {
543+
if (!filePath || filePath.length === 0) {
544+
if (!unsetFallback) {
545+
return undefined;
546+
}
547+
filePath = unsetFallback;
548+
} else if (Array.isArray(filePath)) {
549+
filePath = filePath[0];
550+
}
551+
const uri = URI.file(sanitizeFilePath(filePath, cwd));
552+
try {
553+
const stat = await util.promisify(fs.stat)(uri.fsPath);
554+
if (isDirectory !== stat.isDirectory()) {
555+
return undefined;
556+
}
557+
} catch (error) {
558+
return undefined;
559+
}
560+
return transformer.transformOutgoing(uri);
561+
};
562+
563+
const environment = this.services.get(IEnvironmentService) as IEnvironmentService;
543564
const options: Options = {
544565
WORKBENCH_WEB_CONGIGURATION: {
545-
workspaceUri: workspacePath
546-
? transformer.transformOutgoing(URI.file(sanitizeFilePath(workspacePath, cwd)))
547-
: undefined,
548-
folderUri: folderPath
549-
? transformer.transformOutgoing(URI.file(sanitizeFilePath(folderPath, cwd)))
550-
: undefined,
566+
workspaceUri: await validatePath(parsedUrl.query.workspace, false),
567+
folderUri: !parsedUrl.query.workspace ? await validatePath(parsedUrl.query.folder, true, this.options.folderUri) : undefined,
551568
remoteAuthority,
552569
productConfiguration: product,
553570
},
554-
REMOTE_USER_DATA_URI: transformer.transformOutgoing(
555-
(this.services.get(IEnvironmentService) as EnvironmentService).webUserDataHome,
556-
),
557-
NLS_CONFIGURATION: await getNlsConfiguration(locale, environment.userDataPath),
571+
REMOTE_USER_DATA_URI: transformer.transformOutgoing((<EnvironmentService>environment).webUserDataHome),
572+
NLS_CONFIGURATION: await getNlsConfiguration(environment.args.locale || await getLocaleFromConfig(environment.userDataPath), environment.userDataPath),
558573
};
559574

560575
content = content.replace(/\/static\//g, `/static${product.commit ? `-${product.commit}` : ""}/`).replace("{{WEBVIEW_ENDPOINT}}", "");

0 commit comments

Comments
 (0)