|
| 1 | +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +import * as vscode from "vscode"; |
| 4 | +import { EXTENSION_NS } from "./constants"; |
| 5 | + |
| 6 | +const APPROVED_PATHS_KEY = "deno.approvedPaths"; |
| 7 | + |
| 8 | +export interface DenoPathInfo { |
| 9 | + path: string; |
| 10 | + isFromWorkspace: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +export function getDenoPathInfo(): DenoPathInfo | undefined { |
| 14 | + const config = vscode.workspace.getConfiguration(EXTENSION_NS); |
| 15 | + const inspection = config.inspect<string>("path"); |
| 16 | + |
| 17 | + const rawPath = config.get<string>("path"); |
| 18 | + if (typeof rawPath === "string" && rawPath.trim().length > 0) { |
| 19 | + // check if path is set in workspace or folder settings (not global/user) |
| 20 | + const workspaceValue = inspection?.workspaceValue; |
| 21 | + const folderValue = inspection?.workspaceFolderValue; |
| 22 | + const isFromWorkspace = (typeof workspaceValue === "string" && workspaceValue.trim().length > 0) |
| 23 | + || (typeof folderValue === "string" && folderValue.trim().length > 0); |
| 24 | + return { |
| 25 | + path: rawPath.trim(), |
| 26 | + isFromWorkspace, |
| 27 | + }; |
| 28 | + } else { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export class ApprovedConfigPaths { |
| 34 | + readonly #context: vscode.ExtensionContext; |
| 35 | + readonly #sessionDeniedPaths = new Set<string>(); |
| 36 | + |
| 37 | + constructor(context: vscode.ExtensionContext) { |
| 38 | + this.#context = context; |
| 39 | + } |
| 40 | + |
| 41 | + #getApprovedPaths(): string[] { |
| 42 | + const value = this.#context.workspaceState.get(APPROVED_PATHS_KEY); |
| 43 | + if (!Array.isArray(value)) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + return value.filter((item): item is string => typeof item === "string"); |
| 47 | + } |
| 48 | + |
| 49 | + isPathApproved(path: string): boolean { |
| 50 | + const approvedPaths = this.#getApprovedPaths(); |
| 51 | + return approvedPaths.includes(path); |
| 52 | + } |
| 53 | + |
| 54 | + async #approvePath(path: string): Promise<void> { |
| 55 | + const approvedPaths = this.#getApprovedPaths(); |
| 56 | + if (!approvedPaths.includes(path)) { |
| 57 | + approvedPaths.push(path); |
| 58 | + await this.#context.workspaceState.update(APPROVED_PATHS_KEY, approvedPaths); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** Prompts the user for approval if the path hasn't been approved yet. */ |
| 63 | + async promptForApproval(pathInfo: DenoPathInfo | undefined): Promise<boolean> { |
| 64 | + // null and global paths don't need approval |
| 65 | + if (pathInfo == null || !pathInfo.isFromWorkspace) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + const path = pathInfo.path; |
| 70 | + if (this.isPathApproved(path)) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + // already denied for this session |
| 75 | + if (this.#sessionDeniedPaths.has(path)) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + const allow = "Allow"; |
| 80 | + const deny = "Don't Allow"; |
| 81 | + const result = await vscode.window.showWarningMessage( |
| 82 | + `A workspace setting wants to run a custom Deno executable: ${path}`, |
| 83 | + allow, |
| 84 | + deny, |
| 85 | + ); |
| 86 | + |
| 87 | + if (result === allow) { |
| 88 | + await this.#approvePath(path); |
| 89 | + return true; |
| 90 | + } |
| 91 | + if (result === deny) { |
| 92 | + this.#sessionDeniedPaths.add(path); |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | +} |
0 commit comments