Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always intercept file protocol #116522

Merged
merged 2 commits into from Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 29 additions & 15 deletions src/vs/code/electron-main/protocol.ts
Expand Up @@ -37,19 +37,13 @@ export class FileProtocolHandler extends Disposable {
// Register vscode-file:// handler
defaultSession.protocol.registerFileProtocol(Schemas.vscodeFileResource, (request, callback) => this.handleResourceRequest(request, callback as unknown as ProtocolCallback));

// Block any file:// access (explicitly enabled only)
if (isPreferringBrowserCodeLoad) {
this.logService.info(`Intercepting ${Schemas.file}: protocol and blocking it`);

defaultSession.protocol.interceptFileProtocol(Schemas.file, (request, callback) => this.handleFileRequest(request, callback as unknown as ProtocolCallback));
}
// Intercept any file:// access
defaultSession.protocol.interceptFileProtocol(Schemas.file, (request, callback) => this.handleFileRequest(request, callback as unknown as ProtocolCallback));

// Cleanup
this._register(toDisposable(() => {
defaultSession.protocol.unregisterProtocol(Schemas.vscodeFileResource);
if (isPreferringBrowserCodeLoad) {
defaultSession.protocol.uninterceptProtocol(Schemas.file);
}
defaultSession.protocol.uninterceptProtocol(Schemas.file);
}));
}

Expand Down Expand Up @@ -85,10 +79,29 @@ export class FileProtocolHandler extends Disposable {
}

private async handleFileRequest(request: Electron.ProtocolRequest, callback: ProtocolCallback) {
const uri = URI.parse(request.url);
const fileUri = URI.parse(request.url);

// isPreferringBrowserCodeLoad: false
// => ensure the file path is in our expected roots
if (!isPreferringBrowserCodeLoad) {
if (this.validRoots.findSubstr(fileUri)) {
return callback({
path: fileUri.fsPath
});
}

this.logService.error(`${Schemas.file}: Refused to load resource ${fileUri.fsPath} from ${Schemas.file}: protocol (original URL: ${request.url})`);

this.logService.error(`Refused to load resource ${uri.fsPath} from ${Schemas.file}: protocol`);
callback({ error: -3 /* ABORTED */ });
return callback({ error: -3 /* ABORTED */ });
}

// isPreferringBrowserCodeLoad: true
// => block any file request
else {
this.logService.error(`Refused to load resource ${fileUri.fsPath} from ${Schemas.file}: protocol (original URL: ${request.url})`);

return callback({ error: -3 /* ABORTED */ });
}
}

private async handleResourceRequest(request: Electron.ProtocolRequest, callback: ProtocolCallback) {
Expand All @@ -102,9 +115,10 @@ export class FileProtocolHandler extends Disposable {
return callback({
path: fileUri.fsPath
});
}
} else {
this.logService.error(`${Schemas.vscodeFileResource}: Refused to load resource ${fileUri.fsPath} from ${Schemas.vscodeFileResource}: protocol (original URL: ${request.url})`);

this.logService.error(`${Schemas.vscodeFileResource}: Refused to load resource ${fileUri.fsPath}}`);
callback({ error: -3 /* ABORTED */ });
return callback({ error: -3 /* ABORTED */ });
}
}
}