Skip to content

Commit

Permalink
chore: cleanup webrequest filter for svg files
Browse files Browse the repository at this point in the history
Fixes #114743
  • Loading branch information
deepak1556 committed Jan 24, 2021
1 parent 79be0a0 commit 62fcf3c
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/vs/code/electron-main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,35 +422,40 @@ export class CodeWindow extends Disposable implements ICodeWindow {
this.dispose();
});

const svgFileSchemes = new Set([Schemas.file, Schemas.vscodeFileResource, Schemas.vscodeRemoteResource, 'devtools']);
const svgFileSchemes = new Set([Schemas.file, 'devtools']);
this._win.webContents.session.webRequest.onBeforeRequest((details, callback) => {
const uri = URI.parse(details.url);
// Prevent loading of remote svgs
if (details.url.endsWith('.svg')) {
const uri = URI.parse(details.url);
if (uri && !svgFileSchemes.has(uri.scheme)) {
if (uri && uri.path.endsWith('.svg')) {
const safeScheme = svgFileSchemes.has(uri.scheme) ||
uri.path.includes(Schemas.vscodeRemoteResource);
if (!safeScheme) {
return callback({ cancel: true });
}
}

return callback({});
return callback({ cancel: false });
});

this._win.webContents.session.webRequest.onHeadersReceived((details, callback) => {
const responseHeaders = details.responseHeaders as Record<string, (string) | (string[])>;
const contentType = (responseHeaders['content-type'] || responseHeaders['Content-Type']);

if (contentType && Array.isArray(contentType)) {
const uri = URI.parse(details.url);
// https://github.com/microsoft/vscode/issues/97564
// ensure local svg files have Content-Type image/svg+xml
if (details.url.endsWith('.svg')) {
const uri = URI.parse(details.url);
if (uri && svgFileSchemes.has(uri.scheme)) {
if (uri && uri.path.endsWith('.svg')) {
if (svgFileSchemes.has(uri.scheme)) {
responseHeaders['Content-Type'] = ['image/svg+xml'];
return callback({ cancel: false, responseHeaders });
}
}

if (contentType.some(x => x.toLowerCase().includes('image/svg'))) {
// remote extension schemes have the following format
// http://127.0.0.1:<port>/vscode-remote-resource?path=
if (!uri.path.includes(Schemas.vscodeRemoteResource) &&
contentType.some(x => x.toLowerCase().includes('image/svg'))) {
return callback({ cancel: true });
}
}
Expand Down

0 comments on commit 62fcf3c

Please sign in to comment.