synology_write_file
Write or update Docker configuration files directly on Synology NAS within /volume1/docker.
Instructions
Write or update a configuration file on the NAS (restricted to /volume1/docker)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | Absolute path to the file on the NAS (must be within /volume1/docker) | |
| content | Yes | File content to write |
Implementation Reference
- src/index.ts:177-188 (registration)Registration of the synology_write_file tool in the ListToolsRequestSchema handler, including its inputSchema with required 'filepath' and 'content' parameters.
{ name: "synology_write_file", description: `Write or update a configuration file on the NAS (restricted to ${NAS_DOCKER_DIR})`, inputSchema: { type: "object", properties: { filepath: { type: "string", description: `Absolute path to the file on the NAS (must be within ${NAS_DOCKER_DIR})` }, content: { type: "string", description: "File content to write" }, }, required: ["filepath", "content"], }, }, - src/index.ts:257-267 (handler)Handler for synology_write_file tool. Extracts filepath and content from args, validates the path, base64-encodes the content to avoid shell escaping issues, then writes via SSH using printf and base64 -d redirection.
else if (name === "synology_write_file") { const { filepath, content } = args as { filepath: string; content: string }; validateRestrictedPath(filepath); const base64Content = Buffer.from(content).toString("base64"); // base64 output is [A-Za-z0-9+/=], safe to single-quote directly. const res = await execSshCommand(`printf '%s' ${shQuote(base64Content)} | base64 -d > ${shQuote(filepath)}`); if (res.code !== 0) { return { content: [{ type: "text", text: `Error writing file: ${res.stderr}` }] }; } return { content: [{ type: "text", text: `File ${filepath} successfully written.` }] }; } - src/index.ts:47-58 (helper)Helper function validateRestrictedPath used by the handler to ensure the filepath is absolute, prevents path traversal (..), and restricts writes to NAS_DOCKER_DIR.
function validateRestrictedPath(filepath: string): void { if (!filepath.startsWith("/")) { throw new Error("Path must be absolute"); } if (filepath.split("/").some((p) => p === "..")) { throw new Error("Path traversal not allowed"); } const base = NAS_DOCKER_DIR.replace(/\/$/, ""); if (!filepath.startsWith(base + "/")) { throw new Error(`Path must be within ${NAS_DOCKER_DIR}`); } } - src/index.ts:28-30 (helper)Helper function shQuote used to safely single-quote strings for shell commands, used when passing base64Content and filepath to the SSH command.
function shQuote(s: string): string { return "'" + s.replace(/'/g, "'\\''") + "'"; }