| 1 | import * as crypto from "crypto"; |
| 2 | import * as fs from "fs"; |
| 3 | import * as path from "path"; |
| 4 | |
| 5 | import * as vscode from "vscode"; |
| 6 | |
| 7 | const ENCRYPTION_KEY_NAME = "dev.continue.continue"; |
| 8 | |
| 9 | /** |
| 10 | * vscode.SecretStorage is not reliable (often loads older values for a key) |
| 11 | * but keytar cannot be used in vscode extensions without majorly complicating the build |
| 12 | * so we store the encryption key in vscode.SecrteStorage, and handle the encrypted data ourselves |
| 13 | */ |
| 14 | export class SecretStorage { |
| 15 | private globalStoragePath: string; |
| 16 | private secrets: vscode.SecretStorage; |
| 17 | |
| 18 | constructor(context: vscode.ExtensionContext) { |
| 19 | this.globalStoragePath = context.globalStorageUri.fsPath; |
| 20 | if (!fs.existsSync(this.globalStoragePath)) { |
| 21 | fs.mkdirSync(this.globalStoragePath); |
| 22 | } |
| 23 | this.secrets = context.secrets; |
| 24 | } |
| 25 | |
| 26 | private algorithm = "aes-256-gcm"; |
| 27 | private keyLength = 32; // 256 bits |
| 28 | private ivLength = 16; // 128 bits |
| 29 | private saltLength = 64; |
| 30 | private tagLength = 16; |
| 31 | |
| 32 | async getOrCreateEncryptionKey(): Promise<Buffer> { |
| 33 | let key = await this.secrets.get(ENCRYPTION_KEY_NAME); |
| 34 | if (!key) { |
| 35 | key = crypto.randomBytes(this.keyLength).toString("hex"); |
| 36 | await this.secrets.store(ENCRYPTION_KEY_NAME, key); |
| 37 | } |
| 38 | return Buffer.from(key, "hex"); |
| 39 | } |
| 40 | |
| 41 | async encrypt(data: string, filePath: string): Promise<void> { |
| 42 | const key = await this.getOrCreateEncryptionKey(); |
| 43 | const salt = crypto.randomBytes(this.saltLength); |
| 44 | const iv = crypto.randomBytes(this.ivLength); |
| 45 | |
| 46 | const cipher: crypto.CipherGCM = crypto.createCipheriv( |
| 47 | this.algorithm, |
| 48 | key, |
| 49 | iv, |
| 50 | ) as crypto.CipherGCM; |
| 51 | const encrypted = Buffer.concat([ |
| 52 | cipher.update(data, "utf8"), |
| 53 | cipher.final(), |
| 54 | ]); |
| 55 | const tag = cipher.getAuthTag(); |
| 56 | |
| 57 | const result = Buffer.concat([salt, iv, tag, encrypted]); |
| 58 | fs.writeFileSync(filePath, result); |
| 59 | } |
| 60 | |
| 61 | async decrypt(filePath: string): Promise<string> { |
| 62 | const key = await this.getOrCreateEncryptionKey(); |
| 63 | const data = fs.readFileSync(filePath); |
| 64 | |
| 65 | const salt = data.subarray(0, this.saltLength); |
| 66 | const iv = data.subarray(this.saltLength, this.saltLength + this.ivLength); |
| 67 | const tag = data.subarray( |
| 68 | this.saltLength + this.ivLength, |
| 69 | this.saltLength + this.ivLength + this.tagLength, |
| 70 | ); |
| 71 | const encrypted = data.subarray( |
| 72 | this.saltLength + this.ivLength + this.tagLength, |
| 73 | ); |
| 74 | |
| 75 | const decipher: crypto.DecipherGCM = crypto.createDecipheriv( |
| 76 | this.algorithm, |
| 77 | key, |
| 78 | iv, |
| 79 | ) as crypto.DecipherGCM; |
| 80 | decipher.setAuthTag(tag); |
| 81 | |
| 82 | const decrypted = Buffer.concat([ |
| 83 | decipher.update(encrypted), |
| 84 | decipher.final(), |
| 85 | ]); |
| 86 | return decrypted.toString("utf8"); |
| 87 | } |
| 88 | |
| 89 | private keyToFilepath(key: string): string { |
| 90 | // Replace invalid characters with underscores |
| 91 | const sanitizedKey = key.replace(/[\\/:*?"<>|]/g, "_"); |
| 92 | return path.join(this.globalStoragePath, `${sanitizedKey}.bin`); |
| 93 | } |
| 94 | |
| 95 | async store(key: string, value: string): Promise<void> { |
| 96 | const filePath = this.keyToFilepath(key); |
| 97 | await this.encrypt(value, filePath); |
| 98 | } |
| 99 | |
| 100 | async get(key: string): Promise<string | undefined> { |
| 101 | const filePath = this.keyToFilepath(key); |
| 102 | if (fs.existsSync(filePath)) { |
| 103 | const value = await this.decrypt(filePath); |
| 104 | return value; |
| 105 | } |
| 106 | return undefined; |
| 107 | } |
| 108 | } |