-
Notifications
You must be signed in to change notification settings - Fork 17
feat(mcp): encrypt local TON config files #319
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
Closed
+115
−7
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14c2ce0
feat: config file encryption
ArkadiyStena 4e73308
fix(mcp): re-encrypt plaintext config on read
ArkadiyStena b2f85a3
docs(mcp): clarify current key storage
ArkadiyStena 179cc6f
chore(mcp): rename read and write functions
ArkadiyStena 3041fd4
chore(mcp): fix typo
ArkadiyStena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@ton/mcp': patch | ||
| --- | ||
|
|
||
| Encrypt local TON config files on first read when an existing plaintext config is detected. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * Copyright (c) TonTech. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| */ | ||
|
|
||
| import { readFileSync, writeFileSync } from 'node:fs'; | ||
| import type { Mode, PathOrFileDescriptor, WriteFileOptions } from 'node:fs'; | ||
| import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto'; | ||
|
|
||
| const ENCRYPTION_ALGORITHM = 'aes-256-gcm'; | ||
| const PROTECTED_FILE_MAGIC = Buffer.from([0x8a, 0x54, 0x4d, 0x01]); | ||
| const ENCRYPTION_KEY_BYTES = 32; | ||
| const ENCRYPTION_IV_BYTES = 12; | ||
| const ENCRYPTION_TAG_BYTES = 16; | ||
| const HEADER_LENGTH = PROTECTED_FILE_MAGIC.length + ENCRYPTION_KEY_BYTES + ENCRYPTION_IV_BYTES + ENCRYPTION_TAG_BYTES; | ||
|
|
||
| type ProtectedFileWriteOptions = WriteFileOptions & { | ||
| mode?: Mode; | ||
| flag?: string; | ||
| }; | ||
|
|
||
| type ProtectedFileReadResult = { content: string; isProtected: boolean }; | ||
|
|
||
| function encodeProtectedText(value: string): Buffer { | ||
| const key = randomBytes(ENCRYPTION_KEY_BYTES); | ||
| const iv = randomBytes(ENCRYPTION_IV_BYTES); | ||
| const cipher = createCipheriv(ENCRYPTION_ALGORITHM, key, iv); | ||
| const encrypted = Buffer.concat([cipher.update(value, 'utf-8'), cipher.final()]); | ||
| const authTag = cipher.getAuthTag(); | ||
|
|
||
| return Buffer.concat([PROTECTED_FILE_MAGIC, key, iv, authTag, encrypted]); | ||
| } | ||
|
|
||
| function decodeProtectedText(value: Buffer): ProtectedFileReadResult { | ||
| if ( | ||
| value.length < PROTECTED_FILE_MAGIC.length || | ||
| !value.subarray(0, PROTECTED_FILE_MAGIC.length).equals(PROTECTED_FILE_MAGIC) | ||
| ) { | ||
| return { content: value.toString('utf-8'), isProtected: false }; | ||
| } | ||
|
|
||
| if (value.length < HEADER_LENGTH) { | ||
| throw new Error('Invalid protected file format.'); | ||
| } | ||
|
|
||
| let offset = PROTECTED_FILE_MAGIC.length; | ||
| const key = value.subarray(offset, offset + ENCRYPTION_KEY_BYTES); | ||
| offset += ENCRYPTION_KEY_BYTES; | ||
| const iv = value.subarray(offset, offset + ENCRYPTION_IV_BYTES); | ||
| offset += ENCRYPTION_IV_BYTES; | ||
| const authTag = value.subarray(offset, offset + ENCRYPTION_TAG_BYTES); | ||
| offset += ENCRYPTION_TAG_BYTES; | ||
| const encrypted = value.subarray(offset); | ||
|
|
||
| const decipher = createDecipheriv(ENCRYPTION_ALGORITHM, key, iv); | ||
| decipher.setAuthTag(authTag); | ||
|
|
||
| return { | ||
| content: Buffer.concat([decipher.update(encrypted), decipher.final()]).toString('utf-8'), | ||
| isProtected: true, | ||
| }; | ||
| } | ||
|
|
||
| export function readMaybeEncryptedFile(path: PathOrFileDescriptor): ProtectedFileReadResult { | ||
| const raw = readFileSync(path); | ||
| return decodeProtectedText(raw); | ||
| } | ||
|
|
||
| export function writeEncryptedFile( | ||
| path: PathOrFileDescriptor, | ||
| data: string, | ||
| options?: ProtectedFileWriteOptions, | ||
ArkadiyStena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ): void { | ||
| const protectedData = encodeProtectedText(data); | ||
| writeFileSync(path, protectedData, { | ||
| ...(options?.mode !== undefined ? { mode: options.mode } : {}), | ||
| ...(options?.flag ? { flag: options.flag } : {}), | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.