Skip to content

Commit

Permalink
Add error markers to executable settings in untrusted workspace settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 authored and kieferrm committed Jan 24, 2017
1 parent 97b08a7 commit 03c1584
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/vs/platform/configuration/common/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export interface IConfigModel<T> {
config<V>(section: string): IConfigModel<V>;
configWithOverrides<V>(identifier: string, section?: string): IConfigModel<V>;
refilter(): void;
hasActiveFilter(): boolean;
}

export interface IOverrides<T> {
Expand Down
12 changes: 0 additions & 12 deletions src/vs/platform/configuration/common/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,6 @@ export class ConfigModel<T> implements IConfigModel<T> {
this._contents = toValuesTree(this._raw, message => console.error(`Conflict in settings file ${this.name}: ${message}`));
}
}

public hasActiveFilter(): boolean {
if (this._raw === this._unfilteredRaw) {
return false;
}
for (let key in this._unfilteredRaw) {
if (!this._raw.hasOwnProperty(key)) {
return true;
}
}
return false;
}
}

export class DefaultConfigModel<T> extends ConfigModel<T> {
Expand Down
45 changes: 45 additions & 0 deletions src/vs/workbench/parts/preferences/browser/preferencesEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment'
import { VSash } from 'vs/base/browser/ui/sash/sash';
import { Widget } from 'vs/base/browser/ui/widget';
import { overrideIdentifierFromKey } from 'vs/platform/configuration/common/model';
import { IMarkerService, IMarkerData } from 'vs/platform/markers/common/markers';
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';

// Ignore following contributions
import { FoldingController } from 'vs/editor/contrib/folding/browser/folding';
Expand Down Expand Up @@ -568,6 +570,7 @@ export class SettingsRenderer extends Disposable implements IPreferencesRenderer
private editSettingActionRenderer: EditSettingRenderer;
private highlightPreferencesRenderer: HighlightPreferencesRenderer;
private defaultSettingsModel: DefaultSettingsEditorModel;
private untrustedSettingRenderer: UnTrustedWorkspaceSettingsRenderer;
private modelChangeDelayer: Delayer<void> = new Delayer<void>(200);

private _onFocusPreference: Emitter<ISetting> = new Emitter<ISetting>();
Expand All @@ -584,6 +587,9 @@ export class SettingsRenderer extends Disposable implements IPreferencesRenderer
@IInstantiationService protected instantiationService: IInstantiationService
) {
super();
if (this.preferencesService.workspaceSettingsResource.toString() === preferencesModel.uri.toString()) {
this.untrustedSettingRenderer = this._register(instantiationService.createInstance(UnTrustedWorkspaceSettingsRenderer, editor, preferencesModel));
}
this.settingHighlighter = this._register(instantiationService.createInstance(SettingHighlighter, editor, this._onFocusPreference, this._onClearFocusPreference));
this.highlightPreferencesRenderer = this._register(instantiationService.createInstance(HighlightPreferencesRenderer, editor));
this.initializationPromise = this.initialize();
Expand All @@ -596,6 +602,9 @@ export class SettingsRenderer extends Disposable implements IPreferencesRenderer
public render(): void {
this.initializationPromise.then(() => {
this.editSettingActionRenderer.render(this.preferencesModel.settingsGroups);
if (this.untrustedSettingRenderer) {
this.untrustedSettingRenderer.render();
}
});
}

Expand Down Expand Up @@ -1339,6 +1348,42 @@ class SettingHighlighter extends Disposable {
}
}

class UnTrustedWorkspaceSettingsRenderer extends Disposable {

constructor(private editor: editorCommon.ICommonCodeEditor, private workspaceSettingsEditorModel: SettingsEditorModel,
@IWorkspaceConfigurationService private configurationService: IWorkspaceConfigurationService,
@IMarkerService private markerService: IMarkerService
) {
super();
}

public render(): void {
const untrustedConfigurations = this.configurationService.getUntrustedConfigurations();
if (untrustedConfigurations.length) {
const markerData: IMarkerData[] = [];
for (const untrustedConfiguration of untrustedConfigurations) {
const setting = this.workspaceSettingsEditorModel.getPreference(untrustedConfiguration);
if (setting) {
markerData.push({
severity: Severity.Error,
startLineNumber: setting.keyRange.startLineNumber,
startColumn: setting.keyRange.startColumn,
endLineNumber: setting.keyRange.endLineNumber,
endColumn: setting.keyRange.endColumn,
message: nls.localize('untrustedWorkspaceWithExectuables', "`{0}` is an executable property and since current workspace is untrusted, it is ignored. To enable it, open User settings and add current workspace to trusted workspaces list using setting `security.workspacesTrustedToSpecifyExecutables`", setting.key)
});
}
}
this.markerService.changeOne('preferencesEditor', this.workspaceSettingsEditorModel.uri, markerData);
}
}

public dispose(): void {
this.markerService.remove('preferencesEditor', [this.workspaceSettingsEditorModel.uri]);
super.dispose();
}
}

const DefaultSettingsEditorCommand = EditorCommand.bindToContribution<PreferencesEditorContribution<ISetting>>((editor: editorCommon.ICommonCodeEditor) => <PreferencesEditorContribution<ISetting>>editor.getContribution(DefaultSettingsEditorContribution.ID));

