Skip to content

Commit 656fb61

Browse files
authored
Port trust fixes (#12929)
* Fix regressions in trusted notebooks (#12902) * Handle trustAllNotebooks selection * Fix bug where after trusting, UI didn't update * Recover from ENOENT due to missing parent directory when trusting notebook (#12913) * Disable keydown on native cells in untrusted notebooks (#12914)
1 parent f0bad89 commit 656fb61

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

src/client/datascience/interactive-ipynb/digestStorage.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ export class DigestStorage implements IDigestStorage {
2626
public async saveDigest(uri: Uri, signature: string) {
2727
const fileLocation = await this.getFileLocation(uri);
2828
// Since the signature is a hex digest, the character 'z' is being used to delimit the start and end of a single digest
29-
await this.fs.appendFile(fileLocation, `z${signature}z\n`);
30-
if (!this.loggedFileLocations.has(fileLocation)) {
31-
traceInfo(`Wrote trust for ${uri.toString()} to ${fileLocation}`);
32-
this.loggedFileLocations.add(fileLocation);
29+
try {
30+
await this.saveDigestInner(uri, fileLocation, signature);
31+
} catch (err) {
32+
// The nbsignatures dir is only initialized on extension activation.
33+
// If the user deletes it to reset trust, the next attempt to trust
34+
// an untrusted notebook in the same session will fail because the parent
35+
// directory does not exist.
36+
if (isFileNotFoundError(err)) {
37+
// Gracefully recover from such errors by reinitializing directory and retrying
38+
await this.initDir();
39+
await this.saveDigestInner(uri, fileLocation, signature);
40+
} else {
41+
traceError(err);
42+
}
3343
}
3444
}
3545

@@ -46,6 +56,14 @@ export class DigestStorage implements IDigestStorage {
4656
}
4757
}
4858

59+
private async saveDigestInner(uri: Uri, fileLocation: string, signature: string) {
60+
await this.fs.appendFile(fileLocation, `z${signature}z\n`);
61+
if (!this.loggedFileLocations.has(fileLocation)) {
62+
traceInfo(`Wrote trust for ${uri.toString()} to ${fileLocation}`);
63+
this.loggedFileLocations.add(fileLocation);
64+
}
65+
}
66+
4967
private async getFileLocation(uri: Uri): Promise<string> {
5068
const normalizedName = os.platform() === 'win32' ? uri.fsPath.toLowerCase() : uri.fsPath;
5169
const hashedName = createHash('sha256').update(normalizedName).digest('hex');

src/client/datascience/interactive-ipynb/nativeEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class NativeEditor extends InteractiveBase implements INotebookEditor {
252252

253253
// Sign up for dirty events
254254
model.changed(this.modelChanged.bind(this));
255-
this.previouslyNotTrusted = model.isTrusted;
255+
this.previouslyNotTrusted = !model.isTrusted;
256256
}
257257

258258
// tslint:disable-next-line: no-any

src/client/datascience/interactive-ipynb/trustCommandHandler.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import { Uri } from 'vscode';
7+
import { commands, Uri } from 'vscode';
88
import { IExtensionSingleActivationService } from '../../activation/types';
99
import { IApplicationShell, ICommandManager } from '../../common/application/types';
1010
import { ContextKey } from '../../common/contextKey';
@@ -57,18 +57,25 @@ export class TrustCommandHandler implements IExtensionSingleActivationService {
5757
DataScience.doNotTrustNotebook(),
5858
DataScience.trustAllNotebooks()
5959
);
60-
if (selection !== DataScience.trustNotebook() || model.isTrusted) {
61-
return;
60+
61+
switch (selection) {
62+
case DataScience.trustAllNotebooks():
63+
commands.executeCommand('workbench.action.openSettings', 'python.dataScience.alwaysTrustNotebooks');
64+
break;
65+
case DataScience.trustNotebook():
66+
// Update model trust
67+
model.update({
68+
source: 'user',
69+
kind: 'updateTrust',
70+
oldDirty: model.isDirty,
71+
newDirty: model.isDirty,
72+
isNotebookTrusted: true
73+
});
74+
const contents = model.getContent();
75+
await this.trustService.trustNotebook(model.file, contents);
76+
break;
77+
default:
78+
break;
6279
}
63-
// Update model trust
64-
model.update({
65-
source: 'user',
66-
kind: 'updateTrust',
67-
oldDirty: model.isDirty,
68-
newDirty: model.isDirty,
69-
isNotebookTrusted: true
70-
});
71-
const contents = model.getContent();
72-
await this.trustService.trustNotebook(model.file, contents);
7380
}
7481
}

src/datascience-ui/native-editor/nativeCell.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ export class NativeCell extends React.Component<INativeCellProps> {
281281

282282
// tslint:disable-next-line: cyclomatic-complexity max-func-body-length
283283
private keyDownInput = (cellId: string, e: IKeyboardEvent) => {
284+
if (!this.isNotebookTrusted() && !isCellNavigationKeyboardEvent(e)) {
285+
return;
286+
}
284287
const isFocusedWhenNotSuggesting = this.isFocused() && e.editorInfo && !e.editorInfo.isSuggesting;
285288
switch (e.code) {
286289
case 'ArrowUp':
@@ -886,3 +889,15 @@ export class NativeCell extends React.Component<INativeCellProps> {
886889
export function getConnectedNativeCell() {
887890
return connect(null, actionCreators)(NativeCell);
888891
}
892+
893+
function isCellNavigationKeyboardEvent(e: IKeyboardEvent) {
894+
return (
895+
e.code === 'Enter' ||
896+
e.code === 'NumpadEnter' ||
897+
e.code === 'ArrowUp' ||
898+
e.code === 'k' ||
899+
e.code === 'ArrowDown' ||
900+
e.code === 'j' ||
901+
e.code === 'Escape'
902+
);
903+
}

0 commit comments

Comments
 (0)