UNPKG

@arkade-os/sdk

Version:

Bitcoin wallet SDK with Taproot and Ark integration

139 lines (138 loc) 5.34 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.FileSystemStorageAdapter = void 0; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); class FileSystemStorageAdapter { constructor(dirPath) { // Normalize and resolve the storage base path once this.basePath = path.resolve(dirPath).replace(/[/\\]+$/, ""); } validateAndGetFilePath(key) { // Reject dangerous keys if (key === "." || key === "..") { throw new Error("Invalid key: '.' and '..' are not allowed"); } // Check for null bytes if (key.includes("\0")) { throw new Error("Invalid key: null bytes are not allowed"); } // Check for path traversal attempts before normalization if (key.includes("..")) { throw new Error("Invalid key: directory traversal is not allowed"); } // Check for reserved Windows names (case-insensitive) const reservedNames = /^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i; const keyWithoutExt = key.split(".")[0]; if (reservedNames.test(keyWithoutExt)) { throw new Error(`Invalid key: '${key}' uses a reserved Windows name`); } // Check for trailing spaces or dots if (key.endsWith(" ") || key.endsWith(".")) { throw new Error("Invalid key: trailing spaces or dots are not allowed"); } // Normalize path separators and sanitize key const normalizedKey = key .replace(/[/\\]/g, "_") .replace(/[^a-zA-Z0-9._-]/g, "_"); // Resolve the full path and check for directory traversal const resolved = path.resolve(this.basePath, normalizedKey); const relative = path.relative(this.basePath, resolved); // Reject if trying to escape the base directory if (relative.startsWith("..") || relative.includes(path.sep + "..")) { throw new Error("Invalid key: directory traversal is not allowed"); } return resolved; } async ensureDirectory() { try { await fs.access(this.basePath); } catch { await fs.mkdir(this.basePath, { recursive: true }); } } async getItem(key) { try { const filePath = this.validateAndGetFilePath(key); const data = await fs.readFile(filePath, "utf-8"); return data; } catch (error) { if (error.code === "ENOENT") { return null; } console.error(`Failed to read file for key ${key}:`, error); return null; } } async setItem(key, value) { try { await this.ensureDirectory(); const filePath = this.validateAndGetFilePath(key); await fs.writeFile(filePath, value, "utf-8"); } catch (error) { console.error(`Failed to write file for key ${key}:`, error); throw error; } } async removeItem(key) { try { const filePath = this.validateAndGetFilePath(key); await fs.unlink(filePath); } catch (error) { if (error.code !== "ENOENT") { console.error(`Failed to remove file for key ${key}:`, error); } } } async clear() { try { const entries = await fs.readdir(this.basePath); await Promise.all(entries.map(async (entry) => { const entryPath = path.join(this.basePath, entry); // Use fs.rm with recursive option to handle both files and directories await fs.rm(entryPath, { recursive: true, force: true }); })); } catch (error) { console.error("Failed to clear storage directory:", error); } } } exports.FileSystemStorageAdapter = FileSystemStorageAdapter;