CommonEditorRegistry.registerEditorCommand(new DefaultSettingsEditorCommand({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TrustContribution implements IWorkbenchContribution {

private checkWorkspaceTrust(): void {
const wasUntrusted = this.isUntrusted;
this.isUntrusted = this.workspaceConfigurationService.hasUntrustedConfigurations();
this.isUntrusted = this.workspaceConfigurationService.getUntrustedConfigurations().length > 0;
if (this.isUntrusted && !wasUntrusted) {
this.showTrustWarning();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export interface IWorkspaceConfigurationService extends IConfigurationService {
hasWorkspaceConfiguration(): boolean;

/**
* Returns iff the workspace configuration contains configuration keys that are untrusted.
* Returns untrusted configuration keys for the current workspace
*/
hasUntrustedConfigurations(): boolean;
getUntrustedConfigurations(): string[];

/**
* Override for the IConfigurationService#lookup() method that adds information about workspace settings.
Expand Down
25 changes: 12 additions & 13 deletions src/vs/workbench/services/configuration/common/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
'use strict';

import { ConfigModel } from 'vs/platform/configuration/common/model';
import { IConfigModel } from 'vs/platform/configuration/common/configuration';
import { IWorkspaceTrust, WORKSPACE_STANDALONE_CONFIGURATIONS } from 'vs/workbench/services/configuration/common/configuration';

export class ScopedConfigModel<T> extends ConfigModel<T> {
Expand All @@ -26,6 +25,8 @@ export class ScopedConfigModel<T> extends ConfigModel<T> {

export class TrustedWorkspaceSettingsConfigModel<T> extends ConfigModel<T> {

private _untrustedKeys: string[] = [];

constructor(content: string, name: string = '', private workspaceTrust: IWorkspaceTrust = null) {
super(null, name);
if (content) {
Expand All @@ -34,7 +35,7 @@ export class TrustedWorkspaceSettingsConfigModel<T> extends ConfigModel<T> {
}

protected filterRaw(raw: any): { newRaw: any; removals: any } {

this._untrustedKeys = [];
let allUntrustedKeys = {};
if (this.workspaceTrust && !this.workspaceTrust.isTrusted()) {
allUntrustedKeys = this.workspaceTrust.allKnownConfigKeysForExecutables();
Expand All @@ -44,15 +45,21 @@ export class TrustedWorkspaceSettingsConfigModel<T> extends ConfigModel<T> {
for (let property in raw) {
if (!allUntrustedKeys[property]) {
trustedProperties[property] = raw[property];
} else {
this._untrustedKeys.push(property);
}
}
return trustedProperties;
}

public get untrustedKeys(): string[] {
return this._untrustedKeys;
}
}

export class WorkspaceConfigModel<T> extends ConfigModel<T> {

constructor(private workspaceSettingsConfig: IConfigModel<T>, private scopedConfigs: ScopedConfigModel<T>[]) {
constructor(private workspaceSettingsConfig: TrustedWorkspaceSettingsConfigModel<T>, private scopedConfigs: ScopedConfigModel<T>[]) {
super(null);
this.consolidate();
}
Expand Down Expand Up @@ -83,15 +90,7 @@ export class WorkspaceConfigModel<T> extends ConfigModel<T> {
});
}

public hasActiveFilter(): boolean {
if (this.workspaceSettingsConfig.hasActiveFilter()) {
return true;
}
this.scopedConfigs.forEach(scopedConfigModel => {
if (scopedConfigModel.hasActiveFilter()) {
return true;
}
});
return false;
public get untrustedKeys(): string[] {
return this.workspaceSettingsConfig.untrustedKeys;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class WorkspaceConfigurationService extends Disposable implements IWorksp
this.workspaceFilePathToConfiguration = Object.create(null);

this.cachedConfig = new ConfigModel<any>(null);
this.cachedWorkspaceConfig = new WorkspaceConfigModel(new ConfigModel(null), []);
this.cachedWorkspaceConfig = new WorkspaceConfigModel(new TrustedWorkspaceSettingsConfigModel(null), []);

this._onDidUpdateConfiguration = this._register(new Emitter<IConfigurationServiceEvent>());

Expand Down Expand Up @@ -228,7 +228,7 @@ export class WorkspaceConfigurationService extends Disposable implements IWorksp
return this.loadWorkspaceConfigFiles().then(workspaceConfigFiles => {

// Consolidate (support *.json files in the workspace settings folder)
let workspaceSettingsModel: IConfigModel<T> = <IConfigModel<T>>workspaceConfigFiles[WORKSPACE_CONFIG_DEFAULT_PATH] || new ConfigModel<T>(null);
let workspaceSettingsModel: TrustedWorkspaceSettingsConfigModel<T> = <TrustedWorkspaceSettingsConfigModel<T>>workspaceConfigFiles[WORKSPACE_CONFIG_DEFAULT_PATH] || new TrustedWorkspaceSettingsConfigModel<T>(null);
let otherConfigModels = Object.keys(workspaceConfigFiles).filter(key => key !== WORKSPACE_CONFIG_DEFAULT_PATH).map(key => <ScopedConfigModel<T>>workspaceConfigFiles[key]);

this.cachedWorkspaceConfig = new WorkspaceConfigModel<T>(workspaceSettingsModel, otherConfigModels);
Expand Down Expand Up @@ -347,8 +347,8 @@ export class WorkspaceConfigurationService extends Disposable implements IWorksp
return [WORKSPACE_CONFIG_DEFAULT_PATH, WORKSPACE_STANDALONE_CONFIGURATIONS.launch, WORKSPACE_STANDALONE_CONFIGURATIONS.tasks].some(p => p === workspaceRelativePath);
}

public hasUntrustedConfigurations(): boolean {
return this.cachedWorkspaceConfig.hasActiveFilter();
public getUntrustedConfigurations(): string[] {
return this.cachedWorkspaceConfig.untrustedKeys;
}
}

Expand Down

0 comments on commit 03c1584

Please sign in to comment.