Skip to content

Port final trust fixes for release #12965

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

Merged
merged 3 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/client/datascience/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ export enum Telemetry {
RunByLineStart = 'DATASCIENCE.RUN_BY_LINE',
RunByLineStep = 'DATASCIENCE.RUN_BY_LINE_STEP',
RunByLineStop = 'DATASCIENCE.RUN_BY_LINE_STOP',
RunByLineVariableHover = 'DATASCIENCE.RUN_BY_LINE_VARIABLE_HOVER'
RunByLineVariableHover = 'DATASCIENCE.RUN_BY_LINE_VARIABLE_HOVER',
TrustAllNotebooks = 'DATASCIENCE.TRUST_ALL_NOTEBOOKS',
TrustNotebook = 'DATASCIENCE.TRUST_NOTEBOOK',
DoNotTrustNotebook = 'DATASCIENCE.DO_NOT_TRUST_NOTEBOOK',
NotebookTrustPromptShown = 'DATASCIENCE.NOTEBOOK_TRUST_PROMPT_SHOWN'
}

export enum NativeKeyboardCommandTelemetry {
Expand Down
35 changes: 21 additions & 14 deletions src/client/datascience/interactive-ipynb/nativeEditorStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ export class NativeEditorStorage implements INotebookStorage {
const dirtyContents = skipDirtyContents ? undefined : await this.getStoredContents(file, backupId);
if (dirtyContents) {
// This means we're dirty. Indicate dirty and load from this content
return this.loadContents(file, dirtyContents, true, contents, forVSCodeNotebook);
return this.loadContents(file, dirtyContents, true, forVSCodeNotebook);
} else {
// Load without setting dirty
return this.loadContents(file, contents, undefined, undefined, forVSCodeNotebook);
return this.loadContents(file, contents, undefined, forVSCodeNotebook);
}
} catch (ex) {
// May not exist at this time. Should always have a single cell though
Expand All @@ -308,7 +308,6 @@ export class NativeEditorStorage implements INotebookStorage {
file: Uri,
contents: string | undefined,
isInitiallyDirty = false,
trueContents?: string,
forVSCodeNotebook?: boolean
) {
// tslint:disable-next-line: no-any
Expand Down Expand Up @@ -348,18 +347,9 @@ export class NativeEditorStorage implements INotebookStorage {
}
const pythonNumber = json ? await this.extractPythonMainVersion(json) : 3;

/* As an optimization, we don't call trustNotebook for hot exit, since our hot exit backup code gets called by VS
Code whenever the notebook model changes. This means it's called very often, perhaps even as often as autosave.
Instead, when loading a file that is dirty, we check if the actual file contents on disk are trusted. If so, we treat
the dirty contents as trusted as well. */
const contentsToCheck = isInitiallyDirty && trueContents !== undefined ? trueContents : contents;
const isTrusted =
contents === undefined || isUntitledFile(file)
? true // If no contents or untitled, this is a newly created file, so it should be trusted
: await this.trustService.isNotebookTrusted(file, contentsToCheck!);
return this.factory.createModel(
const model = this.factory.createModel(
{
trusted: isTrusted,
trusted: true,
file,
cells: remapped,
notebookJson: json,
Expand All @@ -369,6 +359,23 @@ export class NativeEditorStorage implements INotebookStorage {
},
forVSCodeNotebook
);

// If no contents or untitled, this is a newly created file
// If dirty, that means it's been edited before in our extension
if (contents !== undefined && !isUntitledFile(file) && !isInitiallyDirty) {
const isNotebookTrusted = await this.trustService.isNotebookTrusted(file, model.getContent());
if (isNotebookTrusted !== model.isTrusted) {
model.update({
source: 'user',
kind: 'updateTrust',
oldDirty: model.isDirty,
newDirty: model.isDirty,
isNotebookTrusted
});
}
}

return model;
}

private getStaticStorageKey(file: Uri): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import '../../common/extensions';
import { IDisposableRegistry, IExperimentService } from '../../common/types';
import { swallowExceptions } from '../../common/utils/decorators';
import { DataScience } from '../../common/utils/localize';
import { Commands } from '../constants';
import { sendTelemetryEvent } from '../../telemetry';
import { Commands, Telemetry } from '../constants';
import { INotebookStorageProvider } from '../interactive-ipynb/notebookStorageProvider';
import { INotebookEditorProvider, ITrustService } from '../types';

Expand Down Expand Up @@ -57,10 +58,12 @@ export class TrustCommandHandler implements IExtensionSingleActivationService {
DataScience.doNotTrustNotebook(),
DataScience.trustAllNotebooks()
);
sendTelemetryEvent(Telemetry.NotebookTrustPromptShown);

switch (selection) {
case DataScience.trustAllNotebooks():
commands.executeCommand('workbench.action.openSettings', 'python.dataScience.alwaysTrustNotebooks');
sendTelemetryEvent(Telemetry.TrustAllNotebooks);
break;
case DataScience.trustNotebook():
// Update model trust
Expand All @@ -73,6 +76,10 @@ export class TrustCommandHandler implements IExtensionSingleActivationService {
});
const contents = model.getContent();
await this.trustService.trustNotebook(model.file, contents);
sendTelemetryEvent(Telemetry.TrustNotebook);
break;
case DataScience.doNotTrustNotebook():
sendTelemetryEvent(Telemetry.DoNotTrustNotebook);
break;
default:
break;
Expand Down
8 changes: 2 additions & 6 deletions src/client/datascience/notebookStorage/baseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,8 @@ export abstract class BaseNotebookModel implements INotebookModel {
this.ensureNotebookJson();

// Reuse our original json except for the cells.
const json = {
cells: this.cells.map((c) => pruneCell(c.data)),
metadata: this.notebookJson.metadata,
nbformat: this.notebookJson.nbformat,
nbformat_minor: this.notebookJson.nbformat_minor
};
const json = { ...this.notebookJson };
json.cells = this.cells.map((c) => pruneCell(c.data));
return JSON.stringify(json, null, this.indentAmount);
}
}
10 changes: 10 additions & 0 deletions src/client/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2192,10 +2192,20 @@ export interface IEventNamePropertyMapping {
[Telemetry.StartPageOpenFileBrowser]: never | undefined;
[Telemetry.StartPageOpenFolder]: never | undefined;
[Telemetry.StartPageOpenWorkspace]: never | undefined;

// Run by line events
[Telemetry.RunByLineStart]: never | undefined;
[Telemetry.RunByLineStep]: never | undefined;
[Telemetry.RunByLineStop]: never | undefined;
[Telemetry.RunByLineVariableHover]: never | undefined;

// Trusted notebooks events
[Telemetry.NotebookTrustPromptShown]: never | undefined;
[Telemetry.TrustNotebook]: never | undefined;
[Telemetry.TrustAllNotebooks]: never | undefined;
[Telemetry.DoNotTrustNotebook]: never | undefined;

// Native notebooks events
[VSCodeNativeTelemetry.AddCell]: never | undefined;
[VSCodeNativeTelemetry.DeleteCell]: never | undefined;
[VSCodeNativeTelemetry.MoveCell]: never | undefined;
Expand Down
3 changes: 1 addition & 2 deletions src/datascience-ui/native-editor/nativeCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,7 @@ export function getConnectedNativeCell() {

function isCellNavigationKeyboardEvent(e: IKeyboardEvent) {
return (
e.code === 'Enter' ||
e.code === 'NumpadEnter' ||
((e.code === 'Enter' || e.code === 'NumpadEnter') && !e.shiftKey && !e.ctrlKey && !e.altKey) ||
e.code === 'ArrowUp' ||
e.code === 'k' ||
e.code === 'ArrowDown' ||
Expand Down